2

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

2
  • Probably content you get is gziped (As said in Content-Encoding)? Does gzdecode(file_get_contents($fileSaveAs)) return right content? Commented May 31, 2016 at 9:30
  • Yes this solved my problem Commented May 31, 2016 at 9:40

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

@Zudwa: Thanks for the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.