How do I upload a gzip object to s3?

Viewed 10493

I am creating a gzip string and uploading it as an object to 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

Here is the code I am running to generate the gzip file and push it to 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. So I must be uploading the file incorrectly, any thoughts?

2 Answers

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.

Related