2008-11-06 246 views

回答

34

它是測試版的功能,但是當您使用copy an object時,您可以指定新的元數據。爲副本指定相同的源和目標,並且這隻會更新對象上的元數據。

PUT /myObject HTTP/1.1 
Host: mybucket.s3.amazonaws.com 
x-amz-copy-source: /mybucket/myObject 
x-amz-metadata-directive: REPLACE 
x-amz-meta-myKey: newValue 
+6

不要忘記在headers參數中包含對象的Content-type,因爲PUT請求會重寫所有原始標題。 – 2011-04-12 11:44:50

+0

通常,複製操作會在源和目標相同時使用您提供的元數據重寫所有元數據。請參閱[文檔](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html),瞭解x-amz-metadata-directive```,它需要相同的拷貝請求目的地來指定```REPLACE```。如果要保留現有用戶或S3元數據,則需要獲取現有對象的元數據,添加/更改條目並在複製請求中提供更新後的元數據。 – pauljm 2015-04-05 15:04:51

10

這已經過測試版,可以通過執行put命令並將對象複製爲documented here。它也可以在他們的SDK中使用。例如用C#:

var s3Client = new AmazonS3Client("publicKey", "privateKey"); 
var copyRequest = new CopyObjectRequest() 
        .WithDirective(S3MetadataDirective.REPLACE) 
        .WithSourceBucket("bucketName") 
        .WithSourceKey("fileName") 
        .WithDestinationBucket("bucketName") 
        .WithDestinationKey("fileName) 
        .WithMetaData(new NameValueCollection { { "x-amz-meta-yourKey", "your-value }, { "x-amz-your-otherKey", "your-value" } }); 
var copyResponse = s3Client.CopyObject(copyRequest); 
+0

感謝您的更新。 :) – Scott 2012-01-19 01:09:59

+0

@Scott嗨,得到「試圖使用不是或不再可用的對象」。使用新的API – 2014-03-06 13:25:31

2

與亞馬遜AWS-SDK,這樣做有額外的頭一個copy_object似乎這樣的伎倆來設置緩存控制標頭爲現有S3對象。

===================== x ======================== =======================

<?php 
    error_reporting(-1); 
    require_once 'sdk.class.php'; 

    // UPLOAD FILES TO S3 
     // Instantiate the AmazonS3 class 
    $options = array("key" => "aws-key" , "secret" => "aws-secret") ; 


     $s3 = new AmazonS3($options); 
     $bucket = "bucket.3mik.com" ; 


    $exists = $s3->if_bucket_exists($bucket); 
    if(!$exists) { 
     trigger_error("S3 bucket does not exists \n" , E_USER_ERROR); 
    } 

    $name = "cows-and-aliens.jpg" ; 
    echo " change headers for $name \n" ; 
    $source = array("bucket" => $bucket, "filename" => $name); 
    $dest = array("bucket" => $bucket, "filename" => $name); 

    //caching headers 
    $offset = 3600*24*365; 
    $expiresOn = gmdate('D, d M Y H:i:s \G\M\T', time() + $offset); 
    $headers = array('Expires' => $expiresOn, 'Cache-Control' => 'public, max-age=31536000'); 

     $meta = array('acl' => AmazonS3::ACL_PUBLIC, 'headers' => $headers); 

    $response = $s3->copy_object($source,$dest,$meta); 
    if($response->isOk()){ 
     printf("copy object done \n"); 

    }else { 
     printf("Error in copy object \n"); 
    } 

?> 

==================== === X ============================================== ==

7

這是你如何與AWS SDK做了PHP 2:

<?php 
require 'vendor/autoload.php'; 

use Aws\Common\Aws; 
use Aws\S3\Enum\CannedAcl; 
use Aws\S3\Exception\S3Exception; 

const MONTH = 2592000; 

// Instantiate an S3 client 
$s3 = Aws::factory('config.php')->get('s3'); 
// Settings 
$bucketName = 'example.com'; 
$objectKey = 'image.jpg'; 
$maxAge = MONTH; 
$contentType = 'image/jpeg'; 

try { 
    $o = $s3->copyObject(array(
     'Bucket' => $bucketName, 
     'Key' => $objectKey, 
     'CopySource' => $bucketName . '/'. $objectKey, 
     'MetadataDirective' => 'REPLACE', 
     'ACL' => CannedAcl::PUBLIC_READ, 
     'command.headers' => array(
      'Cache-Control' => 'public,max-age=' . $maxAge, 
      'Content-Type' => $contentType 
     ) 
    )); 

    // print_r($o->ETag); 
} catch (Exception $e) { 
    echo $objectKey . ': ' . $e->getMessage() . PHP_EOL; 
} 
?> 
1

在Java中,試試這個

S3Object s3Object = amazonS3Client.getObject(bucketName, fileKey); 
ObjectMetadata metadata = s3Object.getObjectMetadata(); 
Map customMetaData = new HashMap(); 
customMetaData.put("yourKey", "updateValue"); 
customMetaData.put("otherKey", "newValue"); 
metadata.setUserMetadata(customMetaData); 

amazonS3Client.putObject(new PutObjectRequest(bucketName, fileId, s3Object.getObjectContent(), metadata)); 

您也可以嘗試複製對象。這裏元數據在複製對象時不會複製。 您必須獲取原始的元數據並設置爲複製請求。推薦 這種方法更多地插入或亞馬遜S3對象

ObjectMetadata metadata = amazonS3Client.getObjectMetadata(bucketName, fileKey); 
ObjectMetadata metadataCopy = new ObjectMetadata(); 
metadataCopy.addUserMetadata("yourKey", "updateValue"); 
metadataCopy.addUserMetadata("otherKey", "newValue"); 
metadataCopy.addUserMetadata("existingKey", metadata.getUserMetaDataOf("existingValue")); 

CopyObjectRequest request = new CopyObjectRequest(bucketName, fileKey, bucketName, fileKey) 
     .withSourceBucketName(bucketName) 
     .withSourceKey(fileKey) 
     .withNewObjectMetadata(metadataCopy); 

amazonS3Client.copyObject(request); 
0

以下是Python中的幫助代碼。

import boto 
 

 
one_year = 3600*24*365 
 
cckey = 'cache-control' 
 
s3_connection = S3Connection() 
 
bucket_name = 'my_bucket' 
 
bucket = s3_connection.get_bucket(bucket_name validate=False) 
 

 

 
for key in bucket: 
 
    key_name = key.key 
 
    if key.size == 0: # continue on directories 
 
     continue 
 
    # Get key object 
 
    key = bucket.get_key(key_name) 
 

 
    if key.cache_control is not None: 
 
     print("Exists") 
 
     continue 
 

 
    cache_time = one_year 
 
    #set metdata 
 
    key.set_metadata(name=cckey, value = ('max-age=%d, public' % (cache_time))) 
 
    key.set_metadata(name='content-type', value = key.content_type) 
 
    # Copy the same key 
 
    key2 = key.copy(key.bucket.name, key.name, key.metadata, preserve_acl=True) 
 
    continue 
 

 

說明:代碼添加新的元數據到現有的密鑰,然後複製相同的文件。

相關問題