Eliminar todos los objetos de un bucket de Amazon S3 dado mediante un SDK de AWS
En los siguientes ejemplos de código, se muestra cómo eliminar todos los objetos de un bucket de Amazon S3.
- JavaScript
-
- SDK para JavaScript (v3)
-
Elimine todos los objetos de un bucket de Amazon S3 dado.
import {
DeleteObjectsCommand,
paginateListObjectsV2,
S3Client,
} from "@aws-sdk/client-s3";
/**
*
* @param {{ bucketName: string }} config
*/
export const main = async ({ bucketName }) => {
const client = new S3Client({});
try {
console.log(`Deleting all objects in bucket: ${bucketName}`);
const paginator = paginateListObjectsV2(
{ client },
{
Bucket: bucketName,
},
);
const objectKeys = [];
for await (const { Contents } of paginator) {
objectKeys.push(...Contents.map((obj) => ({ Key: obj.Key })));
}
const deleteCommand = new DeleteObjectsCommand({
Bucket: bucketName,
Delete: { Objects: objectKeys },
});
await client.send(deleteCommand);
console.log(`All objects deleted from bucket: ${bucketName}`);
} catch (caught) {
if (caught instanceof Error) {
console.error(
`Failed to empty ${bucketName}. ${caught.name}: ${caught.message}`,
);
}
}
};
// Call function if run directly.
import { fileURLToPath } from "node:url";
import { parseArgs } from "node:util";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const options = {
bucketName: {
type: "string",
},
};
const { values } = parseArgs({ options });
main(values);
}
Para obtener una lista completa de las guías para desarrolladores de AWS SDK y ejemplos de código, consulte Desarrollo con Amazon S3 mediante los SDK de AWS. En este tema también se incluye información sobre cómo empezar a utilizar el SDK y detalles sobre sus versiones anteriores.