[英]403 forbidden error - Accessing Amazon S3 bucket using Http GET Request in c#
我正在編寫一個c#代碼,試圖通過REST調用訪問Amazon S3存儲桶。 該代碼對在s3存儲桶中創建的xml文件進行get請求。
我正在使用秘密密鑰和訪問ID來創建將在授權標頭中使用的簽名。
我創建的簽名基於Amazon的文檔http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
我已經為經過身份驗證的請求提供了訪問s3存儲桶中xml文件的權限。
我正在使用的代碼
string AccessId = "xyz";
string SecretKey = "xyz";
string bucketName = "bucket";
string filename = "filename.xml";
string httpDate = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss +0000\n");
string StringtoSign = "GET\n"
+ "\n"
+ "\n"
+ httpDate + "\n"
+ "/bucketName/filename.xml";
//Creating Signature
Encoding e_UTF = new UTF8Encoding();
Encoding e_ASCI = new ASCIIEncoding();
byte[] key_new= e_ASCI.GetBytes(SecretKey);
byte[] message_new = e_UTF.GetBytes(StringtoSign);
HMACSHA1 myhmacsha1 = new HMACSHA1(key_new);
byte[] final=myhmacsha1.ComputeHash(message_new);
string AWSSignature = Convert.ToBase64String(final);
// Sending request
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://"+bucketname+".s3-us-west-2.amazonaws.com/"+filename);
request.Method = "GET";
request.Headers.Add("Authorization", "AWS"+ " " + AccessId + ":" + AWSSignature);
try
{
// Getting response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
String ResStr = sr.ReadToEnd();
Console.WriteLine(ResStr);
}
catch (WebException ex)
{
var Resp = (HttpWebResponse)ex.Response;
Stream new_str=Resp.GetResponseStream();
StreamReader stred = new StreamReader(new_str);
MessageBox.Show(stred.ReadToEnd().ToString());
}
如果將xml文件的權限設置為public,Same Code可以正常工作。 因此,它必須對簽名進行處理。 我不確定發生了什么事情。 如果有人可以看一下,那就太好了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.