carlogtt_python_library.aws_boto3.s3 module

This module …

class carlogtt_python_library.aws_boto3.s3.S3(aws_region_name: str, *, aws_profile_name: str | None = None, aws_access_key_id: str | None = None, aws_secret_access_key: str | None = None, aws_session_token: str | None = None, caching: bool = False, client_parameters: dict[str, Any] | None = None)[source]

Bases: AwsServiceBase[S3Client]

The S3 class provides a simplified interface for interacting with Amazon S3 services within a Python application.

It includes an option to cache the client session to minimize the number of AWS API call.

Parameters:
  • aws_region_name – The name of the AWS region where the service is to be used. This parameter is required to configure the AWS client.

  • aws_profile_name – The name of the AWS profile to use for credentials. This is useful if you have multiple profiles configured in your AWS credentials file. Default is None, which means the default profile or environment variables will be used if not provided.

  • aws_access_key_id – The AWS access key ID for programmatically accessing AWS services. This parameter is optional and only needed if not using a profile from the AWS credentials file.

  • aws_secret_access_key – The AWS secret access key corresponding to the provided access key ID. Like the access key ID, this parameter is optional and only needed if not using a profile.

  • aws_session_token – The AWS temporary session token corresponding to the provided access key ID. Like the access key ID, this parameter is optional and only needed if not using a profile.

  • caching – Determines whether to enable caching for the client session. If set to True, the client session will be cached to improve performance and reduce the number of API calls. Default is False.

  • client_parameters – A key-value pair object of parameters that will be passed to the low-level service client.

create_presigned_post_for_file(bucket: str, filename: str, expiration_time: int = 3600, **kwargs) dict[str, Any][source]

Creates a presigned URL and the form fields used for a presigned s3 post The URL expires after a fixed amount of time.

Parameters:
  • bucket – The name of the S3 bucket.

  • filename – The name of the file to upload.

  • expiration_time – The number of seconds until the URL expires. (Default: 3600).

  • kwargs – Any other param passed to the underlying boto3.

Returns:

A dictionary with two elements: url and fields. Url is the url to post to. Fields is a dictionary filled with the form fields and respective values to use when submitting the post.

Raises:

S3Error – If operation fails.

create_presigned_url_for_file(bucket: str, filename: str, expiration_time: int = 3600, **kwargs) str[source]

Creates a presigned URL for a file stored in Amazon S3. The URL can be used to access the file for a limited time. The URL expires after a fixed amount of time.

Parameters:
  • bucket – The name of the S3 bucket.

  • filename – The name of the file to retrieve.

  • expiration_time – The number of seconds until the URL expires. (Default: 3600).

  • kwargs – Any other param passed to the underlying boto3.

Returns:

The presigned URL.

Raises:

S3Error – If operation fails.

delete_file(bucket: str, filename: str, **kwargs) DeleteObjectOutputTypeDef[source]

Delete objects from Amazon S3.

Parameters:
  • bucket – The name of the S3 bucket.

  • filename – The name of the file to delete.

  • kwargs – Any other param passed to the underlying boto3.

Returns:

S3 delete response syntax.

Raises:

S3Error – If operation fails.

get_file(bucket: str, filename: str, **kwargs) GetObjectOutputTypeDef[source]

Retrieves objects from Amazon S3.

Parameters:
  • bucket – The name of the S3 bucket.

  • filename – The name of the file to retrieve.

  • kwargs – Any other param passed to the underlying boto3.

Returns:

The object stored in S3.

Raises:

S3Error – If operation fails.

list_files(bucket: str, folder_path: str = '', **kwargs) list[str][source]

List all the files in the bucket.

Parameters:
  • bucket – The name of the S3 bucket.

  • folder_path – The prefix to the path of the folder to list. Leave default to list all the files in the bucket. (Default: “”).

  • kwargs – Any other param passed to the underlying boto3.

Returns:

A list of the files in the bucket.

Raises:

S3Error – If operation fails.

store_file(bucket: str, filename: str, file: str | bytes | IO[Any], **kwargs) PutObjectOutputTypeDef[source]

Store objects to Amazon S3.

Parameters:
  • bucket – The name of the S3 bucket.

  • filename – The name of the file to retrieve.

  • file – The body of the file in bytes.

  • kwargs – Any other param passed to the underlying boto3.

Returns:

The object stored in S3.

Raises:

S3Error – If operation fails.