S3 无法解压缩上传的文件

S3 fails to unzip uploaded file

我正在关注 this example

// Load the stream
var fs = require('fs'), zlib = require('zlib');
var body = fs.createReadStream('bigfile').pipe(zlib.createGzip());

// Upload the stream
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body}, function(err, data) {
  if (err) console.log("An error occurred", err);
  console.log("Uploaded the file at", data.Location);
})

它 "works" 因为它所做的一切都完全符合预期,除了文件到达 S3 压缩并保持这种状态。

据我所知,它没有自动工具可以在 S3 上解压缩它,因此,如果您打算上传公开可用的图像或视频(或最终用户打算简单消费的任何其他内容) ) 解决方案似乎让上传的文件像这样解压缩...

// Load the stream
var fs = require('fs'), zlib = require('zlib');
var body = fs.createReadStream('bigfile');//.pipe(zlib.createGzip()); <-- removing the zipping part

// Upload the stream
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body}, function(err, data) {
  if (err) console.log("An error occurred", err);
  console.log("Uploaded the file at", data.Location);
})

我很好奇我是否做错了什么,是否有一种自动方法让 S3 识别文件已压缩并解压缩?

它的工作方式是 s3 现在无需任何帮助就可以知道文件已 gzip 压缩。上传时需要在文件上设置元数据,告诉 S3 它是 gzip 压缩的。它会做正确的事情,这是设置好的。

上传时需要在对象元数据中设置Content-Encoding: gzipContent-Type: <<your file type>>

稍后编辑: 找到了这些解释如何为 Cloudfront 做的,但基本相同:http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#CompressedS3
http://www.cameronstokes.com/2011/07/20/compressed-content-amazon-s3-and-cloudfront/

但是请注意,根据 this blogpost,S3 将提供 gzip 文件并依靠浏览器解压缩它。这在很多情况下都可以正常工作,但正如博主所说,在 curl 中会失败(因为 curl 不知道如何处理 gzip 文件)。因此,如果您的目的只是简单地上传文件以供用户原始使用,那么最好的办法是跳过 gzip 压缩并以未压缩状态上传文件。