Created
August 14, 2024 17:57
-
-
Save joeharrison714/98ee3337c5a9a719305ba5242d4c3379 to your computer and use it in GitHub Desktop.
download_decompress_and_encode_s3_file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import base64 | |
import gzip | |
from io import BytesIO | |
def download_decompress_and_encode_s3_file(bucket_name, key): | |
# Initialize a session using Amazon S3 | |
s3 = boto3.client('s3') | |
# Download file content into memory | |
file_stream = BytesIO() | |
s3.download_fileobj(bucket_name, key, file_stream) | |
# Rewind the file stream to the beginning | |
file_stream.seek(0) | |
# Decompress the gzipped content | |
with gzip.GzipFile(fileobj=file_stream, mode='rb') as gz: | |
decompressed_content = gz.read() | |
# Encode the decompressed content to base64 | |
base64_encoded = base64.b64encode(decompressed_content).decode('utf-8') | |
return base64_encoded | |
# Example usage | |
bucket_name = 'your-bucket-name' | |
key = 'your-file-key.gz' | |
encoded_content = download_decompress_and_encode_s3_file(bucket_name, key) | |
print(encoded_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment