Gets the web-accessible URL for the Amazon S3 object or generates a time-limited signed request for a private file.
Access
public
Parameters
Parameter |
Type |
Required |
Description |
---|---|---|---|
|
Required |
The name of the bucket to use. |
|
|
Required |
The file name for the Amazon S3 object. |
|
|
Optional |
Specifies that a presigned URL for this request should be returned. May be passed as a number of seconds since UNIX Epoch, or any string compatible with |
|
|
Optional |
An associative array of parameters that can have the following keys:
|
Returns
Type |
Description |
---|---|
The file URL, with authentication and/or torrent parameters if requested. |
Examples
Get the URL for a given object.
// Instantiate the class $s3 = new AmazonS3(); // Get the URL $url = $s3->get_object_url('my-bucket', 'video/sample_public.mp4'); // Success? var_dump($url);Result:
string(57) "http://my-bucket.s3.amazonaws.com/video/sample_public.mp4"
Get an object URL that will return requested response headers.
// Instantiate the class $s3 = new AmazonS3(); $bucket = 'my-bucket' . strtolower($s3->key); // Get the URL $url = $s3->get_object_url($bucket, 'mårkér wîth spåcés ånd întl/åéîøü.txt', '5 minutes', array( 'response' => array( 'content-type' => 'text/plain', 'content-language' => 'en-US', 'expires' => gmdate(DATE_RFC2822, strtotime('1 January 1980')) ) )); // Try to fetch the URL so we can get the status code $http = new CFRequest($url); $http->add_header('Content-Type', ''); # Simulate a web browser $http->send_request(true); $code = $http->get_response_code(); $headers = $http->get_response_header(); // Success? var_dump($url); var_dump($code); var_dump($headers['content-language']); var_dump($headers['content-type']); var_dump($headers['expires']);
AmazonS3 - Get Object URL with temporary credentials
// Dependencies require_once dirname(__FILE__) . '/../../sdk.class.php'; require_once dirname(__FILE__) . '/../testutil.inc.php'; // Instantiate STS and get temporary credentials $sts = new AmazonSTS(); $response = $sts->get_session_token(); if ($response->isOK()) { $credentials = array( 'key' => (string) $response->body->GetSessionTokenResult->Credentials->AccessKeyId, 'secret' => (string) $response->body->GetSessionTokenResult->Credentials->SecretAccessKey, 'token' => (string) $response->body->GetSessionTokenResult->Credentials->SessionToken, ); } else { die('Temporary credentials could not retrieved.'); } // Instantiate S3 $s3 = new AmazonS3($credentials); $source_bucket = 'my-bucket' . strtolower(CFCredentials::get()->key); $dest_bucket = 'my-bucket' . strtolower(CFCredentials::get()->key); // Pull over an object that we want to get the URL for. $copy = $s3->copy_object( array( // Source 'bucket' => $source_bucket, 'filename' => 'text/sample.txt' ), array( // Destination 'bucket' => $dest_bucket, 'filename' => 'text/sample_private.txt' ), array( // Optional parameters 'acl' => AmazonS3::ACL_PRIVATE // Let's explicitly set it to private. ) ); // Ensure that the copy was successful if ($copy->isOK()) { // Get the URL $url = $s3->get_object_url($dest_bucket, 'text/sample_private.txt', '5 minutes'); // Try to fetch the URL so we can get the status code $http = new RequestCore($url); $http->add_header('Content-Type', ''); # Simulate a web browser $http->send_request(true); $code = $http->get_response_code(); // Success? var_dump($url); var_dump($code); } else { die('S3 object for test was not copied.'); }
Related Methods
See Also
Source
Method defined in services/s3.class.php | Toggle source view (36 lines) | View on GitHub