I am writing an c# code that is trying to access the Amazon S3 bucket through REST calls. The code makes a get request to an xml file created in the s3 bucket.
I am using the secret key and access Id to create a signature that will be used in the authorization header.
The Signature I created is based on Amazon's documentation, http://ift.tt/1hobFjy
I have provided permissions for authenticated requests to access the xml file in the s3 bucket.
The Code I am using,
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
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());
}
The Same Code works fine if I set the permissions for the xml file as public. So it has got to do something with the signature. I am not sure what am doing wrong. It would be great if someone can have a look at it.
Aucun commentaire:
Enregistrer un commentaire