1

我知道很多人问过这个问题,我查看了他们的帖子,但我仍然没有得到正确的答案,所以就这样吧。

我正在尝试使用 Amazon dev 引入的基于浏览器的上传技术将图像上传到 s3。现在我可以计算出我的策略和签名。但是当我尝试上传图片时,我总是得到“签名不匹配”(>.<)。我遇到的一个主要问题是我拥有的凭证只是临时的:AWS Security Token Service,它由 accessKEY、secretKey 和安全令牌组成。我会发布我的代码,所以请任何人

这是我的 policy_json 转换

function setValues(accesskey, secretkey, token) {
      var folder = 'avatars/email@domain.com/',
          acl = 'public-read', 
          bucket = 'my-bucket';

      var POLICY_JSON = { "expiration": "2013-12-03T12:29:27.000Z",
          "conditions": [
            {"bucket": bucket},
            ["starts-with", "$key", folder],
            {"acl": acl},
            {"success_action_redirect": "201"},
            ["starts-with", "$Content-Type", "image/png"]
          ]
        };
      var policy = Base64.encode(JSON.stringify(POLICY_JSON));
      var signature = b64_hmac_sha1(secretkey, policy);

      return {
          "policy":policy, 
          "signature":signature,
          "folder" : folder,
          "acl" : acl,
          "bucket" : bucket
        }
    }

我的上传

function upload(acl, accesskey, policy, signature, token) {
  $(':button').click(function()
  {
    var file = document.getElementById('file').files[0];
     // var key = "events/" + (new Date).getTime() + '-' + file.name;
    var xdate = '20131016T105000Z';
    // var formData = new FormData($('form')[0]); //$('form')[0]
    var formData = new FormData(); //$('form')[0]
    formData.append("key", "avatars/email@domain.com/${filename}");
    formData.append("acl", acl);
    formData.append("success_action_redirect", "201");
    formData.append("Content-Type", file.type);
    formData.append("AWSAccessKeyId", accesskey);
    formData.append("policy", policy);
    formData.append("signature", signature);
    // formData.append("x-amz-security-token", token);
    formData.append("file", file);
    $.ajax({
        url: 'https://mybucket.s3.amazonaws.com',  //Server script to process data
        type: 'POST',
        xhr: function() {  // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // Check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
            }
            return myXhr;
        },
        //Ajax events
        beforeSend: function(e) { 
          e.setRequestHeader("Authorization", "AWS "+accesskey+":"+signature);
          e.setRequestHeader("x-amz-date", xdate);
          e.setRequestHeader("x-amz-acl", acl);
          e.setRequestHeader("x-amz-security-token", token);

          // alert('Are you sure you want to upload document.'); 
        },
        success: function(e) { alert('Upload completed'); } ,
        error: function(jqXHR, textStatus, errorThrown) { 
          console.log(textStatus);
          console.log(errorThrown);
        } ,
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false
    });
  });
}

我唯一的html

<form enctype="multipart/form-data">
    <input id="file" name="file" type="file" />
    <input type="button" value="Upload" />
</form>

这让我感到困惑,起初在 mybucket 中它有一个名为 avatars 的文件夹,现在当我的同事(say image1.jpg)使用他的 email_address上传图像(say other_email_Address@domain.com)时,当我们查看存储桶时上传成功,它创建了一个名为他的 email_address 的文件夹,并且在里面它的图像。这是为什么?

mybucket/
 - avatars/
  - my_email_address.@domain.com
    - image1
  - other_email_address@domain.com
    - image1
  so on ...

我使用的工具是:webtoolkit.base64.js、sha1.js、base64-binary.js。

4

0 回答 0