![](/img/trans.png)
[英]In a C# .NET web application, can I download an Amazon S3 object directly into memory and serve it as an image?
[英]Amazon S3 - Get Object .NET C# Error
我正在開發某些.NET C#實現,並且遇到了問題。
我一直在使用類似於以下代碼的一組代碼:
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);
}
但是,文件從服務器成功下載后,它將捕獲從DownloadAsync函數引發的TypeLoadException並指出以下內容:
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'.
我什至沒有使用AWSSDK.Core版本3.3.0.0; 我使用的是與NuGet的AWSSDK.S3版本3.3.5.15捆綁在一起的版本3.3.13.2。
我不確定該如何進行處理,因為無論使用什么版本的AWS都將引發此錯誤,並且S3上的內容是否經過加密都無關緊要。
想通了。 如果捕獲到AmazonServiceException,則文件將正確保存。 顯然,應用程序找不到DecryptStream類型並不重要。
只需更改:
} catch (TypeLoadException e) {
至
} catch (AmazonServiceException e) {
為了正確捕獲異常並繼續執行其余代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.