Amazon S3 REST API 403错误c#

我正在尝试使用REST API通过C#获取一些代码来从S3获取文件。 我见过其他人做类似的事情,但由于某种原因我不断收到403错误。 我已经尝试使用适用于.Net的AWS SDK做同样的事情并且它可以工作,所以我认为这是我创建授权标头的方式。

有人能够对此有所了解吗?

string awsAccessId = "***"; string awsSecretKey = "***"; string bucketName = "thebucket"; string httpDate = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss +0000\n"); string canonicalString = "GET\n" + "\n" + "\n" + "x-amz-date:" + httpDate + "\n" + "/" + bucketName + "/readme.txt"; // now encode the canonical string Encoding ae = new UTF8Encoding(); // create a hashing object HMACSHA1 signature = new HMACSHA1(); // secretId is the hash key signature.Key = ae.GetBytes(awsSecretKey); byte[] bytes = ae.GetBytes(canonicalString); byte[] moreBytes = signature.ComputeHash(bytes); // convert the hash byte array into a base64 encoding string encodedCanonical = Convert.ToBase64String(moreBytes); // Send the request HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://" + bucketName +".s3.amazonaws.com/readme.txt"); request.Headers.Add("x-amz-date", httpDate); request.Headers.Add("Authorization", "AWS " + awsAccessId + ":" + encodedCanonical); request.Method = "GET"; // Get the response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); Console.Read(); 

我不知道这是否是唯一的问题,但它看起来像一个明确的问题:

 + "x-amz-date:" + httpDate + "\n" 

x-amz-date是取代HTTP请求本身的Date:标头的标头,但是在要签名的字符串中,你只需要输入日期,而不是“x-amz-date:”或其前面的任何内容,示例:

 GET\n \n \n Tue, 27 Mar 2007 19:36:42 +0000\n /johnsmith/photos/puppy.jpg 

http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationRequestCanonicalization

只能为请求生成一个正确的签名。 S3将生成该签名,并将其与您发送的签名进行比较,因此在字符串到符号中没有单个字节的错误空间。

我测试了你的代码,它的工作原理! 你需要一个额外的\ n加上将http更改为https,你就完成了。

  string stringToConvert = "GET\n" + "\n" + "\n" + "\n" + "x-amz-date:" + timeStamp + "\n" + "/" + bucketName + "/" + FileName; 

Amazon Rest API没有很好的文档,缺少示例会让每个人都转到SDK。