Python
1. Install The Dependencies
Before using AIOZ W3S, it is necessary to install the required dependencies. In this tutorial, we will be using Poetry (opens in a new tab) to handle the Python dependencies. So you need to install Poetry first as this tutorial (opens in a new tab). After you installed Poetry, to install the project dependencies, simply run the following command in your terminal:
poetry install
poetry shell
2. Setup The Environment
We will be setting up two environment variables, ACCESS_KEY
and SECRET_KEY
, which are required to access the AIOZ W3S service. Assign the values of these variables to the values of your credential that you created in the AIOZ W3S dashboard (opens in a new tab).
export ACCESS_KEY=<your-access-key>
export SECRET_KEY=<your-secret-key>
3. List All Buckets
To list all buckets, you can use the following code:
python list_buckets.py
Other functionalities are straightforward and follow a similar pattern. You can check out the code example repository for a comprehensive understanding.
Minimal Code Example
import os
import sys
import boto3
from botocore.exceptions import ClientError
from botocore.config import Config
def main():
s3_resource = boto3.resource('s3',
region_name='us-east-1',
# Get the credentials from the environment variables.
aws_access_key_id="YOUR_ACCESS_KEY_ID",
aws_secret_access_key="YOUR_SECRET_ACCESS_KEY",
endpoint_url="W3S_ENDPOINT_URL",
config=Config(
# signature_version='s3',
s3={'addressing_style': 'path'}
)
)
# Create an Amazon S3 service resource object.
bucket_name = "YOUR_BUCKET_NAME"
file_path = "YOUR_FILE_PATH"
bucket = s3_resource.Bucket(bucket_name)
object_key = os.path.basename(file_path)
bucket = bucket.Object(object_key)
with open(file_path, 'rb') as data:
try:
result = bucket.put(Body=data)
bucket.wait_until_exists()
print(f"Success", result)
except ClientError as e:
print("Error", e)
if __name__ == '__main__':
main()