AWS S3 SDKs

Overview

AWS S3 SDKs allow you to programmatically manage the contents of your StackPath Object Storage buckets.

To use the S3 API with our Object Storage solution, you must configure the endpoint setting to point towards the endpoint URL.

Available Languages

AWS S3 SDKs are available for the following languages:

  • JavaScript
  • Python (Boto)
  • PHP
  • .NET
  • Ruby
  • Java

For more details, please visit the official Using the AWS SDKs page.

In our examples below, we are using the S3 library, boto3 for python. The initial configuration for this would appear as follows:

import os
import boto3

session = boto3.session.Session()
client = session.client('s3',
                        region_name='us-east-1',
                        endpoint_url='https://s3.us-east-1.stackpathstorage.com',
                        aws_access_key_id=os.getenv('ACCESS_KEY'),
                        aws_secret_access_key=os.getenv('SECREY_KEY'))

Uploading a File

Below is an example of how to upload a file using the public-read ACL, meaning it will be publicly accessible. Use the private canned ACL if you do not want the file to be publicly accessible. Refer to AWS' official Access control list (ACL) overview guide for more information.

client.put_object(Bucket='spbucket',
                  Key='file.ext',
                  Body=b'The contents of the file.',
                  ACL='public-read',
                  Metadata={
                      'x-amz-meta-my-key': 'your-value'
                  }
                )

Listing Files in a Bucket

The example below provides you with a list of your buckets files.

response = client.list_objects(Bucket='spbucket')
for obj in response['Contents']:
    print(obj['Key'])

Deleting a File

In the following example, we are deleting a file named example-file.ext from our bucket spbucket.

client.delete_object(Bucket='spbucket',
                     Key='example-file.ext')