This Node.js code example shows:
- How to obtain and display a list of Amazon S3 buckets in your account.
- How to create an Amazon S3 bucket.
- How to upload an object to a specified bucket.
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:
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listBuckets-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjects-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucket-property
To set up and run this example, you must first complete these tasks:
- Install Node.js. For more information about installing Node.js, see the Node.js website.
- Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading Credentials in Node.js from the Shared Credentials File.
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'});
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.
// Load the SDK for JavaScript
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'});
// 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.
node s3_listbuckets.js
This sample code can be found here on GitHub.
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.
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the parameters for calling createBucket
var bucketParams = {
Bucket : process.argv[2]
};
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
// 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.
node s3_createbucket.js BUCKET_NAME
This sample code can be found here on GitHub.
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.
// 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'});
// 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.
node s3_upload.js BUCKET_NAME FILE_NAME
This sample code can be found here on GitHub.
Create a Node.js module with the file names3_listobjects.js
. Make sure to configure the SDK as previously shown. Create an AWS.S3
service object.
Add a variable to hold the parameters used to call the listObjects
method of the Amazon S3 service object, including the name of the bucket to read. The callback function logs a list of objects (files) or a failure message.
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the parameters for calling createBucket
var bucketParams = {
Bucket : BUCKET_NAME
};
// Call S3 to create the bucket
s3.listObjects(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
To run the example, type the following at the command line.
node s3_listobjects.js
This sample code can be found here on GitHub.
Create a Node.js module with the file name s3_deletebucket.js
. Make sure to configure the SDK as previously shown. Create an AWS.S3
service object.
Add a variable to hold the parameters used to call the createBucket
method of the Amazon S3 service object, including the name of the bucket to delete. The bucket must be empty in order to delete it. The callback function logs a success or failure message.
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the parameters for calling createBucket
var bucketParams = {
Bucket : BUCKET_NAME
};
// Call S3 to create the bucket
s3.deleteBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
To run the example, type the following at the command line.
node s3_deletebucket.js
This sample code can be found here on GitHub.