Created
March 18, 2019 10:49
-
-
Save breeko/f3732001ce9051346926ca0260055001 to your computer and use it in GitHub Desktop.
Helper functions for aws 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 requests | |
from urllib.parse import urlparse | |
import os | |
import urllib.request | |
import shutil | |
def download_url(url, file_name): | |
""" Download the file from `url` and save it locally under `file_name`""" | |
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file: | |
shutil.copyfileobj(response, out_file) | |
return file_name | |
def get_image_name_from_url(url): | |
a = urlparse(url) | |
return os.path.basename(a.path) | |
def download_to_s3(s3_client, s3_bucket, url): | |
if not bucket_exists(s3_client, s3_bucket): | |
s3_client.create_bucket(Bucket=s3_bucket) | |
image_name = get_image_name_from_url(url) | |
image = download_url(url, "/tmp/{}".format(image_name)) | |
s3_client.upload_file(image, s3_bucket, image_name) | |
return {'S3Object': {'Bucket': s3_bucket,'Name': image_name}} | |
def delete_from_s3(s3_client, s3_bucket, file_name): | |
s3_client.delete_object(Bucket=s3_bucket, Key=file_name) | |
def bucket_exists(s3_client, s3_bucket): | |
return s3_bucket in [b.get("Name") for b in s3_client.list_buckets().get("Buckets")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment