获取403 AccessDenied仅在使用公共读取ACL时将文件上载到S3

时间:2014-02-26 17:16:16

标签: javascript amazon-s3

我正在尝试对图像数据进行POST(使用JavaScript从浏览器直接进行)。如果我使用private ACL,一切正常,但如果我将其更改为public-read,我会收到403回复,邮件为AccessDenied

  • 我的政策中的acl属性与我发送的acl表单参数相匹配。
  • 我肯定已经过了身份验证阶段,好像我故意搞砸了签名,我得到了SignatureDoesNotMatch回复。
  • 正如我所说,使用不同的ACL,上传工作正常。

任何可能导致这种情况的想法?

根据要求,这里是我正在使用的代码(在我开始工作后需要整理一下):

.directive 's3ImageUploader', ['$http', '$filter', ($http, $filter) ->
  replace: true
  restrict: 'E'
  scope: true
  template: '<input type="file" name="image" class="form-control input-lg">'
  link: ($scope, $element) ->
    awsAccessKeyId = '...'
    secret = '...'
    bucket = '...'
    folder = '...'
    policy =
      expiration: '2020-12-01T12:00:00.000Z'
      conditions: [
        {bucket: bucket}
        ['starts-with', '$key', '']
        {acl: 'public-read'}
        ['starts-with', '$Content-Type', 'image/']
      ]
    policyBase64 = $filter('base64') policy
    signature = $filter('hmacSha1') policyBase64, secret

    $element.on 'change', ->
      file = $element.get(0).files[0]
      key = "#{folder}/#{new Date().getTime()}-#{file.name}"

      formData = new FormData()
      formData.append 'key', key
      formData.append 'acl', 'public-read'
      formData.append 'Content-Type', file.type
      formData.append 'AWSAccessKeyId', awsAccessKeyId
      formData.append 'policy', policyBase64
      formData.append 'signature', signature
      formData.append 'file', file

      $http.post "https://#{bucket}.s3.amazonaws.com/", formData,
        headers:
          'Content-Type': `undefined`
        transformRequest: (data) -> data
]

1 个答案:

答案 0 :(得分:1)

事实证明,我使用其凭据的用户需要设置一个策略,以便我使用public-read

相关问题