Skip to content

Instantly share code, notes, and snippets.

@2tony2
Created July 5, 2024 16:14
Show Gist options
  • Save 2tony2/b4919e1e937c7cb38e192173efd85bd3 to your computer and use it in GitHub Desktop.
Save 2tony2/b4919e1e937c7cb38e192173efd85bd3 to your computer and use it in GitHub Desktop.
import s3fs
def stream_data_from_s3(bucket_name, file_path, chunk_size=1024):
fs = s3fs.S3FileSystem()
with fs.open(f'{bucket_name}/{file_path}', 'rb') as file:
while chunk := file.read(chunk_size):
yield chunk
# Example usage
bucket_name = 'my-bucket'
file_path = 'large_file.csv'
for chunk in stream_data_from_s3(bucket_name, file_path):
# Process each chunk
print(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment