因为业务,需要将两张MongoDB 表的数据切换到 AWS 的S3中,再切换中发现下面的代码;先删除整个TempFiles 目录,将流写到特定文件中,然后再读出来; 通过尝试修改,认为前人是为了解决AWS 获取的流是不能直接拿来,才这么处理的。但是这样处理会有并发问题。
var request = new GetObjectRequest
{
BucketName = bucket.StartsWith(BucketPrefix) ? bucket : BucketPrefix + bucket,
Key = key
};
var tempDirectory = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/TempFiles/";
if (Directory.Exists(tempDirectory))
{
var tempFiles = Directory.GetFiles(tempDirectory);
foreach (var tempFile in tempFiles)
{
try
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
catch (Exception ex)
{
Logger.Debug($"Can't delete temp file {tempFile}. Eception={ex.Message}");
}
}
}
using (GetObjectResponse response = await Client.GetObjectAsync(request).ConfigureAwait(false))
{
// the response stream from amazon is encrypt, so wo have to write stream to a temp file.
var tempFilePath = Path.Combine(tempDirectory + Guid.NewGuid());
response.WriteResponseStreamToFileAsync(tempFilePath, append: false, CancellationToken.None).GetAwaiter().GetResult();
var fileName = response.Metadata["x-amz-meta-title"];
return new ReadFileAsyncResult
{
FileStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read),
FileName = fileName
};
}
通过求教 获取了一个新的方法
var request = new GetObjectRequest { BucketName = bucket.StartsWith(BucketPrefix) ? bucket : BucketPrefix + bucket, Key = key }; using (GetObjectResponse response = await Client.GetObjectAsync(request).ConfigureAwait(false)) { var fileName = response.Metadata["x-amz-meta-title"]; var memoryStream = new MemoryStream(); response.ResponseStream.CopyTo(memoryStream); memoryStream.Position = 0; return new ReadFileAsyncResult { FileStream = memoryStream, FileName = fileName };
}