Skip to content

Instantly share code, notes, and snippets.

@nstankov-bg
Last active October 10, 2023 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nstankov-bg/50e6d12cda4389a298e368c3ef698842 to your computer and use it in GitHub Desktop.
Save nstankov-bg/50e6d12cda4389a298e368c3ef698842 to your computer and use it in GitHub Desktop.
Python3 Issue Gist
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