Part 4 : Programmatically accessing bucket and analyzing failures

Anirudha | Sun, 11/24/2019 - 08:09

I am using python boto3 API here.

import boto3
session = boto3.session.Session()
access = "Eph1ZAx4RgfR-aqzepB3B4twX8EGpJMv"
secret = "RIWP9BtqVDF6PksEhLeBr8ZMBWNSLOkb"
endpoint_url = "http://objects007.scalcia.com"

#Create boto3 client 
s3_client=session.client(aws_access_key_id=access, aws_secret_access_key=secret,
                         endpoint_url=endpoint_url, service_name="s3")
bucket_name = "demobucket"

#Try accessing bucket metadata.
s3_client.head_bucket(Bucket=bucket_name)

If you execute this code from any file Or directly from python prompt. You should see below failure.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/my_virtual_env/lib/python2.7/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/user/my_virtual_env/lib/python2.7/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadBucket operation: Forbidden

Failure is expected here since we have not shared the bucket with user. Now share the bucket with “Josh” (refer these steps) and try accessing the bucket again.

print s3_client.head_bucket(Bucket=bucket_name)

#Output is
{
  "ResponseMetadata": {
    "HostId": "",
    "RetryAttempts": 0,
    "HTTPStatusCode": 200,
    "RequestId": "15D9F38FFFD93FF3",
    "HTTPHeaders": {
      "date": "Sun, 24 Nov 2019 01:02:24 GMT",
      "accept-ranges": "bytes",
      "x-amz-request-id": "15D9F38FFFD93FF3",
      "vary": "Origin",
      "server": "NutanixS3"
    }
  }
}

Above success means, user has read access to the bucket. Now try creating some object in the bucket :

print s3_client.put_object(Bucket=bucket_name, 
                           Key="someobject",
                           Body="this is the content of this object")

#Output is
{
  "ETag": "691f00f7d54f26bd5970ebb6bfa4ebd8",
  "ResponseMetadata": {
    "HostId": "",
    "RetryAttempts": 0,
    "HTTPStatusCode": 200,
    "RequestId": "15D9F39B56B20978",
    "HTTPHeaders": {
      "content-length": "0",
      "accept-ranges": "bytes",
      "vary": "Origin",
      "server": "NutanixS3",
      "etag": "691f00f7d54f26bd5970ebb6bfa4ebd8",
      "x-amz-request-id": "15D9F39B56B20978",
      "date": "Sun, 24 Nov 2019 01:03:13 GMT"
    }
  }
}

 

You can execute other API's such as delete, list etc... on the bucket. Take a look at Objects supported API's documentation to know what all API's are allowed.

Thats it. You don't need anything special to access Objects API . Standard Aws SDK will work just fine.