Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Creating and Using Amazon S3 Buckets


                                            Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Obtain and display a list of Amazon S3 buckets in your account.

  • Create an Amazon S3 bucket.

  • Upload an object to a specified bucket.

The Scenario

In this example, a series of Node.js modules are used to obtain a list of existing Amazon S3 buckets, create a bucket, and upload a file to a specified bucket. These Node.js modules use the SDK for JavaScript to get information from and upload files to an Amazon S3 bucket using these methods of the Amazon S3 client class:

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');

Displaying a List of Amazon S3 Buckets

Create a Node.js module with the file name s3_listbuckets.js. Make sure to configure the SDK as previously shown. To access Amazon Simple Storage Service, create an AWS.S3 service object. Call the listBuckets method of the Amazon S3 service object to retrieve a list of your buckets. The data parameter of the callback function has a Buckets property containing an array of maps to represent the buckets. Display the bucket list by logging it to the console.

Copy
// Load the SDK for JavaScript 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'}); // Call S3 to list current buckets s3.listBuckets(function(err, data) { if (err) { console.log("Error", err); } else { console.log("Bucket List", data.Buckets); } });

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

Copy
node s3_listbuckets.js

This sample code can be found here on GitHub.

Creating an Amazon S3 Bucket

Create a Node.js module with the file name s3_createbucket.js. Make sure to configure the SDK as previously shown. Create an AWS.S3 service object. The module will take a single command-line argument to specify a name for the new bucket.

Add a variable to hold the parameters used to call the createBucket method of the Amazon S3 service object, including the name for the newly created bucket. The callback function logs the new bucket's location to the console after Amazon S3 successfully creates it.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create the parameters for calling createBucket var bucketParams = { Bucket : process.argv[2] }; // Call S3 to create the bucket s3.createBucket(bucketParams, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Location); } });

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

Copy
node s3_createbucket.js BUCKET_NAME

This sample code can be found here on GitHub.

Uploading a File to an Amazon S3 Bucket

Create a Node.js module with the file name s3_upload.js. Make sure to configure the SDK as previously shown. Create an AWS.S3 service object. The module will take two command-line arguments, the first one to specify the destination bucket and the second to specify the file to upload.

Create a variable with the parameters needed to call the upload method of the Amazon S3 service object. Provide the name of the target bucket in the Bucket parameter. The Key parameter is set to the name of the selected file, which you can obtain using the Node.js path module. The Body parameter is set to the contents of the file, which you can obtain using createReadStream from the Node.js fs module.

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'}); // call S3 to retrieve upload file to specified bucket var uploadParams = {Bucket: process.argv[2], Key: '', Body: ''}; var file = process.argv[3]; var fs = require('fs'); var fileStream = fs.createReadStream(file); fileStream.on('error', function(err) { console.log('File Error', err); }); uploadParams.Body = fileStream; var path = require('path'); uploadParams.Key = path.basename(file); // call S3 to retrieve upload file to specified bucket s3.upload (uploadParams, function (err, data) { if (err) { console.log("Error", err); } if (data) { console.log("Upload Success", data.Location); } });

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

Copy
node s3_upload.js BUCKET_NAME FILE_NAME

This sample code can be found here on GitHub.