Created
September 29, 2020 21:47
-
-
Save anna-anisienia/2f1be5e22bd492d3141acbf4a29c4cc6 to your computer and use it in GitHub Desktop.
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 os | |
import contextlib | |
import boto3 | |
s3 = boto3.client('s3', aws_access_key_id='my_aws_access_key', | |
aws_secret_access_key='my_aws_secret_key', | |
region_name='eu-central-1') | |
@contextlib.contextmanager | |
def this_directory(path): | |
""" | |
Change the working dir to the path specified. Then, change back to the original one. | |
""" | |
original_workdir = os.getcwd() | |
os.chdir(path) | |
try: | |
yield | |
finally: | |
os.chdir(original_workdir) | |
# Usage: | |
with this_directory(path='../data'): | |
file = 'my_s3_file.csv' | |
s3.download_file(bucket='s3_bucket', key=file, filename=file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment