2023-10-02 09:18:52 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[GettingStarted.JavaScript.NodeJS.sampleV3]
// This is used for getting user input.
2024-10-16 11:53:18 -04:00
import { createInterface } from "node:readline/promises" ;
2023-10-02 09:18:52 -04:00
import {
S3Client ,
PutObjectCommand ,
CreateBucketCommand ,
DeleteObjectCommand ,
DeleteBucketCommand ,
paginateListObjectsV2 ,
GetObjectCommand ,
} from "@aws-sdk/client-s3" ;
export async function main ( ) {
// A region and credentials can be declared explicitly. For example
// `new S3Client({ region: 'us-east-1', credentials: {...} })` would
//initialize the client with those settings. However, the SDK will
// use your local configuration and credentials if those properties
// are not defined here.
const s3Client = new S3Client ( { } ) ;
// Create an Amazon S3 bucket. The epoch timestamp is appended
// to the name to make it unique.
const bucketName = ` test-bucket- ${ Date . now ( ) } ` ;
await s3Client . send (
new CreateBucketCommand ( {
Bucket : bucketName ,
2024-10-16 11:53:18 -04:00
} ) ,
2023-10-02 09:18:52 -04:00
) ;
// Put an object into an Amazon S3 bucket.
await s3Client . send (
new PutObjectCommand ( {
Bucket : bucketName ,
Key : "my-first-object.txt" ,
Body : "Hello JavaScript SDK!" ,
2024-10-16 11:53:18 -04:00
} ) ,
2023-10-02 09:18:52 -04:00
) ;
// Read the object.
const { Body } = await s3Client . send (
new GetObjectCommand ( {
Bucket : bucketName ,
Key : "my-first-object.txt" ,
2024-10-16 11:53:18 -04:00
} ) ,
2023-10-02 09:18:52 -04:00
) ;
console . log ( await Body . transformToString ( ) ) ;
// Confirm resource deletion.
const prompt = createInterface ( {
input : process . stdin ,
output : process . stdout ,
} ) ;
const result = await prompt . question ( "Empty and delete bucket? (y/n) " ) ;
prompt . close ( ) ;
if ( result === "y" ) {
// Create an async iterator over lists of objects in a bucket.
const paginator = paginateListObjectsV2 (
{ client : s3Client } ,
2024-10-16 11:53:18 -04:00
{ Bucket : bucketName } ,
2023-10-02 09:18:52 -04:00
) ;
for await ( const page of paginator ) {
const objects = page . Contents ;
if ( objects ) {
// For every object in each page, delete it.
for ( const object of objects ) {
await s3Client . send (
2024-10-16 11:53:18 -04:00
new DeleteObjectCommand ( { Bucket : bucketName , Key : object . Key } ) ,
2023-10-02 09:18:52 -04:00
) ;
}
}
}
// Once all the objects are gone, the bucket can be deleted.
await s3Client . send ( new DeleteBucketCommand ( { Bucket : bucketName } ) ) ;
}
}
// Call a function if this file was run directly. This allows the file
// to be runnable without running on import.
2024-10-16 11:53:18 -04:00
import { fileURLToPath } from "node:url" ;
2023-10-02 09:18:52 -04:00
if ( process . argv [ 1 ] === fileURLToPath ( import . meta . url ) ) {
main ( ) ;
}
// snippet-end:[GettingStarted.JavaScript.NodeJS.sampleV3]