Elimina tutti gli oggetti in un determinato bucket Amazon S3 utilizzando un. AWS SDK - Esempi di codice dell'AWS SDK

Ci sono altri AWS SDK esempi disponibili nel repository AWS Doc SDK Examples GitHub .

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Elimina tutti gli oggetti in un determinato bucket Amazon S3 utilizzando un. AWS SDK

Il seguente esempio di codice mostra come eliminare tutti gli oggetti in un bucket Amazon S3.

JavaScript
SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Elimina tutti gli oggetti per un determinato bucket Amazon S3.

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 "url"; import { parseArgs } from "util"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const options = { bucketName: { type: "string", }, }; const { values } = parseArgs({ options }); main(values); }