简体   繁体   中英

Amazon S3 - Get Object .NET C# Error

I am working on some .NET C# implementation and am running into issues.

I've been working with a set of code that looks similar to the following:

IAmazonS3 client = new AmazonS3Client("id", "key", "region");
TransferUtility transfer = new TransferUtility(client);

TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest();
request.BucketName = "bucketName";
request.Key = "keyName";
request.FilePath = destinationPath;

try {
    await transfer.DownloadAsync(request);
} catch (AmazonS3Exception amazonS3Exception) {
    if (amazonS3Exception.ErrorCode != null &&
        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
        ||
        amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
    {
        throw new Exception("Check the provided AWS Credentials.");
    } else  {
        throw new Exception("Error occurred: " + amazonS3Exception.Message);
    }
} catch (TypeLoadException e)  {
    Debug.WriteLine("caught some weird bug: " + e.StackTrace);
}

However after the file successfully downloads from the server it catches the TypeLoadException thrown from the DownloadAsync function and states the following:

Amazon.Runtime.AmazonServiceException occurred
  HResult=0x80131500
  Message=Could not load type 'Amazon.Runtime.Internal.Util.DecryptStream' from assembly 'AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604'.
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at Amazon.S3.Transfer.Internal.DownloadCommand.<ExecuteAsync>d__11.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MyApp.StartPage.<download>d__20.MoveNext() in C:\...\StartPage.xaml.cs:line 295

Inner Exception 1:
TypeLoadException: Could not load type 'Amazon.Runtime.Internal.Util.DecryptStream' from assembly 'AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604'.

I'm not even using AWSSDK.Core version 3.3.0.0; I am using version 3.3.13.2 which is bundled in with AWSSDK.S3 version 3.3.5.15 from NuGet.

I'm not sure how to proceed since this error is thrown no matter what version of AWS I use and it doesn't matter if the content on the S3 is encrypted or not.

Figured this out. If the AmazonServiceException is caught, the file will save correctly. Apparently it does not matter that the app can't find the DecryptStream type.

Just change the:

} catch (TypeLoadException e)  {

to

} catch (AmazonServiceException e)  {

in order to properly catch the exception and continue with the rest of the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM