How to upload files to storage bucket ?
Introduction
[edit]In modern computing environments, efficient data management is paramount. Cloud storage solutions offer scalable and reliable options for storing and managing data. Amazon Simple Storage Service (S3) is one such solution provided by Amazon Web Services (AWS), offering secure, durable, and highly available object storage. In this guide, we'll walk you through the process of uploading files from a remote server to an S3 bucket using a Node.js script.
Prerequisites
[edit]1. SSH Access: Ensure that you have a user account with sudo privileges to SSH into the remote server where the files are located.
2. AWS Access Keys: Obtain the Access Key ID and Secret Access Key associated with your AWS account, which has permission to access the target S3 bucket.
Implementation
[edit]Step 1: Create a Script for File Upload
Before proceeding, make sure you have Node.js and npm installed on your system. Then, follow these steps:
Install AWS SDK: Run npm install aws-sdk to install the AWS SDK for JavaScript.
Create a Node.js Script (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 the 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);
}
});
Replace 'YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY', 'YOUR_BUCKET_NAME', 'path/to/example.txt', and 'example.txt' with your actual AWS access keys, bucket name, and file path.
Step 2: Accessing the File
After executing the script, you can access the uploaded file via its URL. For example, if your domain is example.com, and the file is example.txt, you can access it using:
http://example.com/example.txt
Conclusion
[edit]By following the steps outlined in this guide, you can efficiently upload files from a remote server to an Amazon S3 bucket using Node.js. This process leverages the AWS SDK to interact with S3, ensuring secure and reliable file transfers.