Created
September 24, 2020 00:28
-
-
Save raninho/80b8faddf2b000e526f4a5b7b461d1dc to your computer and use it in GitHub Desktop.
boto3 and s3
This file contains hidden or 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 | |
import json | |
from urllib.parse import urlparse | |
BUCKET_NAME = 'bucket-name' | |
session = boto3.Session( | |
aws_access_key_id="secret-id", | |
aws_secret_access_key="secret-access-key" | |
) | |
def get_key_from_s3_url(url): | |
url = urlparse(url) | |
return url.path.lstrip("/") | |
def get_content_s3(url, bucket="bucket-name"): | |
if url: | |
key = get_key_from_s3_url(url) | |
s3 = session.client("s3") | |
try: | |
obj = s3.get_object(Bucket=bucket, Key=key) | |
except Exception as err: | |
raise err | |
print(json.loads(obj["Body"].read().decode("utf-8"))) | |
if __name__ == "__main__": | |
get_content_s3('s3://filename.json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment