Cómo cambiar metadatos en un objeto en Amazon S3
Si ya ha cargado un objeto en un bucket de Amazon S3, ¿cómo cambia los metadatos mediante la API? Es posible hacer esto en la consola de administración de AWS, pero no está claro cómo se podría hacer mediante programación. Específicamente, estoy usando la API boto en Python y de la lectura de la fuente es claro que el uso de clave.set_metadata solo funciona antes de que se cree el objeto, ya que solo afecta a un diccionario local.
7 answers
Parece que necesita sobrescribir el objeto consigo mismo, usando un "PUT Object (Copy)" con un encabezado x-amz-metadata-directive: REPLACE
además de los metadatos. En boto, esto se puede hacer así:
k = k.copy(k.bucket.name, k.name, {'myKey':'myValue'}, preserve_acl=True)
Tenga en cuenta que los metadatos que no incluya en el diccionario antiguo se eliminarán. Por lo tanto, para preservar los atributos antiguos, tendrá que hacer algo como:
k.metadata.update({'myKey':'myValue'})
k2 = k.copy(k.bucket.name, k.name, k.metadata, preserve_acl=True)
k2.metadata = k.metadata # boto gives back an object without *any* metadata
k = k2;
Casi me perdí esta solución, que se insinúa en la introducción a una pregunta mal titulada que en realidad trata de un problema diferente que esta pregunta: Cambiar Contenido-Disposición del objeto S3 existente
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:46:49
Para establecer metadatos en archivos S3,simplemente no proporcione la ubicación de destino, ya que solo la información de origen es suficiente para establecer metadatos.
final ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata(metadataKey, value);
final CopyObjectRequest request = new CopyObjectRequest(bucketName, keyName, bucketName, keyName)
.withSourceBucketName(bucketName)
.withSourceKey(keyName)
.withNewObjectMetadata(metadata);
s3.copyObject(request);`
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-26 19:02:00
Si desea que sus metadatos se almacenen de forma remota, use set_remote_metadata
Ejemplo:
key.set_remote_metadata({'to_be': 'added'}, ['key', 'to', 'delete'], {True/False})
La implementación está aquí: https://github.com/boto/boto/blob/66b360449812d857b4ec6a9834a752825e1e7603/boto/s3/key.py#L1875
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-14 10:35:08
Puede cambiar los metadatos sin volver a cargar el objeto utilizando el comando copiar. Vea esta pregunta: ¿Es posible cambiar los encabezados de un objeto S3 sin descargar el objeto completo?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:34:17
Para la primera respuesta es una buena idea incluir el tipo de contenido original en los metadatos, por ejemplo:
key.set_metadata('Content-Type', key.content_type)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-17 21:44:42
En Java, puede copiar el objeto a la misma ubicación. Aquí los metadatos no se copiarán mientras se copia un objeto. Debe obtener los metadatos del original y establecer la solicitud de copia. Este método es más recomendable para insertar o actualizar metadatos de un objeto 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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-23 05:23:20
Aquí está el código que funcionó para mí. Estoy usando aws-java-sdk-s3 versión 1.10.15
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(fileExtension.getMediaType());
s3Client.putObject(new PutObjectRequest(bucketName, keyName, tempFile)
.withMetadata(metadata));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-04 05:35:54