Skip to main content

AWS Python SDK Example

CAUTION

For versions 1.36+, make sure you disable Checksum Calculation and Verification

The following example shows how AWS Python SDK can be used to interact with Telnyx Cloud Storage.

import requests
import uuid
import os
from botocore.config import Config
import boto3

# Only perform CRC checks `when_required`
config = Config(
request_checksum_calculation="when_required",
response_checksum_validation="when_required",
)

telnyx_api_key = os.getenv("TELNYX_API_KEY")

if not telnyx_api_key:
print("TELNYX_API_KEY environment variable not set")
exit(1)

# 1. Initialize the AWS client with specific options
client = boto3.client(
"s3",
endpoint_url="https://us-central-1.telnyxcloudstorage.com",
aws_access_key_id=telnyx_api_key,
aws_secret_access_key=telnyx_api_key,
config=config
)

# 2. Create a bucket
bucket_name = f"my-test-bucket-{uuid.uuid4()}"

client.create_bucket(Bucket=bucket_name)

# 3. Upload two objects with random data
for i in range(2):
name = f"my-test-object-{i}"
body = f"Telnyx Cloud Storage {i}"

client.put_object(Bucket=bucket_name, Key=name, Body=body)

# 4. List objects in the bucket
for obj in client.list_objects(Bucket=bucket_name)["Contents"]:
print(obj["Key"])

# 5. Download the first object
result = client.get_object(Bucket=bucket_name, Key="my-test-object-0")

print(result["Body"].read())

# 6. Create a presigned URL for the first file
response = requests.post(
f"https://api.telnyx.com/v2/storage/buckets/{bucket_name}/my-test-object-0/presigned_url",
json={"TTL": 30},
headers={"Authorization": f"Bearer {telnyx_api_key}"},
)

body = response.json()

print(body)

# 7. Download the file using the presigned URL
response = requests.get(body["data"]["presigned_url"])

print(response.text)