Pankaj
Pankaj

Reputation: 370

PHP - file encoding issue with files downloaded from AWS bucket

Using following code in PHP, I am trying to download file from AWS bucket. I am able to download file successfully, but the downloaded file is not readable. File encoding is set to ANSI.

In AWS bucket the meta data for this file is as follows Content-Type: text/csv;%20charset=utf-16le Content-Encoding: gzip



    require '/aws/aws-autoloader.php'; 
    use Aws\S3\S3Client;

    // Instantiate the S3 client with your AWS credentials
    $key = [access_key];
    $secret = [secret_key];

    $client = S3Client::factory(array(
        'credentials' => array(
            'key'    => $key,
            'secret' => $secret
        )
    ));


    $bucket = [bucket_name];
    $file=[bucket_file_location];
    $fileSaveAs = [download_file_location];

    // Get an object using the getObject operation & download file
    $result = $client->getObject(array(
        'Bucket' => $bucket,
        'Key'    => $file,
        'SaveAs' => $fileSaveAs
    ));

Can anyone explain, what is wrong here ?

Edit 1: This file downloads nicely when I download it from AWS bucket directly.

Edit 2 : I just noticed that downloaded CSV File is always of 1KB size.

Downloaded file damage pattern: `‹½”ÍJÃ@F¿µOÑPi+h]V[Tð§hâFÚ4HÐjI¬Ð—W¿¤

Edit 3 : All these files are transferred from Google play bucket using gsutil

Upvotes: 2

Views: 701

Answers (1)

Pankaj
Pankaj

Reputation: 370

Files received from AWS bucket are gzip. (Content-Encoding: gzip)

So need to decode gzip compressed string using gzdecode function

The following code solves problem



    $content = gzdecode(file_get_contents($fileSaveAs));
    file_put_contents($fileSaveAs,$content);

Upvotes: 1

Related Questions