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.

Mark
已提問 1 年前檢視次數 244 次
1 個回答
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
專家
已回答 1 年前
  • 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

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南