Unity3d 将x-amz-expires添加到查询身份验证标头时出现错误(AccessDenied)

Unity3d 将x-amz-expires添加到查询身份验证标头时出现错误(AccessDenied),unity3d,amazon-s3,Unity3d,Amazon S3,我正在使用Unity的WWW类从amazon服务器下载资产。在我将x-amz-expires添加到标题之前,它工作正常。我需要url在一段时间后过期。我无法理解,是因为过期日期和时间的格式。请帮助 void Start() { SendRequestToAmazonS3 (mFileName); } void SendRequestToAmazonS3(string inFileName) { Dictionary<string,string> headers=

我正在使用Unity的WWW类从amazon服务器下载资产。在我将x-amz-expires添加到标题之前,它工作正常。我需要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");
    }
}
void Start()
{
SendRequestToAmazonS3(mFileName);
}
void SendRequestToAmazonS3(字符串填充名)
{
字典头=新字典();
字符串dateString=DateTime.UtcNow.ToString(“ddd,dd-MMM-yyyy-HH:mm:ss”)+“GMT”;
headers.Add(“x-amz-date”,dateString);
System.Int32 TENTIMESTAMP=(System.Int32)(System.DateTime.UtcNow.ADDMINES(10)。减去(新System.DateTime(1970,1,1))。总秒数;
字符串unixTimestamp=testimestamp.ToString();
headers.Add(“x-amz-expires”,unixTimestamp);//如果不添加这个,效果很好
string canonicalString=“GET\n\n\nx amz date:“+dateString+”\n/“+mBucketName+”/“+inFileName;
//现在对规范字符串进行编码
var utf8=new System.Text.UTF8Encoding();
//创建散列对象
HMACSHA1签名=新的HMACSHA1();
//secretId是散列键
signature.Key=utf8.GetBytes(mSecretKey);
byte[]bytes=utf8.GetBytes(canonicalString);
byte[]hashBytes=signature.ComputeHash(字节);
//将哈希字节数组转换为base64编码
string EncodedCanonic=System.Convert.ToBase64String(hashBytes);
//最后,这是授权头。
添加(“授权”、“AWS”+MacAccessKey+”:“+encodedCanonical”);
//URL,路径托管或虚拟托管,加上bucket中的项目路径
字符串url=mAwsS3Url+inFileName;
Debug.Log(url);
WWW=新的WWW(url,空,标题);
//在此协同程序中发送请求,以避免繁忙等待
启动例行程序(WaitForDownload(www));
}
IEnumerator WaitForDownload(WWW)
{
收益率;
//检查错误
如果(www.error==null)
{
File.writealBytes(Application.dataPath+“/assetsS3.zip”,www.bytes);
调试日志(“传输完成”);
}
其他的
{
Debug.Log(“错误”);
}
}

我自己找到了答案。x-am-expires的格式也必须与date相同。
字符串expires=DateTime.Now.AddMinutes(10).ToString(“ddd,dd-MMM-yyy-HH:mm:ss”)+“GMT”

好。您是如何发现时间是GMT而不是UTC的?实际上,整个签名和身份验证过程都在链接中给出。在这里,我发现时间是UTC。我按照文档编写了代码…:)很好。当您使用
WWW
制作完API后,我建议您将其移植到
UnityWebRequest
这是与服务器通信的最新方式。它解决了
WWW
所面临的许多问题。很可能
WWW
会被弃用。现在不要担心这一点,只要您阅读下面的链接,就可以轻松完成移植。非常有用的文档..感谢您提供的信息...)