AWS SDK for Python (boto3) With Wasabi
    • 12 Aug 2024
    • 5 Minutes to read
    • PDF

    AWS SDK for Python (boto3) With Wasabi

    • PDF

    Article summary

    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:

    1. Install the AWS SDK for Python using pip.

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

    In the following IAM and S3 examples, the profile name is set as "wasabi" in the "~/.aws/credentials" file. While the examples use Wasabi's us-east-1 storage region, you can use another appropriate Wasabi service URL.

    Refer to the AWS documentation for additional examples.

    Send all IAM requests to iam.wasabisys.com.

    Setting the Credentials

    Set the connection in boto3 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>")

    Connecting 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>")

    Creating 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>')

    Creating 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)

    Uploading 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)

    Reading 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)

    Deleting 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)