首页 > 解决方案 > 使用 Clojure 的 Amazonica 创建 S3 签名 URL 时如何指定 ACL 策略?

问题描述

我正在尝试使上传的 ACL 为public-read. Amazonica的文档非常薄,经过数小时的修修补补,我还没有弄清楚如何实现这一目标。简而言之,我不知道如何让它签署标题。

服务器端,我的代码如下所示。

(s3/generate-presigned-url 
  creds
  {:bucket-name          "mybucket"
   :method               "PUT"
   :expires              10000
   :key                  "my-key"
   :cache-control        "max-age=31557600;"
   :request-parameters {:x-amz-acl "public-read"}
   })

客户端,我获取创建并执行 XHR PUT 请求的 URL

var xhr = new XMLHttpRequest();
    xhr.open("PUT", signedUrl);
    xhr.setRequestHeader('Cache-Control', 'max-age=31557600')
    xhr.onload = ...
    xhr.onerror = ...
xhr.send(file);

这完美地工作,除了它有错误的ACL:“私人”而不是“公共”

添加它的客户端很容易

var xhr = new XMLHttpRequest();
    xhr.open("PUT", signedUrl);
    xhr.setRequestHeader('Cache-Control', 'max-age=31557600')
    xhr.setRequestHeader('x-amz-acl', 'public-read')
    xhr.onload = ...
    xhr.onerror = ...
xhr.send(file);

但是由于HeadersNotSigned. 我根本不知道如何在服务器端添加它以便他们得到签名。该SignedHeaders部分从不包含任何附加参数。

我盲目地尝试过各种组合

(s3/generate-presigned-url 
  creds
  {:headers              {:x-amz-acl "public-read"}
   :x-amz-acl            "public-read"
   :metadata             {:x-amz-acl "public-read"}
   :signed-headers       {:x-amz-acl "public-read"}
   :amz-acl "public-read"
   :x-amz-signed-headers {:x-amz-acl "public-read"}
   :X-Amz-SignedHeaders ["x-amz-acl"]
   :request-parameters {:x-amz-acl "public-read"}
   })

如何将 ACL 策略添加到签名的 url?

标签: amazon-s3clojureamazonica

解决方案


我对此没有直接的答案,但有一个解决方法供您考虑:将 s3 存储桶中的所有对象默认为公开读取。

您可以通过将此存储桶策略添加到您的存储桶来做到这一点(bucketnm当然要替换):

{
  "Id": "Policy1397632521960",
  "Statement": [
    {
      "Sid": "Stmt1397633323327",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::bucketnm/*",
      "Principal": {
        "AWS": [
          "*"
        ]
      }
    }
  ]
}

推荐阅读