简体   繁体   中英

How to update metdata of object on amazon s3 using aws sdk?

I am working on java project which is related to amazon s3 services in which I am using jets3 library for s3 operations like create bucket etc, now i am replacing jets3 with aws-sdk-java-v2 but problem ie in jets3 there was following way to update metadata of uploaded object is:-

s3ervice.updateObjectMetadata(sBucket, sObject);

But in new aws-sdk I did not find any way. I am using following code block to update metdata but every time content replaced and I want update only metadata without replacing content so please suggest.

code :-

    public static boolean updateObjectMetadataByCopyObject(S3Client s3Client,
                                                       String bucketName,
                                                       String objectKey
) {
    Map ds = new HashMap<>();
    ds.put("ds", "dsa");
    String encodedUrl = null;
    try {
        encodedUrl = URLEncoder.encode(bucketName + "/" + objectKey, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        System.out.println("URL could not be encoded: " + e.getMessage());
    }
    CopyObjectRequest copyReq = CopyObjectRequest.builder()
            .destinationBucket(bucketName).copySource(encodedUrl)
            .destinationKey(objectKey).metadata(ds).storageClass(StorageClass.REDUCED_REDUNDANCY)
            .build();
    try {
        CopyObjectResponse copyRes = s3Client.copyObject(copyReq);
        copyRes.copyObjectResult().toString();
        return true;
    } catch (S3Exception e) {
        throw e;
    }
}## Heading ##

Please suggest how can update metdata of uploaded objects using aws-sdk-v2.

Metadata Without Copy

Unfortunately it is not possible to update the metadata without doing a copy object. Here is the excerpt from Amazon

After you upload the object, you cannot modify object metadata. The only way to modify object metadata is to make a copy of the object and set the metadata. The only way to modify object metadata is to make a copy of the object and set the metadata.

Working with object metadata

Furthermore, even the functionality exposed through the management console to change metadata does a copy object.

This action creates a copy of the object with updated settings and the last-modified date.

Editing object metadata in the Amazon S3 console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM