Last active
October 10, 2023 21:31
-
-
Save nstankov-bg/50e6d12cda4389a298e368c3ef698842 to your computer and use it in GitHub Desktop.
Python3 Issue Gist
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
import boto3 | |
def download_s3_file(bucket_name, file_prefix, local_filename): | |
try: | |
s3_client = boto3.client('s3') | |
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=file_prefix) | |
if 'Contents' in response: | |
target_file = response['Contents'][0]['Key'] | |
local_path = f"/tmp/{local_filename}" | |
s3_client.download_file(bucket_name, target_file, local_path) | |
with open(local_path, 'r') as file: | |
content = file.read() | |
print(content) | |
else: | |
print(f"No file found starting with prefix {file_prefix}") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
download_s3_file('coderbytechallengesandbox', '__cb__', 'local_file.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment