S3Bucket
A storage bucket in AWS's S3 service.
Like all Resources, this class configures itself across multiple Environments.
For more information see the official documentation.
Example Usage
1import launchflow as lf
2
3# Automatically creates / connects to a S3 Bucket in your AWS account
4s3 = lf.aws.S3Bucket("my-bucket")
5
6# Quick utilities for reading and writing file contents
7s3.upload_from_string("file contents", "path/in/s3/file.txt")
8
9# You can also use the boto3 library directly
10bucket = s3.bucket()
11with open("my-file", "r") as f:
12 bucket.upload_fileobj(f, "path/in/s3/file.txt")
initialization
Create a new S3 bucket resource.
Args:
name (str)
: The name of the bucket. This must be globally unique.force_destroy (bool)
: If true, the bucket will be destroyed even if it contains objects.
bucket
1S3Bucket.bucket()
Get the AWS bucket resource returned by the boto3 library.
Returns:
- The AWS bucket object from the boto3 library.
upload_file
1S3Bucket.upload_file(to_upload: Union[str, IO], bucket_path: str)
Uploads a file to the S3 bucket.
Args:
to_upload (Union[str, IO])
: The file to upload. This can be a string representing the path to the file, or a file-like object.bucket_path (str)
: The path to upload the file to in the bucket.
Example usage:
1import launchflow as lf
2bucket = lf.aws.S3Bucket("my-bucket")
3bucket.upload_file("my-file.txt", "my-file.txt")
4bucket.upload_file(open("my-file.txt", "r"), "my-file.txt")
upload_from_string
1S3Bucket.upload_from_string(to_upload: str, bucket_path: str)
Uploads a string to the S3 bucket.
Args:
to_upload (str)
: The string to upload.bucket_path (str)
: The path to upload the string to in the bucket.
Example usage:
1import launchflow as lf
2
3bucket = lf.aws.S3Bucket("my-bucket")
4bucket.upload_from_string("hello", "hello.txt")
download_file
1S3Bucket.download_file(bucket_path: str)
Downloads a file from the S3 bucket.
Args:
bucket_path (str)
: The path to the file in the bucket.
Returns:
- The contents of the file as bytes.
Example usage:
1import launchflow as lf
2bucket = lf.aws.S3Bucket("my-bucket")
3with open("my-file.txt", "w") as f:
4 f.write(bucket.download_file("my-file.txt"))