添加x-amz-expires以查询身份验证标头会产生错误(AccessDenied)

时间:2016-04-28 04:55:52

标签: unity3d amazon-s3

我使用Unity的WWW类从亚马逊服务器下载资产。它工作正常,直到我将x-amz-expires添加到header.I需要url在一段时间后过期。我无法理解,是因为到期日期和时间的格式。请帮助。

void Start()
{
    SendRequestToAmazonS3 (mFileName);
}


void SendRequestToAmazonS3(string inFileName)
{

    Dictionary<string,string> headers=new Dictionary<string, string>();
    string dateString =DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss ") + "GMT";
    headers.Add("x-amz-date", dateString);

    System.Int32 tempTimestamp = (System.Int32)(System.DateTime.UtcNow.AddMinutes(10).Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds; 
    string unixTimestamp = tempTimestamp.ToString();
    headers.Add ("x-amz-expires", unixTimestamp);  //works fine if I don't add this 

    string canonicalString = "GET\n\n\n\nx-amz-date:" + dateString + "\n/" + mBucketName + "/" + inFileName;
    // now encode the canonical string
    var utf8 = new System.Text.UTF8Encoding();
    // create a hashing object
    HMACSHA1 signature = new HMACSHA1();
    // secretId is the hash key
    signature.Key = utf8.GetBytes(mSecretKey);
    byte[] bytes  = utf8.GetBytes(canonicalString);
    byte[] hashBytes = signature.ComputeHash(bytes);
    // convert the hash byte array into a base64 encoding
    string encodedCanonical = System.Convert.ToBase64String(hashBytes);

    // finally, this is the Authorization header.
    headers.Add("Authorization", "AWS " + mAccessKey + ":" + encodedCanonical);

    // The URL, either PATH_HOSTED or VIRTUAL_HOSTED, plus the item path in the bucket 
    string url = mAwsS3Url + inFileName;
    Debug.Log (url);

    WWW www = new WWW(url, null, headers);

    // Send the request in this coroutine so as not to wait busily
    StartCoroutine(WaitForDownload(www));
}



IEnumerator WaitForDownload(WWW www)
{
    yield return www;

    // Check for errors
    if(www.error==null)
    {
        File.WriteAllBytes (Application.dataPath + "/assetsS3.zip", www.bytes);
        Debug.Log ("Transfer complete");
    }
    else
    {
        Debug.Log ("Error");
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了答案我自己.x-am-expires也必须采用与date相同的格式。 string expires = DateTime.Now.AddMinutes(10).ToString(&#34; ddd,dd MMM yyyy HH:mm:ss&#34;)+&#34; GMT&#34 ;;