convert-bucket-to-website.ts - AWS Code Sample


/* Copyright, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at Purpose: convert-bucket-to-website.ts is part of a tutorial demonstrating how to build and deploy an app to submit data to an Amazon DynamoDB table. To run the full tutorial, see convert-bucket-to-website.ts applies a bucket policy to convert an Amazon S3 bucket into a static web host. Inputs (replace in code): - REGION - BUCKET_NAME Running the code: node upload-files-to-s3.ts */ const { S3Client, CreateBucketCommand, PutBucketWebsiteCommand, PutBucketPolicyCommand, } = require("@aws-sdk/client-s3"); // Set the AWS Region const REGION = "REGION"; //e.g. "us-east-1" // Create params JSON for S3.createBucket const bucketName = "BUCKET_NAME"; //BUCKET_NAME const readOnlyAnonUserPolicy = { Version: "2012-10-17", Statement: [ { Sid: "AddPerm", Effect: "Allow", Principal: "*", Action: ["s3:GetObject"], Resource: [""], }, ], }; // create selected bucket resource string for bucket policy var bucketResource = "arn:aws:s3:::" + bucketName + "/*"; //BUCKET_NAME readOnlyAnonUserPolicy.Statement[0].Resource[0] = bucketResource; // convert policy JSON into string and assign into params var bucketPolicyParams = { Bucket: bucketName, Policy: JSON.stringify(readOnlyAnonUserPolicy), }; // Instantiate an S3 client const s3 = new S3Client(REGION); const run = async () => { try { const response = await s3.send( new PutBucketPolicyCommand(bucketPolicyParams) ); console.log("Success, permissions added to bucket", response); } catch (err) { console.log("Error", err); } }; run(); // For unit tests only = run();