S3 examples using boto3
Use Boto3 with WEKA S3 for credentials, temporary tokens, pre-signed URLs, and object operations.
Boto3
Boto3, the official AWS SDK for Python, is used to create, configure, and manage AWS services.
The following are examples of defining a resource/client in boto3 for the WEKA S3 service, managing credentials and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.
Installation
pip install boto3
Credentials
There are many ways to set credentials in boto3, as described on the boto3 credentials page. Specifically, look into the Assume Role Provider method, which uses the access/secret keys to automatically generate and use the temporary security token.
For WEKA S3, use the S3 access key and secret key generated by WEKA. The system creates these credentials automatically when an administrator attaches an S3 policy to a user, or when an administrator or user runs weka s3 user keys-generate.
Do not use the WEKA account username and password in the aws_access_key_id and aws_secret_access_key fields.
Resource
Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than service clients' raw, low-level calls. To use resources, invoke the resource() method of a Session and pass in a service name.
Use the WEKA-generated S3 access key and secret key in the boto3 session:
s3 = boto3.resource('s3',
endpoint_url='https://weka:9000',
aws_access_key_id='s3_key',
aws_secret_access_key='s3_secret')
Client
Clients provide a low-level interface to AWS, whose methods map close to 1:1 with service APIs. All service operations are supported by clients (in our case, s3 and sts).
Assume role example
Example code of using an access/secret key to obtain a temporary security token for the S3 service:
Pre-signed URL example
Example of signing on a GET request for myobject within mybucket for anonymous access:
Use the response to access the object without providing any credentials:
Pre-signed URL with assume role example
Combine the above two examples by providing a pre-signed URL from a temporary security token:
Upload/Download example
An example of using the boto3 resource to upload and download an object:
Create bucket example
An example of creating a bucket newbucket with a boto3 client: