How to change metadata on an object in Amazon S3

前端 未结 7 1122
迷失自我
迷失自我 2020-12-24 06:01

If you have already uploaded an object to an Amazon S3 bucket, how do you change the metadata using the API? It is possible to do this in the AWS Management Console, but it

7条回答
  •  轮回少年
    2020-12-24 06:31

    In Java, You can copy object to the same location. Here metadata will not copy while copying an Object. You have to get metadata of original and set to copy request. This method is more recommended to insert or update metadata of an Amazon S3 object

    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);
    

提交回复
热议问题