HCP S3 Code Example using Boto and Python

Document created by Nick DeRoo on Feb 3, 2016Last modified by Michael Ratner on Oct 15, 2017
Version 7Show Document
  • View in full screen mode

Boto v2.39.0 with Python 2.7 Example

 

S3 access requires that you convert your username/password to the namespace to base64 and md5sum respectively.

Access Key: base64 of the tenant name or username that can access the s3 namespace on the HCP

Secret Access Key: md5sum of the password

 

Note:  You will need to enable MAPI on the tenant to create new buckets (namespaces) with the HS3 API.  New namespaces will be created with the namespace defaults configured at the Tenant Management Console > Configuration > Namespace Defaults.

[nderoo@nderoo-dt ~]$ python

Python 2.7.3 (default, Aug  9 2012, 17:23:57)

[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import boto

>>> from boto.s3.key import Key

>>> import boto.s3.connection

>>> AWS_ACCESS_KEY_ID = '<access key>'

>>> AWS_SECRET_ACCESS_KEY = '<my secret key>'

>>> Bucketname = '<my existing HCP Namespace Name>'

>>> c = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,

...         host ='t1.cluster64b-vm4.lab.archivas.com',

...         is_secure=True,          

...         calling_format = boto.s3.connection.OrdinaryCallingFormat(),

...         )

>>> bucket = conn.get_bucket(Bucketname)

>>> bucket

<Bucket: ns2>

>>> # Create a bucket

>>> c.create_bucket('nick')

<Bucket: nick>

>>> b = conn.get_bucket('nick')

>>> # delete a bucket

>>> c.delete_bucket(b)

>>> # creating a namespace and uploading a object to it

>>> c.create_bucket('s3namespace')

<Bucket: s3namespace>

>>> b = c.get_bucket('s3namespace')

>>> b

<Bucket: s3namespace>

>>> k = Key(b)

>>> k.key = 'newfile'

>>> k.set_contents_from_filename('/tmp/dir.csv')

1694

>>> # Downloading a object

>>> k.get_contents_to_filename('/tmp/downfile')

>>> k.get_contents_as_string()

'                (449L,)\n                (500L,)\n                (558L,)\n                (421L,)\n                (661L,)\n                (464L,)\n                (664L,)   \n                (576L,)   \n                (747L,)   \n                (665L,)   \n                (673L,)   \n                (725L,)   \n                (535L,)   \n                (712L,)   \n                (453L,)   \n                (658L,)   \n                (762L,)   \n                (597L,)   \n                (736L,)   \n                (747L,)   \n                (582L,)   \n                (714L,)   \n                (456L,)   \n                (684L,)   \n                (435L,)   \n                (581L,)   \n                (492L,)   \n                (475L,)   \n                (608L,)   \n                (442L,)   \n                (687L,)   \n                (495L,)   \n                (722L,)   \n                (644L,)   \n                (711L,)   \n                (742L,)   \n                (578L,)   \n                (733L,)   \n                (495L,)   \n                (672L,)   \n                (435L,)   \n                (569L,)   \n                (491L,)   \n                (434L,)   \n                (606L,)   \n                (458L,)   \n                (677L,)   \n                (499L,)   \n                (452L,)   \n                (519L,)   \n                (545L,)   \n                (446L,)   \n                (645L,)   \n                (446L,)   \n                (727L,)   \n                (553L,)   \n                (757L,)   \n                (725L,)   \n                (653L,)   \n                (790L,)\n                (509L,)\n                (668L,)\n                (445L,)\n                (596L,)'

>>>

Attachments

    Outcomes