Python boto3: receive versionId after uploading a file to S3

Refresh

6 days ago

Views

23 time

1

I want to upload a file to a version-enabled S3 bucket and need its version number. Ideally, without a separate API call to avoid any possibility of a race condition. I'm using the following code snippet for upload (which is working fine):

s3 = boto3.client("s3")
s3.upload_fileobj(file_handle, bucket_name, key)

The response of this function is None and I can't really see how it is defined in boto3, so it's hard to dive any deeper into it.

The official S3 documentation mentions that the version id is included in the header of the response after upload. However, I can't see how I can access this header with boto3.

Is this possible at all? If yes: how? If no: How can I hack boto3 so I can access this response header?

Fyi, I'm using boto3==1.9.64

Thanks for your help!

EDIT: Here the link to S3 documentation that talks about the x-amz-version-id header

1 answers

3

Вы можете использовать put_object с файловыми-подобными объектами. Он возвращается VersionIdв словаре ответа.