¿Es posible cambiar los encabezados de un objeto S3 sin descargar todo el objeto?
-
06-07-2019 - |
Pregunta
He subido un montón de imágenes a Amazon S3 y ahora quiero agregarles un encabezado Cache-Control.
¿Se puede actualizar el encabezado sin descargar la imagen completa? Si es así, ¿cómo?
Solución
Es la funcionalidad beta, pero puede especificar nuevos metadatos cuando copiar un objeto . Especifique el mismo origen y destino para la copia, y esto tiene el efecto de actualizar los metadatos en su objeto.
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
Otros consejos
Esto está fuera de beta y está disponible haciendo un comando put y copiando el objeto como documentado aquí . También está disponible en sus SDK. Por ejemplo con 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);
Así es como lo hace con AWS SDK para 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;
}
?>
con el aws-sdk de amazon, hacer un copy_object con encabezados adicionales parece ser el truco para configurar los encabezados de control de almacenamiento en caché para un objeto S3 existente.
===================== 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 ======================= =========================
En Java, intente esto
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));
También puede intentar copiar el objeto. Aquí los metadatos no se copiarán mientras se copia un Objeto. Debe obtener metadatos del original y configurar la solicitud de copia. Este método es más recomendable para insertar o actualizar metadatos de un objeto de Amazon 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);
Aquí hay un código de ayuda en 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
Explicación: el código agrega nuevos metadatos a la clave existente y luego copia el mismo archivo.