Skip to content

Instantly share code, notes, and snippets.

@VisionOra
Last active July 25, 2022 15:58
Show Gist options
  • Save VisionOra/fb7ad13c28eca85a4ff01bbc70d2eb53 to your computer and use it in GitHub Desktop.
Save VisionOra/fb7ad13c28eca85a4ff01bbc70d2eb53 to your computer and use it in GitHub Desktop.
Upload images to private s3 bucket
import boto3
from botocore.exceptions import NoCredentialsError
import requests
import mimetypes
import time
import random
AWS_STORAGE_BUCKET_NAME = "backend-s3-assets"
AWS_S3_ACCESS_KEY_ID = "*********"
AWS_S3_SECRET_ACCESS_KEY = "************************************"
region_name = '*********'
def get_resource_url(filenames):
"""
Get the resource url which is available in s3 bucket.
If your url expries then try to hit again to get a new one
********************************************************
[For private buckets]
Input:
filenames (list): List of filenames
Output:
list: list of urls
"""
s3 = boto3.client('s3', aws_access_key_id=AWS_S3_ACCESS_KEY_ID, aws_secret_access_key=AWS_S3_SECRET_ACCESS_KEY, region_name= region_name)
urls = []
for filename in filenames:
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': AWS_STORAGE_BUCKET_NAME,
'Key': filename
}
)
urls.append(url)
return urls
def upload_file(remote_url):
"""Upload image to the aws, we are using presigned urls and they have a expire time. So when ever you
need a url please call get_resource_url api.
Args:
remote_url (Str): Url of the image
bucket (Str): Bucket Name
Returns:
Str: URl of the image if uploaded sucessfully
"""
# Making s3 client
s3 = boto3.client('s3', aws_access_key_id=AWS_S3_ACCESS_KEY_ID, aws_secret_access_key=AWS_S3_SECRET_ACCESS_KEY, region_name= region_name)
# Making random file name
filename = str(int(time.time())) + "/" + str(int(random.random() * 1000000))
try:
# Getting image from remote url
imageResponse = requests.get(remote_url, stream=True).raw
# Get the content of image
content_type = imageResponse.headers['content-type']
# Getting the extension of the image
filename = filename + "." +remote_url.rsplit(".", 1)[-1]
# Uploading
s3.upload_fileobj(imageResponse, AWS_STORAGE_BUCKET_NAME, filename)
print("Upload Successful")
return filename
except FileNotFoundError:
print("The file was not found")
return remote_url
except NoCredentialsError:
print("Credentials not available")
return remote_url
url = 'https://img.alicdn.com/imgextra/i2/196993935/O1CN017MMKfT1ewHGMdGhH1_!!196993935.jpg'
filename = upload_file(url)
get_url = get_resource_url([filename])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment