任务:使用 .NET Core 中的 AWSSDK.S3 包将已压缩的文件上传到 Amazon AWS S3 存储桶,并将 Content-Encoding 标头设置为“gzip”。
我有一个要上传到的 S3 存储桶。我正在为数据使用 IO 流,但在上传过程中无法设置项目的 Content-Encoding。这是我想要设置的标头,当 S3 服务提供该项目时(它不是上传本身的 Content-Encoding)。
我正在使用 TransferUtility 和 TransferUtilityUploadRequest 对象。虽然您可以很好地设置 Content-Type(见下文),但没有设置 Content-Encoding 的属性。如果使用 Metadata.Add 功能,它会自动将 Content-Encoding 重写为自定义的“x-amz-meta-content-encoding”键,这当然没用。
据我了解,事后您无法在 S3 中设置元数据,除非通过复制对象或自己手动进行(这对我来说是不行的,文件太多)。通过复制对象,我不确定我是否能够设置元数据。
var s3Client = new AmazonS3Client(awsCredentials, bucketRegion);
var fileTransferUtility = new TransferUtility(s3Client);
byte[] compressedDataArray = <some compressed data>;
using (MemoryStream memoryStream = new MemoryStream(compressedDataArray)) {
var fileTransferUtilityRequest = new TransferUtilityUploadRequest {
BucketName = bucketName,
ContentType = "application/json",
StorageClass = S3StorageClass.Standard,
Key = fileKey + ".gz",
CannedACL = S3CannedACL.PublicRead,
InputStream = memoryStream,
AutoCloseStream = true
};
fileTransferUtilityRequest.Metadata.Add("Content-Encoding", "gzip");
fileTransferUtility.Upload(fileTransferUtilityRequest);
}
它将上传到 S3 中的新文件,但不会设置 Content-Encoding 标头。
请帮助,任何人!谢谢!