1
votes

I have next error:

"The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256."

When I try download a file from my bucket on Amazon S3. My code is the next:

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:

this generete the next error: The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.

I was reasearch it on google and on some blogs. some suggest using the property "signature version" to v4. something like...

config.signatureVersion = "v4";

but my config object, not have this property.

any suggestion?

thank you!!!

1
I suggest you to update you AWS SDK. You can get it using Nuget in VS.Robert

1 Answers

1
votes

Try This Code

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();