Get version after PubObject using SDK for Java v2

0

I'm trying to migrate from SDK for Java v1 to v2.

With the AmazonS3 client in SDK v1, PutObject returns PutObjectResult which makes the object metadata available. From the object metadata I can get the version ID of the file (along with content length and last modified timestamp).

I don't see a way to get any of this information from the S3Client in SDK v2 from the response to the PutObject. I do not want to have to make a separate call to get the version ID.

I'm really hoping someone can tell me that this data is still available in the response and that I'm just not seeing it. Any pointers would be appreciated.

Thanks.

Edited to add that I don't want to make a second request. I'm looking for similar behavior to v1.

1 Answer
0

Hi Mark,

Yes, data is still available.

You can get version id by us issuing a headObject method call (https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html#headObject(java.util.function.Consumer)).

The object needs to exists before you can call the method so first you ll need to PUT it and then execute the HEAD call.

It’s response, HeadObjectResponse, will contain the version Id:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/HeadObjectResponse.html#versionId()

Hope it clarifies

profile picture
EXPERT
answered a year ago
  • I know I can make a second call, but that's not what I want.

    In v1 I can get the version in the response of the PubObject. If I've got to do a second call, there's no guarantee that I'm going to get the version of what I just uploaded if someone else uploads with the same key in the mean time.

  • The only ways I see is either as described above, or actually, according to https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html, the response object contains a header field called: x-amz-version-id, the Version of the object. Let me know if it gives you the right value

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions