Part 3 : S3 APIs for Objects Versioning.

Anirudha | Wed, 03/18/2020 - 06:06

 

Lets start with some code . We will first enable versioning on bucket and then upload few versions to bucket via S3 API using python SDK.

import boto3
session = boto3.session.Session()

#Endpoint information
endpoint_url = "http://Objects.scalcia.com"
access_key = "bVSjIccNo738F5VmpaYjA4BTV_J7h_YH"
secret_key = "_rCf_LXRPea1j9xmuCWKlFsWqu7Qa_1n"

#Bucket name
bucket = "bucket-from-api"

#Connect to your Objects endpoint.
s3c = session.client(aws_access_key_id=access_key,
                                 aws_secret_access_key=secret_key,
                                 endpoint_url=endpoint_url,
                                 service_name="s3")


#Check if bucket exists and create it.
print "Creating bucket : %s"%(bucket)
try:
  s3c.head_bucket(Bucket=bucket)
except Exception as err:
  s3c.create_bucket(Bucket=bucket)

#Enable versioning
print "Enabling versioning on bucket : %s"%(bucket)
versioning_config = {"Status":"Enabled"}
s3c.put_bucket_versioning(Bucket=bucket, VersioningConfiguration=versioning_config)

#Get versioning status of Bucket
print "Versioning status of bucket : %s"%(bucket)
print s3c.get_bucket_versioning(Bucket=bucket)

Find this code on git

Output :

Creating bucket : bucket-from-api

Enabling versioning on bucket : bucket-from-api

Versioning status of bucket : bucket-from-api

{u'Status': 'Enabled', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': '', 'RequestId': '15FCBD036858B7A4', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'accept-ranges': 'bytes', 'vary': 'Origin', 'server': 'NutanixS3', 'x-amz-request-id': '15FCBD036858B7A4', 'date': 'Mon, 16 Mar 2020 08:56:31 GMT', 'content-type': 'application/xml'}}}

Related API :

  • Put_bucket_versioning : Is the API to enable/suspend versioning.
  • Get_bucket_versioning : To get the bucket versioning status.
  • To suspend bucket versioning, change : versioning_config = {"Status":"Enabled"} -> versioning_config = {"Status":"Disabled"} in above code. 

 

Lets upload few versions and check how things look :

import boto3
session = boto3.session.Session()

#Endpoint information
endpoint_url = "http://Objects.scalcia.com"
access_key = "bVSjIccNo738F5VmpaYjA4BTV_J7h_YH"
secret_key = "_rCf_LXRPea1j9xmuCWKlFsWqu7Qa_1n"

#Bucket name
bucket = "bucket-from-api"

#Connect to your Objects endpoint.
s3c = session.client(aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
endpoint_url=endpoint_url,
service_name="s3")


#Check if bucket exists and create it.
print "Creating bucket : %s"%(bucket)
try:
  s3c.head_bucket(Bucket=bucket)
except Exception as err:
  s3c.create_bucket(Bucket=bucket)

#Enable versioning
print "Enabling versioning on bucket : %s"%(bucket)
versioning_config = {"Status":"Enabled"}
s3c.put_bucket_versioning(Bucket=bucket, VersioningConfiguration=versioning_config)

#Get versioning status of Bucket
print "Versioning status of bucket : %s"%(bucket)
s3c.get_bucket_versioning(Bucket=bucket)

#Upload 3 versions
objname = "moviename"
movies = ["The Dark Knight""Star Wars", "The God Father"]
for movie in movies:
  print s3c.put_object(Bucket=bucket, Key=objname, Body=movie)
  print "*"*10

Find this code on git

In above code :

  • We enabled versioning on bucket
  • Validate the bucket versioning.
  • I am uploading object=moviename, three times with different data. Which created 3 different versions of the same object.

 

Output :

Creating bucket : bucket-from-api

Enabling versioning on bucket : bucket-from-api

Versioning status of bucket : bucket-from-api

{u'VersionId': '209393', u'ETag': '"175b13d8ce8d83053c15f2687c916d78"', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': '', 'RequestId': '15FCBEFD7BDEC565', 'HTTPHeaders': {'content-length': '0', 'accept-ranges': 'bytes', 'vary': 'Origin', 'server': 'NutanixS3', 'etag': '"175b13d8ce8d83053c15f2687c916d78"', 'x-amz-request-id': '15FCBEFD7BDEC565', 'date': 'Mon, 16 Mar 2020 09:32:44 GMT', 'x-amz-version-id': '209393'}}}

**********

{u'VersionId': '209394', u'ETag': '"607cf407c1ec1582849e374324e76d48"', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': '', 'RequestId': '15FCBEFD82A00BBD', 'HTTPHeaders': {'content-length': '0', 'accept-ranges': 'bytes', 'vary': 'Origin', 'server': 'NutanixS3', 'etag': '"607cf407c1ec1582849e374324e76d48"', 'x-amz-request-id': '15FCBEFD82A00BBD', 'date': 'Mon, 16 Mar 2020 09:32:44 GMT', 'x-amz-version-id': '209394'}}}

**********

{u'VersionId': '209395', u'ETag': '"f381fa744927007d8ca0aa2d66d19b45"', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': '', 'RequestId': '15FCBEFD89AFE0E8', 'HTTPHeaders': {'content-length': '0', 'accept-ranges': 'bytes', 'vary': 'Origin', 'server': 'NutanixS3', 'etag': '"f381fa744927007d8ca0aa2d66d19b45"', 'x-amz-request-id': '15FCBEFD89AFE0E8', 'date': 'Mon, 16 Mar 2020 09:32:45 GMT', 'x-amz-version-id': '209395'}}}

**********

You may notice 'VersionId' returned in put_object response. And this is the unique identifier for a particular object.

From above code and output, this is how object version to content mapping look :

  • Object with version '209393' has content = “The Dark Knight”
  • Version '209394' has content = “Star Wars”
  • Version '209395' is the latest version (since this was the last upload) and has content = “The God Father”