8

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?

2 Answers 2

11

You should use promise() with await:

await s3.putObject({...}).promise();
Sign up to request clarification or add additional context in comments.

2 Comments

oh man.. take me an hour until i get here... such a mistake... Thank you!
putObject does not return a promise(), you just need to use await as long as your handler is async
1

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

5 Comments

Still, I'm getting the same result as before.
Hmm, can you try to set Body: JS_code to upload it just as a string?
Strange, I just tried and it worked for me. Maybe something with credentials, are you sure that the request completes ok? Can you try it like this? s3.putObject({ Bucket: 'my-bucket-gayashan', Key: 'myFile.js', ContentType:'binary', Body: Buffer.from(JS_code, 'binary') }, (error, response) => console.log(error, response));
putObject doesn't return a promise, so I don't understand why you use it with await. You should pass a callback to putObject and most likely you will see some error.
OK, I think the problem was that I wasn't chaining the promise() method at the end of putObject() method as per Khalid T's answer below. It solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.