How to upload files to AWS S3 bucket via AWS CLI method ?
Introduction
[edit]AWS CLI or Amazon Web Service Command Line Interface is a command-line tool for managing and administering your Amazon Web Services. AWS CLI provides direct access to the public API (Application Programming Interface) of Amazon Web Services. Since it’s a command-line tool, we can also use it to create scripts for automating your Amazon Web Services
Prerequisite
[edit]- A user with sudo access to SSH the remote server
- AWS access & secret key
- A valid domain name with only CNAME record that point to the AWS S3 bucket (A record is not required)
Implementation
[edit]Step 1: Update the APT package repository cache
$ sudo apt-get update
Step 2: Install AWS CLI on Ubuntu 22.04 LTS from the official package repository
$ sudo apt-get install awscli
Step 3: Check the AWS CLI version
$aws --version
Step 4: Configure the AWS account with access and secret key
$ aws configure
Note: Provide the access & secret key of the AWS account where we are uploading the files
Step 5: Create a scrit to upload the files from remote server to AWS S3 bucket
Note:
- Here we tested with a Java script as test.js
- Replace YOUR_ACCESS_KEY, YOUR_SECRET_KEY, YOUR_BUCKET_NAME, path/to/example.txt, example.txt with the appropriate values
- Ensure node, npm & "npm install aws-sdk" should be installed
- Run the script as node test.js
const AWS = require('aws-sdk');
const fs = require('fs');
// Configure AWS credentials
AWS.config.update({
accessKeyId: 'YOUR_ACCESS_KEY',
secretAccessKey: 'YOUR_SECRET_KEY',
});
// Create an instance of the S3 service
const s3 = new AWS.S3();
// Define the bucket name and file details
const bucketName = 'YOUR_BUCKET_NAME';
const filePath = 'path/to/example.txt';
const fileKey = 'example.txt'
// Read the file from local disk
const fileData = fs.readFileSync(filePath);
// Set the parameters for S3 upload
const uploadParams = {
Bucket: bucketName,
Key: fileKey,
Body: fileData,
};
// Upload the file to S3
s3.upload(uploadParams, (err, data) => {
if (err) {
console.error('Error uploading file:', err);
} else {
console.log('File uploaded successfully:', data.Location);
}
});
Step 6: Access the domain in the browser to ensure the functionality
http://example.com/example.txt
Conclusion
[edit]With AWS CLI and the provided script, uploading files to an AWS S3 bucket becomes a seamless process, enhancing data storage and accessibility on the AWS platform. By following the outlined steps and ensuring proper configurations, users can efficiently manage and automate file uploads to AWS S3, contributing to streamlined workflows and improved data management practices.