Unpossible
Unpossible

Reputation: 10697

AWS S3 - Setting Metadata with the PHP SDK2

I am attempting to change the metadata of all of the objects in a particular bucket on S3 using the AWS PHP SDK2. I've had trouble finding a specific example using the new SDK, but have pieced together the following:

$OBJ_aws_s3 = S3Client::factory($config);

$objects = $OBJ_aws_s3->getIterator('ListObjects', array(
    'Bucket' => $bucket,
    'MaxKeys' => 10
));

foreach($objects as $object) {
    $key = $object['Key'];

    echo "Processing " . $key . "\n";

    $response = $OBJ_aws_s3->copyObject(array(
        'Bucket' => $bucket, 
        'Key' => $key,
        'CopySource' => $key,
        'Metadata' => array(
            'Cache-Control' => 'max-age=94608000',
            'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+3 years'))
        ),
        'MetadataDirective' => 'REPLACE',
    ));
}

The foreach loop successfully loops through the first 10 items in the given $bucket, but I get a 403 error on the copyObject() operation:

Uncaught Aws\S3\Exception\AccessDeniedException: AWS Error Code: AccessDenied, Status Code: 403

I am not sure if this is due to incorrect values being passed in to copyObject, or some setting in S3. Note that I have yet to create a rights-restricted account in IAM and am using the base account that should have all rights on the objects.

Any help appreciated.

Upvotes: 6

Views: 6669

Answers (2)

Hubert
Hubert

Reputation: 176

Extended the original answer from Paul Mennega to preserve default ContentTypes when updating files within an existing bucket using the 'MetadataDirective' => 'REPLACE' instead of COPY.

`

//
// Utility to add cache-control and expires headers to existing files in an S3 Bucket
// Defaults to 1 Year which is the RFC spec max.
//


// Requirements:
// AWS PHP SDK
// http://aws.amazon.com/sdkforphp/


// Set your TIMEZONE
// http://www.php.net//manual/en/timezones.php
date_default_timezone_set("Australia/Adelaide");



// CONFIG START

$bucket = "YOUR-BUCKET-NAME-HERE";
$aws_key = "YOUR-AWS-KEY";
$aws_secret = "YOUR-AWS-SECRET";

// CONFIG END



require 'vendor/autoload.php';

use Aws\S3\S3Client;

$filetype = "";
$count = 0;

$OBJ_aws_s3 = S3Client::factory(array(
    'key' => $aws_key,
    'secret' => $aws_secret
));

$objects = $OBJ_aws_s3->getIterator('ListObjects', array(
    'Bucket' => $bucket,
    'MaxKeys' => 10
));

foreach($objects as $object) {
    $key = $object['Key'];

    echo "Processing " . $key . "\n";

    $file_parts = pathinfo($key);

    switch($file_parts['extension'])
    {
        case "jpg":
        echo "ContentType set to: image/jpeg" . "\n\n";
        $filetype = "image/jpeg";
        break;

        case "jpeg":
        echo "ContentType set to: image/jpeg" . "\n\n";
        $filetype = "image/jpeg";
        break;

        case "png":
        echo "ContentType set to: image/png" . "\n\n";
        $filetype = "image/png";
        break;

        case "gif":
        echo "ContentType set to: image/gif" . "\n\n";
        $filetype = "image/gif";
        break;

        case "tif":
        echo "ContentType set to: image/tiff" . "\n\n";
        $filetype = "image/tiff";
        break;

        case "tiff":
        echo "ContentType set to: image/tiff" . "\n\n";
        $filetype = "image/tiff";
        break;

        case "bmp":
        echo "ContentType set to: image/bmp" . "\n\n";
        $filetype = "image/bmp";
        break;

        case "zip":
        echo "ContentType set to: application/zip" . "\n\n";
        $filetype = "application/zip";
        break;

        case "pdf":
        echo "ContentType set to: application/pdf" . "\n\n";
        $filetype = "application/pdf";
        break;

        case "": // Handle file extension for files ending in '.'
        echo "Error: Unknown ContentType" . "\n\n";
        $filetype = "";
        break;
        case NULL: // Handle no file extension
        echo "Error: Unknown ContentType" . "\n\n";
        $filetype = "";
        break;
    }

    // Set EXPIRES and CACHE-CONTROL headers to +1 year (RFC guidelines max.)

    $response = $OBJ_aws_s3->copyObject(array(
        'Bucket' => $bucket, 
        'Key' => $key,
        'CopySource' => urlencode($bucket . '/' . $key),
        'MetadataDirective' => 'REPLACE',
        'CacheControl' => 'max-age=31536000',
        'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+1 years')),
        'ContentType' => $filetype,
    ));
    $count++;
}

echo "DONE! processed ". $count ." files.\n";

`

Upvotes: 1

Unpossible
Unpossible

Reputation: 10697

Ok, figured this out - my syntax was incorrect in two ways.

First, I was using the incorrect value for CopySource. From the documentation:

CopySource - (string) - The name of the source bucket and key name of the source object, separated by a slash (/). Must be URL-encoded.

So in my case, instead of using just 'CopySource' => $key,, it should be 'CopySource' => urlencode($bucket . '/' . $key),. This explains the 403 errors, as I was essentially telling the API that my source file was in a {bucket} / {key} of just {key}.

The second issue relates to the specific headers - specifying the Expires and Cache-Control headers in the Metadata field results in the creation of Amazon-specific meta values, with keys prefixed with x-amz-meta-. Instead I am now using the Expires and CacheControl arguments. My final working code:

$OBJ_aws_s3 = S3Client::factory($config);

$objects = $OBJ_aws_s3->getIterator('ListObjects', array(
    'Bucket' => $bucket,
    'MaxKeys' => 10
));

foreach($objects as $object) {
    $key = $object['Key'];

    echo "Processing " . $key . "\n";

    $response = $OBJ_aws_s3->copyObject(array(
        'Bucket' => $bucket, 
        'Key' => $key,
        'CopySource' => urlencode($bucket . '/' . $key),
        'CacheControl' => 'max-age=94608000',
        'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+3 years')),
        'MetadataDirective' => 'COPY',
    ));
}

Upvotes: 10

Related Questions