更新时间:2022-11-06 20:35:33
我测试了code,它的作品!你只需要一个额外的\ n与变化HTTP到HTTPS,就大功告成了。
字符串stringToConvert =GET \ N
+\ N
+\ N
+\ N
+X-AMZ-日期:+的timeStamp +\ N
+/+ bucketName +/+文件名;
亚马逊的REST API不具备良好的文档,缺少的例子让大家去的SDK代替。
I'm trying to get some code working to fetch a file from S3 using the REST API via C#. I've seen other people doing similar things but for some reason I keep getting a 403 error. I've tried to do the same thing with the AWS SDK for .Net and it works so I assume it's the way I'm creating the authorization header.
Is anyone able to shed any light on this please?
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();
I tested your code, it works! you just need an extra \n plus change http to https and you're done.
string stringToConvert = "GET\n"
+ "\n"
+ "\n"
+ "\n"
+ "x-amz-date:" + timeStamp + "\n"
+ "/" + bucketName + "/" + FileName;
Amazon Rest API don't have a good documentation, the lack of examples makes everyone go to the SDK instead.