当我将文件覆盖到S3时,我将丢失元数据,因为它被视为新对象。我试图在S3中覆盖文件时保留文件的现有原始元数据。我从文档中知道你无法修改S3存储桶中对象的元数据,但我如何复制"将现有元数据放到新覆盖的对象上?
例如,我有以下方法:
public void uploadToS3(File file, String key, String bucketName) {
// original metadata
ObjectMetadata objMetadata = client.getObjectMetadata(bucketName, key);
// How do I set the new overwritten object to have this metadata above??
try {
client.putObject(new PutObjectRequest(bucketName, key, file));
} catch (AmazonServiceException e) {
e.printstacktrace();
} catch(AmazonClientException e) {
e.printstacktrace();
}
我如何"指向"新对象与刚刚覆盖的对象具有相同的元数据?
答案 0 :(得分:0)
在致电AmazonS3#putObject(PutObjectRequest)
之前,请先致电PutObjectRequest#withMetadata(ObjectMetadata)
。
ObjectMetadata objMetadata = client.getObjectMetadata(bucketName, key);
client.putObject(new PutObjectRequest(bucketName, key, file)
.withMetadata(objMetadata));
请注意,根据Object Key and Metadata上的S3文档,只允许更新某些元数据。因此,您可能需要采取额外步骤以确保您的请求不会尝试修改它不应该修改的内容。
ObjectMetadata objMetadata = client.getObjectMetadata(bucketName, key);
// Call a helper method that clones only what you need to maintain in the metadata.
ObjectMetadata clonedMetadata = cloneMetadata(objMetadata);
client.putObject(new PutObjectRequest(bucketName, key, file)
.withMetadata(objMetadata));