leiyonglin
leiyonglin

Reputation: 6814

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.

Upvotes: 11

Views: 23544

Answers (3)

Ben Routson
Ben Routson

Reputation: 61

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

Upvotes: 6

singh1469
singh1469

Reputation: 2044

The object URL is available in the $result variable that is returned in your function call.

To access the object URL do this:

$result = $client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => $file,
    'ACL'    => 'public-read',
));
$data=$result->toArray();
$object_url=$data['ObjectURL'];

Upvotes: 2

Jeremy Lindblom
Jeremy Lindblom

Reputation: 6527

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

Upvotes: 25

Related Questions