如何在AmazonS3Config上使用signatureVersion?

时间:2017-11-30 20:52:55

标签: c# .net amazon-s3 aws-sdk

我有下一个错误:

  

"不支持您提供的授权机制。请使用AWS4-HMAC-SHA256。"

当我尝试在Amazon S3上从我的存储桶下载文件时。我的代码是下一个:

AmazonS3Config config = new AmazonS3Config();
config.CommunicationProtocol = Protocol.HTTP;
config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;
AmazonS3Client s3Client = new AmazonS3Client("MyAccesKeyAWS", "MyAccesSecretAWS", config);

TransferUtility transfer = new TransferUtility(s3Client);
TransferUtilityDownloadRequest downloader = new TransferUtilityDownloadRequest();
downloader.BucketName = "bucketName"; 
downloader.FilePath = "MyPath\\To\\Local\\File\\"; 
downloader.Key = "NameFile.pdf";

transfer.Download(downloader); //<-- here the ERROR:

这会产生下一个错误:不支持您提供的授权机制。请使用AWS4-HMAC-SHA256。

我在谷歌和一些博客上重新研究它。 一些人建议使用该属性&#34;签名版本&#34;到v4。 类似......

config.signatureVersion = "v4";

但是我的配置对象,没有这个属性。

有什么建议吗?

谢谢!!!

1 个答案:

答案 0 :(得分:2)

尝试使用此代码

AmazonS3Config config = new AmazonS3Config();

string accessKey = WebConfigurationManager.AppSettings["AWSaccessKey"].ToString();
string secretKey = WebConfigurationManager.AppSettings["AWSsecretKey"].ToString();
config.ServiceURL = WebConfigurationManager.AppSettings["AWSServiceURL"].ToString();
string storageContainer = WebConfigurationManager.AppSettings["AWSBucketName"].ToString();
AmazonS3Client client2 = new AmazonS3Client(
    accessKey,
    secretKey,
    config
);

Amazon.S3.AmazonS3 client3 = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, config);
GetObjectRequest request1 = new GetObjectRequest();
request1.BucketName = storageContainer;
request1.WithBucketName(storageContainer);
request1.WithKey(originalfileName);

GetObjectResponse response1 = client3.GetObject(request1);
using (Stream responseStream = response1.ResponseStream)
{
    var bytes = ReadStream(responseStream);
    var download = new FileContentResult(bytes, "application/pdf");
    download.FileDownloadName = response1.Key;
    int c = filePath.Split('/').Length;
    byte[] fileBytes = download.FileContents;
    //return download;
    var fileEntry = new ZipEntry(filePath.Split('/')[c - 1].ToString());
    zipStream.PutNextEntry(fileEntry);
    zipStream.Write(fileBytes, 0, fileBytes.Length);
}

 zipStream.Flush();
 zipStream.Close();