Amazon AWS - S3¶
Nothing can be simpler to interact with Amazon Simple Storage Service (S3) than the BotCity plugin for AWS S3.
Interact with your buckets, files, folders and more using this simple and curated API.
Installation¶
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
Setting up connection¶
Caution
S3 does work only POSIX-style paths use "/" (forward slash). Relative paths are not allowed, if you are going to make any changes, put the absolute path of the file or folder.
Note
There are two different ways to authenticate.
1. Creating the .aws
folder in the home directory, you need to create two files.
# ~/.aws/credentials
[default]
aws_access_key_id=<your_aws_access_key_id>
aws_secret_access_key=<your_aws_secret_access_key>
2. Passing credentials in the class constructor.
Bucket operations¶
Listing buckets¶
Creating bucket¶
Attention
Amazon S3 supports global buckets, which means that each bucket name must be unique across all AWS accounts in all the AWS Regions within a partition.
Tip
All methods using bucket_name
parameter are optional and they can be defined like this.
Deleting bucket¶
File operations¶
Uploading file¶
Downloading file¶
Copying file¶
from botcity.plugins.aws.s3 import BotAWSS3Plugin
s3 = BotAWSS3Plugin()
# copying the file to another bucket
s3.copy_file(
bucket_name='<your_bucket_name>',
filename='example.txt',
target_bucket_name='another_bucket',
target_filename='copy_of_the_file_in_another_bucket.txt'
)
# copying the file to another place in the same bucket
s3.copy_file(
bucket_name='<your_bucket_name>',
filename='example.txt',
# target_bucket_name='another_bucket', ignore this argument
target_filename='copy_of_the_file_in_the_same_bucket.txt'
)
Listing files¶
Deleting file¶
Moving file¶
from botcity.plugins.aws.s3 import BotAWSS3Plugin
s3 = BotAWSS3Plugin()
# moving the file to another bucket
s3.move_file(
bucket_name='<your_bucket_name>',
filename='/test/current-file.txt',
target_bucket_name='another_bucket',
target_filename='/test/current-file.txt'
)
# copying the file to another place in the same bucket
s3.move_file(
bucket_name='<your_bucket_name>',
filename='/test/current-file.txt',
# target_bucket_name='another_bucket', ignore this argument
target_filename='/another_folder/current-file.txt'
)
Folder operations¶
Uploading folder¶
Attention
For now, empty folders are ignored.
Downloading folder¶
Copying folder¶
from botcity.plugins.aws.s3 import BotAWSS3Plugin
s3 = BotAWSS3Plugin()
# copying the folder to another bucket
s3.copy_folder(
bucket_name='<your_bucket_name>',
folder_name='/docs',
target_bucket_name='another_bucket',
target_folder_name='/docs'
)
# copying the folder to another place in the same bucket
s3.copy_folder(
bucket_name='<your_bucket_name>',
folder_name='/files/docs',
# target_bucket_name='another_bucket', ignore this argument
target_folder_name='/' # will copy folder to bucket root path. /files/docs -> /docs
)
Deleting folder¶
Moving folder¶
Tip
The function to move folders can be used to rename a folder.
from botcity.plugins.aws.s3 import BotAWSS3Plugin
s3 = BotAWSS3Plugin()
# moving the folder to another bucket
s3.move_folder(
bucket_name='<your_bucket_name>',
folder_path='/docs/tests',
target_bucket_name='another_bucket',
target_folder_name='/' # move /docs/tests to / in another bucket
)
# moving the folder to another place in the same bucket
s3.move_folder(
bucket_name='<your_bucket_name>',
folder_path='/docs',
# target_bucket_name='another_bucket', ignore this argument
target_folder_name='/copy-docs/' # move folder to inside /copy-docs/
)
# renaming folder
s3.move_folder(
bucket_name='<your_bucket_name>',
folder_path='/docs',
target_folder_name='/docs-v2' # rename folder
)
Renaming folder¶
Other operations¶
To use other AWS S3 methods, use the S3 client
property of the BotAWSS3Plugin
class.
Filter Enum
The filter operations (filter buckets
, filter files
) receive a Filter enum with possible values:
Filter.EQUALS
Filter.STARTS_WITH
Filter.ENDS_WITH
Filter.CONTAINS