locopy.s3 module¶
S3 Module.
Module to wrap the boto3 api usage and provide functionality to manage multipart upload to S3 buckets.
- class locopy.s3.S3(profile=None, kms_key=None, **kwargs)[source]¶
Bases:
object
S3 wrapper class.
Utilizes the boto3 library to push files to an S3 bucket.
- Parameters:
profile (str, optional) – The name of the AWS profile to use which is typical stored in the
credentials
file. You can also set environment variableAWS_DEFAULT_PROFILE
which would be used instead.kms_key (str, optional) – The KMS key to use for encryption If kms_key Defaults to
None
then the AES256 ServerSideEncryption will be used.**kwargs – Optional keyword arguments.
- session¶
Hold the AWS session credentials / info
- Type:
boto3.Session
- s3¶
Hold the S3 client object which is used to upload/delete files to S3
- Type:
botocore.client.S3
- Raises:
S3Error – Error initializing AWS Session (ex: invalid profile)
S3CredentialsError – Issue with AWS credentials
S3InitializationError – Issue initializing S3 session
- delete_from_s3(bucket, key)[source]¶
Delete a file from an S3 bucket.
- Parameters:
- Raises:
S3DeletionError – If there is a issue deleting from the S3 bucket
- delete_list_from_s3(s3_list)[source]¶
Delete a list of files from an S3 bucket.
- Parameters:
s3_list (list) – List of strings with the s3 paths of the files to delete. The strings should not include the s3:// scheme.
- download_from_s3(bucket, key, local)[source]¶
Download a file from a S3 bucket.
- Parameters:
- Raises:
S3DownloadError – If there is a issue downloading to the S3 bucket
- parse_s3_url(s3_url)[source]¶
Extract the bucket and key from a s3 url.
- Parameters:
s3_url (str) – s3 url. The string can include the s3:// scheme (which is disgarded)
- Returns:
bucket (str) – s3 bucket
key (str) – s3 key
- upload_list_to_s3(local_list, bucket, folder=None)[source]¶
Upload a list of files to a S3 bucket.
- Parameters:
local_list (list) – List of strings with the file paths of the files to upload
bucket (str) – The AWS S3 bucket which you are copying the local file to.
folder (str, optional) – The AWS S3 folder of the bucket which you are copying the local files to. Defaults to
None
. Please note that you must follow the/
convention when using subfolders.
- Returns:
Returns a list of the generated S3 bucket and keys of the files which were uploaded. The
S3://
part is NOT include. The output would look like the following:["my-bucket/key1", "my-bucket/key2", ...]
- Return type:
Notes
There is a assumption that if you are loading multiple files (via splits) it follows a structure such as file_name.extension.# (# splits). It allows for the COPY statement to use the key prefix vs specificing an exact file name. The returned list helps with this process downstream.
- upload_to_s3(local, bucket, key)[source]¶
Upload a file to a S3 bucket.
- Parameters:
- Raises:
S3UploadError – If there is a issue uploading to the S3 bucket