我正在嘗試將 .zip 檔案從 S3 讀取到 C# 中的流中,并將條目寫回 S3 中的原始檔案夾。我已經查看了無數的 SO 問題,觀看了視頻等,試圖做到這一點,但我似乎遺漏了一些東西。我現在比原來更遠了,但我仍然卡住了。(我真的希望亞馬遜只實作一個解壓縮方法,因為這似乎出現了很多,但還沒有這樣的運氣。)這是我目前的代碼:
private async Task<string> DecompressFile(string bucketName, string keystring)
{
AmazonS3Client client = new AmazonS3Client();
Stream fileStream = new MemoryStream();
string sourceDir = keystring.Split('/')[0];
GetObjectRequest request = new GetObjectRequest{ BucketName = bucketName, Key = keystring };
try
{
using (var response = await client.GetObjectAsync(request))
using (var arch = new ZipArchive(response.ResponseStream))
{
foreach (ZipArchiveEntry entry in arch.Entries)
{
fileStream = entry.Open();
string newFile = sourceDir "/" entry.FullName;
using (Amazon.S3.Transfer.TransferUtility tranute = new Amazon.S3.Transfer.TransferUtility(client))
{
var upld = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
upld.InputStream = fileStream;
upld.Key = newFile;
upld.BucketName = bucketName;
await tranute.UploadAsync(upld);
}
}
}
return $"Decompression complete for {keystring}...";
}
catch (Exception e)
{
ctxt.Logger.LogInformation($"Error decompressing file {keystring} from bucket {bucketName}. Please check the file and try again.");
ctxt.Logger.LogInformation(e.Message);
ctxt.Logger.LogInformation(e.StackTrace);
throw;
}
}
我現在一直遇到的錯誤是在await tranute.UploadAsync(upld). 我得到的錯誤是:
$exception {"This operation is not supported."} System.NotSupportedException
以下是例外詳細資訊:
System.NotSupportedException
HResult=0x80131515
Message=This operation is not supported.
Source=System.IO.Compression
StackTrace:
at System.IO.Compression.DeflateStream.get_Length()
at Amazon.S3.Transfer.TransferUtilityUploadRequest.get_ContentLength()
at Amazon.S3.Transfer.TransferUtility.IsMultipartUpload(TransferUtilityUploadRequest request)
at Amazon.S3.Transfer.TransferUtility.GetUploadCommand(TransferUtilityUploadRequest request, SemaphoreSlim asyncThrottler)
at Amazon.S3.Transfer.TransferUtility.UploadAsync(TransferUtilityUploadRequest request, CancellationToken cancellationToken)
at File_Ingestion.Function.<DecompressFile>d__13.MoveNext() in File-Ingestion\Function.cs:line 136
This exception was originally thrown at this call stack:
[External Code]
File_Ingestion.Function.DecompressFile(string, string) in Function.cs
任何幫助將不勝感激。謝謝!
uj5u.com熱心網友回復:
我認為問題在于AWS需要知道檔案的長度才能上傳,但回傳的流ZipArchiveEntry.Open不知道它的長度。
查看TransferUtilityUploadRequest.ContentLength嘗試呼叫時如何拋出例外DeflateStream.Length(總是拋出),DeflateStream最終回傳的東西在哪里ZipArchiveEntry.Open。
DeflateStream(不報告自己的解壓縮長度有點奇怪。它當然知道應該是什么,但這只是一個可能錯誤的指示,所以它可能想避免報告可能不正確的值。)
我認為您需要做的是將提取的檔案快取在記憶體中,然后再將其傳遞給 AWS。這樣,我們可以找出流的未壓縮長度,這將通過以下方式正確報告MemoryStream.Length:
using var fileStream = entry.Open();
// Copy the fileStream into an in-memory MemoryStream
using var ms = new MemoryStream();
fileStream.CopyTo(ms);
ms.Position = 0;
string newFile = sourceDir "/" entry.FullName;
using (Amazon.S3.Transfer.TransferUtility tranute = new Amazon.S3.Transfer.TransferUtility(client))
{
var upld = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
upld.InputStream = ms;
upld.Key = newFile;
upld.BucketName = bucketName;
await tranute.UploadAsync(upld);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447408.html
標籤:C# 亚马逊-s3 aws-lambda 压缩
