Dzmitry-S
08/23/2024, 7:25 AMMarvin
08/23/2024, 7:25 AMpython
from prefect_aws import S3Bucket, AWSCredentials
s3_bucket = S3Bucket(
bucket_name="my_s3_bucket",
credentials=AWSCredentials.load("my_aws_credentials")
)
s3_bucket.save("my_s3_bucket")
2. Write data to the S3 bucket using the configured block:
python
from prefect import task, flow
import io
@task
def write_to_s3():
data = b"Hello, S3!"
s3_bucket = S3Bucket.load("my_s3_bucket")
# Define the key (path) where the file will be stored in the bucket
key = "path/to/my_file.txt"
# Perform the write operation
s3_bucket.credentials.get_boto3_session().client("s3").upload_fileobj(
Fileobj=io.BytesIO(data),
Bucket=s3_bucket.bucket_name,
Key=key
)
@flow
def my_flow():
write_to_s3()
if __name__ == "__main__":
my_flow()
This example first sets up the S3 bucket block, then defines a task to write data to the S3 bucket using boto3
. The flow orchestrates the execution of the task.
For more details, you can refer to the Prefect documentation here.