Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 php api : How to get url after file upload?

I used aws sdk (https://github.com/aws/aws-sdk-php).

code

$result = $client->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $key,
        'Body'   => $file,
        'ACL'    => 'public-read',
));

It's work well but i have a question:

  1. How to get the url after file upload successful.

Thanks.

like image 619
leiyonglin Avatar asked Aug 27 '13 07:08

leiyonglin


2 Answers

It is returned in the response. See the API docs for putObject.

$result = $client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => $file,
    'ACL'    => 'public-read',
));

$url = $result['ObjectURL'];

You can also use the getObjectUrl() method to get the URL.

$url = $client->getObjectUrl($bucket, $key);
like image 85
Jeremy Lindblom Avatar answered Sep 19 '22 15:09

Jeremy Lindblom


The result returned is an instance of Guzzle\Service\Resource\Model.

To get the url just use the get method provided by that class.

$result = $client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => $file,
    'ACL'    => 'public-read',
));

$url = $result->get('ObjectURL');
like image 25
Ben Routson Avatar answered Sep 22 '22 15:09

Ben Routson