在 C# 中从 S3 读取 .xz 文件
Read .xz file from S3 in C#
我正在尝试使用以下代码从 S3 读取文件:
var dir = new S3DirectoryInfo(_amazonS3Client, bucketName, folderName);
IS3FileSystemInfo[] files = dir.GetFileSystemInfos();
if(files.Length > 0)
{
_bucketKey = files[0].Name;
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = $"{folderName}/{_bucketKey}"
};
using (GetObjectResponse response = await _amazonS3Client.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (var reader = new StreamReader(responseStream))
{
var responseBody = reader.ReadToEnd();
File.WriteAllText(newFilePath, responseBody);
}
}
对于非压缩文件,它工作正常,但是,我需要一些关于如何读取 .xz 文件的建议?我的文件就像 DummyData_2020-07-21.csv.xz
我找到了一个使用 SharpCompress
的解决方案,下面的代码对我有用
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = $"{folderName}/{bucketKey}"
};
using (var obj = _amazonS3Client.GetObject(request))
{
obj.WriteResponseStreamToFile($"{fileLocation}{bucketKey}");
}
using (Stream xz = new XZStream(File.OpenRead($"{fileLocation}{bucketKey}")))
using (TextReader reader = new StreamReader(xz))
{
var responseBody = reader.ReadToEnd();
File.WriteAllText(newFilePath, responseBody);
}
我正在尝试使用以下代码从 S3 读取文件:
var dir = new S3DirectoryInfo(_amazonS3Client, bucketName, folderName);
IS3FileSystemInfo[] files = dir.GetFileSystemInfos();
if(files.Length > 0)
{
_bucketKey = files[0].Name;
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = $"{folderName}/{_bucketKey}"
};
using (GetObjectResponse response = await _amazonS3Client.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (var reader = new StreamReader(responseStream))
{
var responseBody = reader.ReadToEnd();
File.WriteAllText(newFilePath, responseBody);
}
}
对于非压缩文件,它工作正常,但是,我需要一些关于如何读取 .xz 文件的建议?我的文件就像 DummyData_2020-07-21.csv.xz
我找到了一个使用 SharpCompress
的解决方案,下面的代码对我有用
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = $"{folderName}/{bucketKey}"
};
using (var obj = _amazonS3Client.GetObject(request))
{
obj.WriteResponseStreamToFile($"{fileLocation}{bucketKey}");
}
using (Stream xz = new XZStream(File.OpenRead($"{fileLocation}{bucketKey}")))
using (TextReader reader = new StreamReader(xz))
{
var responseBody = reader.ReadToEnd();
File.WriteAllText(newFilePath, responseBody);
}