2016-04-14 117 views
1

我正在尝试使用Amazon S3上传简单的JS图像。以下是我的代码。通过XMLHttpRequest将文件上传到Amazon S3公开

JS

host = "https://s3.eu-central-1.amazonaws.com/my.bucket.url/"; 

uploadAttachment = function(attachment) { 
    file = attachment.file; 
    key = createStorageKey(file); 
    form = new FormData; 
    form.append("key", key); 
    form.append("acl", "public-read"); 
    form.append("Content-Type", file.type); 
    form.append("file", file); 
    xhr = new XMLHttpRequest; 
    xhr.open("PUT", host + key, true); 
    // ... 
    xhr.send(form); 
}; 

createStorageKey = function(file) { 
    var date, day, time; 
    date = new Date; 
    day = date.toISOString().slice(0, 10); 
    time = date.getTime(); 
    return "s3/attachments/" + day + "/" + time + "-" + file.name; 
}; 

S3斗政策

{ 
    "Version": "2008-10-17", 
    "Statement": [ 
     { 
      "Sid": "1", 
      "Effect": "Allow", 
      "Principal": "*", 
      "Action": "s3:*", 
      "Resource": "arn:aws:s3:::my.bucket.url/*" 
     } 
    ] 
} 

S3 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>PUT</AllowedMethod> 
     <MaxAgeSeconds>3000</MaxAgeSeconds> 
     <AllowedHeader>*</AllowedHeader> 
    </CORSRule> 
</CORSConfiguration> 

一切正常,ALM ost罚款,但上传的文件不公开。而且,我甚至无法通过网络界面将它们设置为公开。我唯一能做的就是删除它们。我真的需要这些文件公开。

如果可能的话,我真的很想在不使用任何访问键的情况下完成此操作。

任何帮助真的很感激!

+0

你知道你做了什么,在这里?您的存储区可以匿名上传。由任何人,从任何地方,没有证书需要。这就是为什么你不能修改对象 - 你甚至不拥有它们......但它们在你的存储桶中,所以你为它们付钱,这就是为什么你仍然被允许删除它们的原因。你真的需要回头阅读签名和认证请求。不要试图“修复”这一点。你有更大的问题。 –

+0

谢谢你的建议。我知道发生了什么事。我只想让它工作,然后将其限制到单个域。你是说我还有**要签署一切吗? –

+0

@ Michael-sqlbot请你澄清一下:是否有可能在没有签署请求的情况下公开提供上传的文件? –

回答

1

我想通了。

JS

  • 改变hostmy.bucket.s3.eu-central-1.amazonaws.com
  • xhr.open("POST", host, true)
  • 可能无关紧要,但我删除了acl参数。

CORS:

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

S3斗政策:我只是删除它。

S3区权限Granted "Upload/Delete permissions to everyone