Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 10, 2018 18:30
Show Gist options
  • Save oldarmyc/af106f343006ada9f1b0d1eb7a6ba454 to your computer and use it in GitHub Desktop.
Save oldarmyc/af106f343006ada9f1b0d1eb7a6ba454 to your computer and use it in GitHub Desktop.
S3 example connection
"""
The s3fs package would need to be installed using conda install
conda install -c anaconda s3fs
"""
from s3fs.core import S3FileSystem
import configparser
"""
Credentials would need to be in ini format and look like the following:
[default]
aws_access_key_id=ACCESS_KEY_HERE
aws_secret_access_key=SECRET_ACCESS_KEY_HERE
Configparser is in the standard library and can be used to read the ini file
so that you can use the parts of it when setting up the s3 object below
"""
config = configparser.ConfigParser()
config.read('/var/run/secrets/user_credentials/aws_credentials')
# Set up the object using the credentials from the file
fs = S3FileSystem(
anon=False,
key=config.get('default', 'aws_access_key_id'),
secret=config.get('default', 'aws_secret_access_key')
)
# Provide the bucket and file name
bucket = 'test_bucket'
file_name = 'testing.txt'
# To list what is in the specified bucket
all_files = fs.ls(f'{bucket}')
print(all_files)
# To read the specified file in the named bucket
with fs.open(f'{bucket}/{file_name}', mode='rb') as f:
print(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment