How do I use AWS SDK for Python (boto3) with Wasabi?

AWS SDK for Python (boto3) has been validated for use with Wasabi.  

 

To use the AWS SDK for Python execute the following steps:

1) Install the AWS SDK for Python using pip

2) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload and Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket

Other examples can be referred from the AWS documentation.   

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

Please send all IAM requests to iam.wasabisys.com

 

1. How to set the credentials

The connection in boto3 can be set via the client method.

 

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
# for IAM us-east-1 is the default endpoint.
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

iam = boto3.client('<connection name>',
                  endpoint_url='<endpoint-url>',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# iam = boto3.client('<connection name>',
#                    endpoint_url='<endpoint-url>',
#                    aws_access_key_id="<insert-access-key>",
#                    aws_secret_access_key="<insert-secret-key>")

 

2. Connect to IAM and S3 endpoints

IAM:

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
# for IAM us-east-1 is the default endpoint.
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

iam = boto3.client('iam',
                  endpoint_url='https://iam.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key,
                  region_name='us-east-1')

# Use the following code to connect directly via raw credentials.
# for IAM us-east-1 is the default endpoint.
# iam = boto3.client('iam',
#                    endpoint_url='https://iam.wasabisys.com',
#                    aws_access_key_id="<insert-access-key>",
#                    aws_secret_access_key="<insert-secret-key>",
#                    region_name='us-east-1')

 

S3:

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

s3 = boto3.client('s3',
                  endpoint_url='https://s3.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# s3 = boto3.client('s3',
#                   endpoint_url='https://s3.wasabisys.com',
#                   aws_access_key_id="<insert-access-key>",
#                   aws_secret_access_key="<insert-secret-key>")


3. Create a user using IAM

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
# for IAM us-east-1 is the default endpoint.
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

iam = boto3.client('iam',
                  endpoint_url='https://iam.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key,
                  region_name='us-east-1')

# Use the following code to connect directly via raw credentials.
# for IAM us-east-1 is the default endpoint.
# iam = boto3.client('iam',
#                    endpoint_url='https://iam.wasabisys.com',
#                    aws_access_key_id="<insert-access-key>",
#                    aws_secret_access_key="<insert-secret-key>",
#                    region_name='us-east-1')

iam.create_user(UserName='<iam-user>')

 

4. Create a Bucket

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

s3 = boto3.client('s3',
                  endpoint_url='https://s3.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# s3 = boto3.client('s3',
#                   endpoint_url='https://s3.wasabisys.com',
#                   aws_access_key_id="<insert-access-key>",
#                   aws_secret_access_key="<insert-secret-key>")

bucket_name = "<bucket-name>"

s3.create_bucket(Bucket=bucket_name)

 

5. Upload an object to the Bucket

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

s3 = boto3.client('s3',
                  endpoint_url='https://s3.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# s3 = boto3.client('s3',
#                   endpoint_url='https://s3.wasabisys.com',
#                   aws_access_key_id="<insert-access-key>",
#                   aws_secret_access_key="<insert-secret-key>")

file_path = "<file-to-upload>"
bucket_name = "<bucket-name>"
key_name = "<key-name>"

s3.put_object(Body=file_path, Bucket=bucket_name, Key=key_name)


6.
Read an object from the Bucket

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

s3 = boto3.client('s3',
                  endpoint_url='https://s3.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# s3 = boto3.client('s3',
#                   endpoint_url='https://s3.wasabisys.com',
#                   aws_access_key_id="<insert-access-key>",
#                   aws_secret_access_key="<insert-secret-key>")

bucket_name = "<bucket-name>"
key_name = "<key-name>"

s3.get_object(Bucket=bucket_name, Key=key_name)

 

7. Delete the object from the Bucket

import boto3

# Use the following code to connect using Wasabi profile from .aws/credentials file
session = boto3.Session(profile_name="wasabi")
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key

s3 = boto3.client('s3',
                  endpoint_url='https://s3.wasabisys.com',
                  aws_access_key_id=aws_access_key_id,
                  aws_secret_access_key=aws_secret_access_key)

# Use the following code to connect directly via raw credentials.
# s3 = boto3.client('s3',
#                   endpoint_url='https://s3.wasabisys.com',
#                   aws_access_key_id="<insert-access-key>",
#                   aws_secret_access_key="<insert-secret-key>")

bucket_name = "<bucket-name>"
key_name = "<key-name>"

s3.delete_object(Bucket=bucket_name, Key=key_name)

 

Have more questions? Submit a request