When using the aws-sdk npm plugin for nods.js, I can upload a pdf on 50kb with the following code (to AWS s3):
var params = {
Bucket: BUCKET,
Key: pdf_key,
Body: file,
ContentType: 'application/pdf'
};
var s3 = new AWS.S3();
s3.putObject(params, function(error, data) {
console.log(data);
console.log(error);
if (error) {
console.log(error);
callback(error, null);
} else {
callback(null, pdf_key);
}
});
But when uploading a 11mb pdf, even with specifying the ContentLength
, the upload just continues forever, even with a timeout of 2 minutes.
The question is how do I make aws s3 accept the large pdf file?
UPDATE
I have still not found any documentation or anwers for the question.
UPDATE 2
I will accept answers which show's this or another framework that can do this. I will need that framework to be able to also allow auth-read of the object.
UPDATE 3 I got it working for now but I haven't found a reason it shouldn't work.
Thanks in advance!
The size of an object in S3 can be from a minimum of 0 bytes to a maximum of 5 terabytes, so, if you are looking to upload an object larger than 5 gigabytes, you need to use either multipart upload or split the file into logical chunks of up to 5GB and upload them manually as regular uploads.
When you upload large files to Amazon S3, it's a best practice to leverage multipart uploads. If you're using the AWS Command Line Interface (AWS CLI), then all high-level aws s3 commands automatically perform a multipart upload when the object is large. These high-level commands include aws s3 cp and aws s3 sync.
Upload speed to AWS S3 tops out at 2.3 Mbps. Tried the multipart upload but even with 10 concurrent threads, the total speed remains the same, just gets split between all threads ~20 KB/s.
Now the bucket is ready and we can jump to coding section to get started with Node JS to upload files to AWS S3. The easiest way to get started with Node JS and AWS S3 to upload files is through AWS SDK. Install the AWS SDK using any package manager.
Next, you need to create a bucket for uploading a file (after configuring your AWS CLI ). Let’s create a bucket with the s3 command. First of all, you need to import the aws-sdk module and create a new S3 object. It uses the credentials that you set for your AWS CLI. Locking in API version for S3 object is optional.
Let’s first create a project folder called nodeS3 and install SDK. Then, create the main program file and data folder. In the data folder, drop any file you want. In this example, I am using a json file called data.json. Next, you need to create a bucket for uploading a file (after configuring your AWS CLI ).
To interact with any AWS services, Node.js requires AWS SDK for JavaScript. Let’s first create a project folder called nodeS3 and install SDK. Then, create the main program file and data folder. In the data folder, drop any file you want. In this example, I am using a json file called data.json.
Connecting to S3 isn't fast and then depending on the network fluctuations you can get timeouts and other weird behaviors.
The code you provided is fine, but you could take advantage of multipart uploads that could solve problems especially with >5MB files.
I made a rough implementation of a multipart upload and also made it to retry the upload of any failing part up to 3 times, this will also work for smaller files than 5MB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With