aws - Uploading a string as file to a S3 bucket

£可爱£侵袭症+ 提交于 2020-12-30 08:19:45

问题


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?


回答1:


You should use promise() with await:

await s3.putObject({...}).promise();



回答2:


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



来源:https://stackoverflow.com/questions/49917935/aws-uploading-a-string-as-file-to-a-s3-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!