Click on the IAM option, then click the Users option in the left pane, and click the Add Users button to reach the user creation screen:
Make sure to enter a unique user name and select the Access key - Programmatic access checkbox so we can successfully access the S3 bucket from NodeJS.
Next, we'll need to check the Attach existing policies directly option:
Then, we'll need to filter the policies and check the AmazonS3FullAccess option so we can grant full upload access to our NodeJS application:
Click the Next buttons until you get to the end of the user creation wizard, then click the Create user button to finish.
Now, we'll need to create an S3 bucket. From the portal header, search for S3:
On the next screen, you'll see a section to create a new S3 bucket. In here, click the "Create bucket" button to proceed:
Finally, enter a unique name for your S3 bucket, avoiding spaces and uppercase letters, and select an AWS region to host your bucket:
Once you've entered in your information, scroll down to the bottom of the page and click the Create bucket button.
Now that we've created our AWS user and S3 bucket, we can create some functionality in NodeJS to upload files to our S3 bucket.
First, we'll need to create a .env
file to store our environment variables for our application. In this example, we'll call it process.env:
AWS_ACCESS_KEY_ID=[access_key]
AWS_SECRET_ACCESS_KEY=[secret_key]
AWS_BUCKET_NAME=[bucket_name]
Next, we'll need to install the aws-sdk
library using the command line:
npm install aws-sdk
Now, we'll need to import the newly installed library into our application so we can utilize its functionality:
import AWS from 'aws-sdk';
Now, let's initialize the S3
object so we can communicate with the newly installed AWS library:
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
To finish everything up, we'll now upload a file to the S3 bucket:
const filename = "[my-file-name]";
const content = fs.readFileSync(filename);
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: filename,
Body: content
};
s3.upload(params, (error, data) => {
if (error) {
reject(error);
}
resolve(data.Location)
});
When you refresh your S3 bucket in the AWS portal, you should now see your most recent uploads from the NodeJS application.
Give it a test and try different file types to see how easy and efficient the process is.