Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a file with php and the Amazon S3 sdk?

Tags:

php

amazon-s3

I'm trying to make it so that my script will show test.jpg in an Amazon S3 bucket through php. Here's what I have so far:

require_once('library/AWS/sdk.class.php');

$s3 = new AmazonS3($key, $secret);

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg', array('headers' => array('content-disposition' => $objInfo->header['_info']['content_type'])));

echo $obj->body;

This just dumps out the file data on the page. I think I need to also send the content-disposition header, which I thought was being done in the get_object() method, but it isn't.

Note: I'm using the SDK available here: http://aws.amazon.com/sdkforphp/

like image 499
doremi Avatar asked Sep 12 '11 14:09

doremi


People also ask

How do I download data from aws s3?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

How can I read s3 file in PHP?

$key = 'name of the file you want to read'; $result = $s3->getObject([ 'Bucket' => 'name of the bucket', 'Key' => $key, 'Body' => 'this is the body! ', ]); // Print the body of the result by indexing into the result object. echo result['Body']; ?>

Does PHP work in s3?

S3 doesn't run any sort of CGI script (PHP, Perl, Ruby, etc). Think of it as a static html and image repository. If you want to host your PHP application on AWS, consider using AWS Beanstalk. It will launch an environment (server, IP, etc) where you can deploy and run your PHP application easily.


2 Answers

Both of these methods work for me. The first way seems more concise.

    $command = $s3->getCommand('GetObject', array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'  
       'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"'
    ));

    $signedUrl = $command->createPresignedUrl('+15 minutes');
    echo $signedUrl;
    header('Location: '.$signedUrl);
    die();

Or a more wordy but still functional way.

    $object = $s3->getObject(array(
    'Bucket' => 'bucket_name',
    'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object->body;
like image 148
Maximus Avatar answered Oct 10 '22 20:10

Maximus


Got it to work by echo'ing out the content-type header before echo'ing the $object body.

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg');

header('Content-type: ' . $objInfo->header['_info']['content_type']);
echo $obj->body;
like image 28
doremi Avatar answered Oct 10 '22 19:10

doremi