<@ULVA73B9P> How to manually write (put) a file in...
# marvin-in-the-wild
d
@Marvin How to manually write (put) a file in a bucket using S3 bucket block?
m
Here's how you can manually write (put) a file in an S3 bucket using the S3 bucket block in Prefect: 1. Create and configure your S3 bucket block:
Copy code
python
   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:
Copy code
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.