change_content_type ( $bucket, $filename, $contentType, $opt )

Changes the content type for an existing Amazon S3 object.

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 object.

$contentType

string

Required

The content-type to apply to the object.

$opt

array

Optional

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

  • 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

Change the content type of an object.

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

$cct = $s3->change_content_type($bucket, 'test1.txt', 'text/plain');
sleep(5);
$metadata = $s3->get_object_metadata($bucket, 'test1.txt');

// Success?
var_dump($cct->isOK());
var_dump($metadata['ContentType']);
Result:
bool(true)
string(10) "text/plain"

Source

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

public function change_content_type($bucket, $filename, $contentType, $opt = null)
{
    if (!$opt) $opt = array();

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

    // Merge optional parameters
    $opt = array_merge_recursive(array(
        'headers' => array(
            'Content-Type' => $contentType
        ),
        'metadataDirective' => 'REPLACE'
    ), $opt);

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

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback