-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikhil Harithas
committed
Sep 5, 2023
1 parent
1cbe627
commit b5d2351
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import boto3 | ||
from io import BytesIO | ||
|
||
s3 = boto3.client("s3") | ||
|
||
def get_byte_stream_for_s3_path(s3_path: str) -> BytesIO: | ||
""" | ||
Load data directly into memory from S3 and return a byte stream. | ||
Parameters: | ||
s3_path (str): The S3 path to the file. | ||
Returns: | ||
BytesIO: A byte stream of the loaded data. | ||
""" | ||
bucket_name, s3_key = s3_path.replace("s3://", "").split("/", 1) | ||
s3_object = s3.get_object(Bucket=bucket_name, Key=s3_key) | ||
return BytesIO(s3_object["Body"].read()) | ||
|
||
def download_from_s3(s3_path: str, local_path: str) -> None: | ||
""" | ||
Download a file from S3 to a specified local path. | ||
Parameters: | ||
s3_path (str): The S3 path to the file. | ||
local_path (str): The local path where the file should be saved. | ||
""" | ||
bucket_name, s3_key = s3_path.replace("s3://", "").split("/", 1) | ||
s3.download_file(bucket_name, s3_key, local_path) |