Igor L.
Igor L.

Reputation: 3445

How to upload a file from the filesystem to s3

I have a file in the path ./files/myfile.zip

That I would like to upload to s3.

I have a function:

const writeS3Async = (bucketName, fileName, strInput) => {
    const params = {
        Bucket: bucketName,
        Key: fileName,
        Body: strInput,
    };

    return s3.upload(params).promise();
};

The writeS3Async method is used for uploading strings into an s3 object, not files.

Interestingly, I could not find a decent piece of code for a direct file upload.

Upvotes: 1

Views: 395

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

You need to read the file first. Something like this

var AWS = require('aws-sdk'),
    fs = require('fs');

fs.readFile('./files/myfile.zip', function (err, data) {
  if (err) { throw err; }

  var s3 = new AWS.S3();
  const params = {
    Bucket: bucketName,
    Key: fileName,
    Body: data
  };
  return s3.client.putObject(params).promise();

});

Upvotes: 1

Related Questions