AWS S3 - 使用PHP SDK2设置元数据

时间:2013-05-12 04:06:14

标签: php amazon-web-services amazon-s3

我正在尝试使用AWS PHP SDK2更改S3上特定存储桶中所有对象的元数据。我在使用新SDK找到一个具体的例子时遇到了麻烦,但是拼凑了以下内容:

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

foreach循环成功遍历给定$ bucket中的前10个项目,但我在copyObject()操作上收到403错误:

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

我不确定这是因为传递给copyObject的错误值,还是S3中的某些设置。请注意,我尚未在IAM中创建受权限制的帐户,并且正在使用应拥有对象所有权限的基本帐户。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:9)

好的,想通了 - 我的语法在两个方面都不正确。

首先,我使用CopySource的错误值。来自documentation

  

CopySource - (字符串) - 源对象的名称和源对象的键名称,用斜杠(/)分隔。必须是URL编码。

所以在我的情况下,它不应该只使用'CopySource' => $key,,而应该是'CopySource' => urlencode($bucket . '/' . $key),。这解释了403错误,因为我基本上告诉API我的源文件位于{key}的{bucket} / {key}。

第二个问题与特定标头有关 - 在Metadata字段中指定Expires和Cache-Control标头会导致创建特定于Amazon的元值,其键前缀为x-amz-meta-。相反,我现在使用ExpiresCacheControl参数。我的最终工作代码:

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

答案 1 :(得分:0)

使用'MetadataDirective' => 'REPLACE'而非COPY更新现有存储桶中的文件时,保留Paul Mennega的原始答案以保留默认的ContentTypes。

`

//
// 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";

`