get_object_url ( $bucket, $filename, $preauth, $opt )

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

$bucket

string

Required

The name of the bucket to use.

$filename

string

Required

The file name for the Amazon S3 object.

$preauth

integer
string

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 strtotime().

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • https - boolean - Optional - Set to true if you would like the URL be in https mode. Otherwise, the default behavior is always to use http regardless of your SSL settings.
  • method - string - Optional - The HTTP method to use for the request. Defaults to a value of GET.
  • response - array - Optional - Allows adjustments to specific response headers. Pass an associative array where each key is one of the following: cache-control, content-disposition, content-encoding, content-language, content-type, expires. The expires value should use gmdate() and be formatted with the DATE_RFC2822 constant.
  • torrent - boolean - Optional - A value of true will return a URL to a torrent of the Amazon S3 object. A value of false will return a non-torrent URL. Defaults to false.
  • versionId - string - Optional - The version of the object. Version IDs are returned in the x-amz-version-id header of any previous object-related request.
  • returnCurlHandle - boolean - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.

Returns

Type

Description

string

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

See Also

Source

Method defined in services/s3.class.php | Toggle source view (36 lines) | View on GitHub

public function get_object_url($bucket, $filename, $preauth = 0, $opt = null)
{
    // Add this to our request
    if (!$opt) $opt = array();
    $opt['verb'] = isset($opt['method']) ? $opt['method'] : 'GET';
    $opt['resource'] = $filename;
    $opt['preauth'] = $preauth;

    if (isset($opt['torrent']) && $opt['torrent'])
    {
        $opt['sub_resource'] = 'torrent';
        unset($opt['torrent']);
    }

    // GET responses
    if (isset($opt['response']))
    {
        foreach ($opt['response'] as $key => $value)
        {
            $opt['response-' . $key] = $value;
            unset($opt['response'][$key]);
        }
    }

    // Determine whether or not to use SSL
    $use_ssl = isset($opt['https']) ? (bool) $opt['https'] : false;
    unset($opt['https']);
    $current_use_ssl_setting = $this->use_ssl;

    // Authenticate to S3
    $this->use_ssl = $use_ssl;
    $response = $this->authenticate($bucket, $opt);
    $this->use_ssl = $current_use_ssl_setting;

    return $response;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback