Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.120.0

Managing Amazon S3 Bucket Access Permissions


                                            Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you 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, setting the region, and providing credentials for your code. In this example, the credentials are provided using the JSON file you created.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json');

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.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // 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.

Copy
node s3_getbucketacl.js BUCKET_NAME

This sample code can be found here on GitHub.