update_object ( $bucket, $filename, $opt )

Updates an Amazon S3 object with new headers or other metadata. To replace the content of the specified Amazon S3 object, call create_object() with the same bucket and file name parameters.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket that contains the source file.

$filename

string

Required

The source file name that you want to update.

$opt

array

Optional

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

  • acl - string - Optional - The ACL settings for the specified object. [Allowed values: AmazonS3::ACL_PRIVATE, AmazonS3::ACL_PUBLIC, AmazonS3::ACL_OPEN, AmazonS3::ACL_AUTH_READ, AmazonS3::ACL_OWNER_READ, AmazonS3::ACL_OWNER_FULL_CONTROL]. The default value is ACL_PRIVATE.
  • headers - array - Optional - Standard HTTP headers to send along in the request. Accepts an associative array of key-value pairs.
  • meta - array - Optional - An associative array of key-value pairs. Any header with the x-amz-meta- prefix is considered user metadata and is stored with the Amazon S3 object. It will be stored with the object and returned when you retrieve the object. The total size of the HTTP request, not including the body, must be less than 4 KB.
  • curlopts - array - Optional - A set of values to pass directly into curl_setopt(), where the key is a pre-defined CURLOPT_* constant.
  • 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

CFResponse

A CFResponse object containing a parsed HTTP response.

Examples

Update an object.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$headers = $s3->get_object_headers($bucket, 'test1.txt');
var_dump($headers->header['content-type']);

$response = $s3->update_object($bucket, 'test1.txt', array(
	'acl' => AmazonS3::ACL_PUBLIC,
	'headers' => array(
		'Content-Encoding' => 'UTF-8'
	),
	'meta' => array(
		'Cache-Control' => 'Public',
		'Max-Age' => '604800',
	),
));
var_dump($response->isOK());

sleep(2);
$headers = $s3->get_object_headers($bucket, 'test1.txt');
var_dump($headers->header['content-type']);
Result:
string(10) "text/plain"
bool(true)
string(10) "text/plain"

See Also

Source

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

public function update_object($bucket, $filename, $opt = null)
{
    if (!$opt) $opt = array();
    $opt['metadataDirective'] = 'REPLACE';

    // Retrieve the original metadata
    if ($metadata = $this->get_object_metadata($bucket, $filename))
    {
        if (isset($metadata['ACL']))
        {
            $opt['acl'] = isset($opt['acl']) ? $opt['acl'] : $metadata['ACL'];
        }
        if (isset($metadata['StorageClass']))
        {
            $opt['headers']['x-amz-storage-class'] = $metadata['StorageClass'];
        }
        if (isset($metadata['ContentType']))
        {
            $opt['headers']['Content-Type'] = $metadata['ContentType'];
        }
    }

    // Remove a header
    unset($metadata['Headers']['date']);

    // Merge headers
    $opt['headers'] = array_merge($opt['headers'], $metadata['Headers']);

    // Authenticate to S3
    return $this->copy_object(
        array('bucket' => $bucket, 'filename' => $filename),
        array('bucket' => $bucket, 'filename' => $filename),
        $opt
    );
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback