Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws - Uploading a string as file to a S3 bucket

I'm trying to save a string as a file to an AWS S3 bucket using the AWS SDK for NodeJS. The PUT request gets succeeded, but the file does not get created in the S3 bucket. Following is a snippet from my code.

const s3 = new S3({ apiVersion: '2006-03-01' });

const JS_code = `
  let x = 'Hello World';
`;

// I'm using the following method inside an async function.
await s3.putObject({
        Bucket: 'my-bucket-gayashan',
        Key: 'myFile.js',
        ContentType:'binary',
        Body: Buffer.from(JS_code, 'binary')
      });

Can someone please help me with this?

like image 795
gayashanbc Avatar asked Apr 19 '18 09:04

gayashanbc


2 Answers

You should use promise() with await:

await s3.putObject({...}).promise();
like image 145
Khalid T. Avatar answered Sep 30 '22 20:09

Khalid T.


You should provide a valid MIME type in the ContentType field. In your case it should be application/javascript.

like image 36
poohitan Avatar answered Sep 30 '22 21:09

poohitan