Is it possible to change headers on an S3 object without downloading the entire object?

后端 未结 6 1820
悲哀的现实
悲哀的现实 2020-12-08 09:59

I\'ve uploaded a bunch of images to Amazon S3, and now want to add a Cache-Control header to them.

Can the header be updated without downloading the entire image? I

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 10:31

    This is how you do it with AWS SDK for PHP 2:

    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;
    }
    ?>
    

提交回复
热议问题