I will post three different articles related to the file transferring method using aws-sdk. The first article is about file download.
Initial Setup in AWS
please make sure that you have AWS account with admin authority.
Once you logged in AWS, you should create a policy and attach it to your account, and then create access key.
I'm not going to talk about all the detail of how to setup for AWS, so if you want to know how to do this, please refer to the AWS Official Documentation.
And When you create a policy for file downloading, you should include the following.
Policy
"s3:GetObject",
"s3:ListBucket",
Also, in order to allow others to access your S3 files with the generated URL, you need to set up the CORS policy in the permission tab of your S3 Bucket.
CORS policy
"AllowedMethods": [
"GET",
],
Now, it's ready to access S3 from the code base and download files.
Download File In Javascript Code Base
Once you received the AWS access key and secret key, you can store them with AWS region info and bucket name in the .env file.
Also, please make sure that you installed aws-sdk in your project.
To install aws-sdk, you can simply use npm package manager to do below.
npm install aws-sdk
And here is the simple code to generate the download URL.
First, you need to create S3 bucket object.
const s3bucket = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
signatureVersion: 'v4',
region: process.env.AWS_REGION, // ex) us-west-2
});
And then, use getSignedUrlPromise()
to receive the generated download url.
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Expires: 3000,
Key, // this key is the S3 full file path (ex: mnt/sample.txt)
};
const url = await s3bucket
.getSignedUrlPromise('getObject', params)
.catch((err) => {
logger.error(err);
});
Once, you received the URL, you can use the HTTP request module (in my case, I used axios) to download the file.
// please note that the responseType is stream
const res = await axios.get(url, {
responseType: 'stream',
});
// receive the data as a read stream
const istream = res.data;
// create a write stream with the path including file name and its extension that you want to store the file in your directory.
const ostream = fs.createWriteStream(fullPath);
// using node.js pipe method to pipe the writestream
istream.pipe(ostream);
with the read stream object, you can follow events with .on()
function. The events that you may be interested in during the download are close
(when the stream is finished, meaning download is done), data
(receiving data chunk - downloading process), and error
(when failed to download).
Top comments (2)
Very nice but perhaps you should consider using V3 going further loads of new functionalities and you can bring in just the clients that you need rather than entire SDK.
nice insight. Thanks!