Skip to content

Instantly share code, notes, and snippets.

@chandruscm
Created September 13, 2021 21:27
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 chandruscm/a91db4f9397a5ed4c9c90cf075fc3bb0 to your computer and use it in GitHub Desktop.
Save chandruscm/a91db4f9397a5ed4c9c90cf075fc3bb0 to your computer and use it in GitHub Desktop.
Upload a valid image to an s3 bucket with its url using the requests package. Catches corrupt images using the pillow package.
from copy import deepcopy
import boto3
import requests
import environ
from PIL import Image
import io
env = environ.Env()
# Download an image with the url and store it in s3.
def upload_image_with_url(url, filepath):
try:
headers = {
"User-Agent": "Mozilla",
}
request = requests.get(url, stream=True, headers=headers, timeout=5)
# Checks for resource not found errors.
request.raise_for_status()
image = Image.open(request.raw)
# Grab the image buffer to make a deepcopy.
# - Because image.verify invalidates the buffer and reference.
# - Cannot deepcopy request.raw because it cannot be serialized.
image_buffer = io.BytesIO()
image.save(image_buffer, format=image.format)
image_buffer.seek(0)
image_buffer_copy = deepcopy(image_buffer)
# Image needs to be opened again after save.
image = Image.open(image_buffer)
extension = image.format.lower()
# Check if image is corrupted.
image.verify()
session = boto3.Session()
s3 = session.resource("s3")
# Construct the filepath with the extension.
filepath = filepath + "." + extension
bucket = s3.Bucket(env("AWS_STORAGE_BUCKET_NAME"))
bucket.upload_fileobj(image_buffer_copy, filepath)
image.close()
except Exception as e:
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment