Example functions for reading and writing from an S3 bucket
Install and configure the AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
This Python script includes a function called read_file_from_s3 that reads a file from an Amazon S3 bucket and saves it to a local directory. Here are the main steps of the function:
- The read_file_from_s3 function takes three parameters: s3_bucket_name, s3_object_key, and local_directory.
- The function creates an S3 client using boto3.
- It uses the get_object method of the S3 client to retrieve the file from the specified S3 bucket with the specified object key.
- It constructs the local file path by joining the local_directory and s3_object_key parameters using the os.path.join method.
- It writes the contents of the file to the local directory using the write method of a file object.
- If successful, it prints a success message with the local file path where the file was saved.
- If unsuccessful, it prints an error message.
- The script also includes an example usage of the read_file_from_s3 function, where the function is called with appropriate parameters to read a file from an S3 bucket and save it to a local directory.
This Python script includes a function called write_file_to_s3 that uploads a local file to an Amazon S3 bucket. Here are the main steps of the function:
- The write_file_to_s3 function takes two parameters: local_file_path and s3_bucket_name.
- The function creates an S3 client using boto3.
- It uses the os.path.basename method to get the file name from the local_file_path parameter.
- It uses the upload_file method of the S3 client to upload the file to the specified S3 bucket with the same object key as the local file name.
- If successful, it prints a success message.
- If unsuccessful, it prints an error message.
- The script also includes an example usage of the write_file_to_s3 function, where the function is called with appropriate parameters to write a local file to an S3 bucket.