简体   繁体   English

如何将gzip对象上传到s3?

[英]How do I upload a gzip object to s3?

I am creating a gzip string and uploading it as an object to s3. 我正在创建一个gzip字符串并将其作为对象上传到s3。 However when I download the same file from s3 and decompress it locally with gunzip I get this error: gunzip: 111.gz: not in gzip format When I look at the mime_content_type returned in the file downloaded from s3 it is set as: application/zlib 但是,当我从s3下载相同的文件并使用gunzip在本地解压缩时,我收到此错误: gunzip: 111.gz: not in gzip format当我查看从s3下载的文件中返回的mime_content_type时,它设置为: application/zlib

Here is the code I am running to generate the gzip file and push it to s3: 这是我运行的代码,用于生成gzip文件并将其推送到s3:

for($i=0;$i<=100;$i++) {
    $content .= $i . "\n";
}

$result = $this->s3->putObject(array(
   'Bucket' => 'my-bucket-name',
   'Key'    => '111.gz',
   'Body'   => gzcompress($content),
   'ACL' => 'authenticated-read',
   'Metadata' => [
       'ContentType' => 'text/plain',
       'ContentEncoding' => 'gzip'
   ]
));

The strange thing is that if I view the gzip content locally before I send it to s3 I am able to decompress it and see the original string. 奇怪的是,如果我在将其发送到s3之前在本地查看gzip内容,我可以将其解压缩并查看原始字符串。 So I must be uploading the file incorrectly, any thoughts? 所以我必须正确上传文件,有什么想法吗?

According to http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject the ContentType and ContentEncoding parameters belong on top level, and not under Metadata. 根据http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject,ContentType和ContentEncoding参数属于顶级,而不是元数据。 So your call should look like: 所以你的电话应该是这样的:

$result = $this->s3->putObject(array(
   'Bucket' => 'my-bucket-name',
   'Key'    => '111.gz',
   'Body'   => gzcompress($content),
   'ACL' => 'authenticated-read',
   'ContentType' => 'text/plain',
   'ContentEncoding' => 'gzip'
));

Also it's possible that by setting ContentType to text/plain your file might be truncated whenever a null-byte occurs. 此外,通过将ContentType设置为text / plain,可能会在发生空字节时截断文件。 I would try with'application/gzip' if you still have problems unzipping the file. 如果解压缩文件仍有问题,我会尝试使用'application / gzip'。

I had a very similar issue, and the only way to make it work for our file was with a code like this (slightly changed according to your example): 我有一个非常类似的问题,使它适用于我们的文件的唯一方法是使用这样的代码(根据你的例子略有改变):

$this->s3->putObject(array(
    'Bucket' => 'my-bucket-name',
    'Key'    => '111.gz',
    'Body'   => gzcompress($content, 9, ZLIB_ENCODING_GZIP),
    'ACL' => 'public-read',
    'ContentType' => 'text/javascript',
    'ContentEncoding' => 'gzip'
));

The relevant part being gzcompress($content, 9, ZLIB_ENCODING_GZIP) , as AWS S3 wouldn't recognize the file nor serve it in the right format without the last ZLIB_ENCODING_GZIP parameter. 相关部分是gzcompress($content, 9, ZLIB_ENCODING_GZIP) ,因为AWS S3在没有最后一个ZLIB_ENCODING_GZIP参数的情况下无法识别文件或以正确的格式提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM