AWS开发工具包S3-如何在putObject方法中包括凭证

时间:2018-08-17 23:35:11

标签: javascript amazon-web-services amazon-s3 aws-sdk

使用此HTML表单的方法:

<form action="https://s3.us-east-1.amazonaws.com/BUCKET" method="post" encType="multipart/form-data">

   <input type="hidden" name="key" value="${filename}" /><br />

   <input type="hidden"  name="Content-Type" value="image/jpeg" /><br />
   <input type="hidden" name="x-amz-server-side-encryption" value="AES256" />
   <input type="hidden"   name="X-Amz-Credential" value="MY_CERDENTIALS" />
   <input type="hidden"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
   <input type="hidden"   name="X-Amz-Date" value="20180817T040045Z" />

   <input type="hidden" name="Policy" value='MY_POLICY' />
   <input type="hidden" name="X-Amz-Signature" value="MY_SIGN" />
   File:
   <input type="file"   name="file" /> <br />

   <input type="submit" name="submit" value="Upload to Amazon S3" />
 </form>

我想在JavaScript中使用ASW SDK方法putObject。我设法创建了以下代码:

AWS.config.update({
    endpoint: new AWS.Endpoint('https://s3.amazonaws.com'),
    accessKeyId: 'MY_ID', // the app does not run on AWS, so I need credentials
    secretAccessKey: 'MY_SECRET_KEY',
    s3ForcePathStyle: true,

  });
AWS.config.region = "us-east-1";

const params = {
  params:{
    Bucket: 'ballot-box',
  }
}


var bucket = new AWS.S3(params);


const requestParams = {
  Key: 'test.png',
  ContentType: 'image/jpeg',
  Body: file
}

bucket.putObject(requestParams,function(err,file){
  if(err)
    reject({error: err})
  else
    resolve({image: encode(file.Body)})

我现在的问题是我不知道该在何处以及如何放置该策略,X-Amz-签名,X-Amz-日期,X-Amz-算法,X-Amz-凭证,x-amz-服务器端加密和X-Amz-Date。

编辑:

我的存储桶的CORS配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

1 个答案:

答案 0 :(得分:1)

browser-based upload via POST迁移到JavaScript SDK时,无需提供策略,X-Amz-签名,X-Amz-日期,X-Amz-算法,X-Amz凭证或X-Amz-Date。

如果需要,您可以通过ServerSideEncryption:“ AES256”在请求参数中提供x-amz-server-side-encryption及其加密方式。