# AWS Python SDK

<!-- -->

This guide assumes that you have followed the steps in the [Getting Started](/docs/get-started/.md) guide, and have the access keys available.

You may continue to use the AWS Python SDK as you normally would, but with the endpoint set to Tigris at <https://t3.storage.dev>. Also ensure that the addressing style is set to `virtual`.

```
# Create S3 service client

svc = boto3.client(

    's3',

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)
```

## Getting started[​](#getting-started "Direct link to Getting started")

This example uses the [AWS SDK for Python (Boto3)](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html) and reads the default credentials file or the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.

<!-- -->

```
import boto3

from botocore.client import Config



# Create S3 service client

svc = boto3.client(

    's3',

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)



# List buckets

response = svc.list_buckets()



for bucket in response['Buckets']:

    print(f'  {bucket["Name"]}')



# List objects

response = svc.list_objects_v2(Bucket='tigris-example')



for obj in response['Contents']:

    print(f'  {obj["Key"]}')



# Upload file

response = svc.upload_file(

    'getting-started.py', 'tigris-example', 'getting-started.py')



# Download file

response = svc.download_file(

    'tigris-example', 'getting-started.py', 'getting-started-2.py')
```

## Using multiple AWS Profiles[​](#using-multiple-aws-profiles "Direct link to Using multiple AWS Profiles")

If you want to use Tigris alongside AWS, you'll need to differentiate your access keys. There are several options ranging from passing access keys as parameters when creating clients:

<!-- -->

```
import boto3

import os

from botocore.client import Config



client = boto3.client(

    's3',

    aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],

    aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)
```

Or you can add another profile to `~/.aws/credentials` directly:

```
# ~/.aws/credentials



[aws-compute]

aws_access_key_id=<access_key_id>

aws_secret_access_key=<access_key_secret>



[tigris]

aws_access_key_id=<access_key_id>

aws_secret_access_key=<access_key_secret>

endpoint_url=https://t3.storage.dev
```

To switch profiles while using `boto3`, you can set the `profile` on the `session`:

<!-- -->

```
import boto3

from botocore.client import Config



session = boto3.Session(profile_name='tigris')

tigris_s3_client = session.client(

    's3', config=Config(s3={'addressing_style': 'virtual'}))
```

To change the default `session` to use Tigris, you can configure boto3:

<!-- -->

```
import boto3



boto3.setup_default_session(profile_name='tigris')
```

## Using presigned URLs[​](#using-presigned-urls "Direct link to Using presigned URLs")

Presigned URLs can be used with the AWS Python (Boto3) SDK as follows:

<!-- -->

```
import boto3

from botocore.client import Config



# Create S3 service client

svc = boto3.client(

    's3',

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)



# Generate a presigned URL to upload an object

url = svc.generate_presigned_url(

    'put_object',

    Params={'Bucket': 'foo-bucket', 'Key': 'bar.txt'},

    ExpiresIn=604800

)



print(f'Presigned URL to upload an object: {url}')



# Generate a presigned URL to download an object

url = svc.generate_presigned_url(

    'get_object',

    Params={'Bucket': 'foo-bucket', 'Key': 'bar.txt'},

    ExpiresIn=604800

)



print(f'Presigned URL to download an object: {url}')
```

### Presigned URLs with custom domains[​](#presigned-urls-with-custom-domains "Direct link to Presigned URLs with custom domains")

You can also use a [presigned URL with a custom domain](/docs/objects/presigned/.md#presigned-url-with-custom-domain) by replacing the bucket host with your custom domain:

```
branded_url = presigned_url.replace(

    "foo-bucket.t3.storage.dev", "your-domain.example.com"

)

print(f"Presigned URL for GET (custom domain): {branded_url}")
```

## Object Metadata Querying[​](#object-metadata-querying "Direct link to Object Metadata Querying")

Below is an example for querying for objects [based on their metadata](/docs/objects/query-metadata/.md):

<!-- -->

```
import boto3

from botocore.client import Config



# Create S3 service client

svc = boto3.client(

    's3',

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)



# build an object metadata query





def _x_tigris_query(request, query):

    request.headers.add_header('X-Tigris-Query', query.strip())





# Register event into boto with custom query

svc.meta.events.register(

    "before-sign.s3.ListObjectsV2",

    lambda request, **kwargs: _x_tigris_query(

        request, '`Content-Type` = "text/plain" ORDER BY `Last-Modified`'),

)



response = svc.list_objects_v2(Bucket="tigris-example")



if 'Contents' not in response:

    print('No objects found with Content-Type "text/plain"')

    exit(1)



print('Objects found with Content-Type "text/plain":')

for obj in response['Contents']:

    print(f'* {obj["Key"]}')
```

Note that in order to use this feature, you need to create a separate boto3 client for each kind of query you want to do. This is a limitation of boto3. For best effect, create that boto3 client inline in the function that needs it.

## Object Renaming[​](#object-renaming "Direct link to Object Renaming")

Below is an example of how to use the AWS Python SDK to [rename an object](/docs/objects/object-rename/.md):

<!-- -->

```
import boto3

from botocore.client import Config

from uuid import uuid4



from tigris_boto3_ext import rename_object



# Create S3 service client

svc = boto3.client(

    's3',

    endpoint_url='https://t3.storage.dev',

    config=Config(s3={'addressing_style': 'virtual'}),

)



object_name = str(uuid4())

object_rename = str(uuid4())



svc.upload_file('rename-object.py', 'tigris-example', object_name)



# Rename object

rename_object(svc, 'tigris-example', object_name, object_rename)



# head object to make sure it exists

svc.head_object(Bucket='tigris-example', Key=object_rename)



# Delete object

svc.delete_object(Bucket='tigris-example', Key=object_rename)
```
