Managing Amazon S3 Bucket Access Permissions - AWS SDK for JavaScript

The AWS SDK for JavaScript version 3 (v3) is a rewrite of v2 with some great new features, including modular architecture. For more information, see the AWS SDK for JavaScript v3 Developer Guide.

Managing Amazon S3 Bucket Access Permissions

                        JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to retrieve or set the access control list for an Amazon S3 bucket.

The Scenario

In this example, a Node.js module is used to display the bucket access control list (ACL) for a selected bucket and apply changes to the ACL for a selected bucket. The Node.js module uses the SDK for JavaScript to manage Amazon S3 bucket access permissions using these methods of the Amazon S3 client class:

For more information about access control lists for Amazon S3 buckets, see Managing Access with ACLs in the Amazon Simple Storage Service Developer Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the Region for your code. In this example, the Region is set to us-west-2.

// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the Region AWS.config.update({region: 'us-west-2'});

Retrieving the Current Bucket Access Control List

Create a Node.js module with the file name s3_getbucketacl.js. The module will take a single command-line argument to specify the bucket whose ACL configuration you want. Make sure to configure the SDK as previously shown.

Create an AWS.S3 service object. The only parameter you need to pass is the name of the selected bucket when calling the getBucketAcl method. The current access control list configuration is returned by Amazon S3 in the data parameter passed to the callback function.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create S3 service object s3 = new AWS.S3({apiVersion: '2006-03-01'}); var bucketParams = {Bucket: process.argv[2]}; // call S3 to retrieve policy for selected bucket s3.getBucketAcl(bucketParams, function(err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.Grants); } });

To run the example, type the following at the command line.

node s3_getbucketacl.js BUCKET_NAME

This sample code can be found here on GitHub.