using System;
using System.Net;
using System.Text;
using System.Security.Cryptography;
namespace SampleProject
{
class MainClass
{
public static void Main(string[] args)
{
string awsAccessId = "myaccessid";
string awsSecretKey = "mysecretkey";
string httpDate = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss +0000\n");
string canonicalString = "GET"
+"\n"
+ "\n"
+ "\n"
+ httpDate+"\n"
+ "/mybucket/sample.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);
Console.WriteLine(bytes);
byte[] moreBytes = signature.ComputeHash(bytes);
Console.WriteLine(moreBytes);
// convert the hash byte array into a base64 encoding
string encodedCanonical = Convert.ToBase64String(moreBytes);
// Send the request
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://mybucket.s3-ap-northeast-1.amazonaws.com/sample.txt");
request.Headers.Add("Date", httpDate);
request.Headers.Add("Authorization", "AWS " + awsAccessId + ":" + encodedCanonical);
request.Method = "GET";
// Get the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Response value");
Console.WriteLine(response.StatusCode);
Console.Read();
}
}
}
Необработанное исключение: System. Net .WebException: удаленный сервер возвратил ошибку: (403) Запрещено. в System. Net .HttpWebRequest.GetResponseFromData (поток System. Net .WebResponseStream, System.Threading.CancellationToken cancellationToken) [0x00146] в / Users / builder / jenkins / workspace / build-package-osx-mono / 12 / внешний / bockbuild / builds / моно-x64 / mcs / класс / System / System.Net / HttpWebRequest.cs: 1101