Reputation: 81
I'm creating a website for my photography. The images (which are on the larger side) are being stored in an S3 bucket which does not have public access. The data related to the images, such as image name, location taken, and the S3 URL, is being stored in a PostgreSQL table.
When the client requests an image (or images), my plan was to query the table for the image object, grab the S3 URL, fetch the file using said S3 URL, and respond to the client with the jpeg (or jpegs) + the data from the postgres instance (or instances).
But now that I am at the point of being able to do that, I don't know how to do it. What is the response object going to look like to accomplish this? I'm used to serving up images through publicly available URLs and throwing that in an element or through static assets.
Edit: Should my S3 objects be public so that I can just display the images the way "I'm used to"? If so, how do I prevent an excess of reads from happening and driving up my bill? I see that AWS has built in throttling, but I'd like to throttle reads from individual clients, not reads to the bucket as a whole.
Upvotes: 0
Views: 225
Reputation: 633
This code was taken from the boto3 documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html
The output contains the url to the s3 file:
import boto3
from botocore.exceptions import ClientError
def create_presigned_url(bucket_name, object_name, expiration=3600):
"""Generate a presigned URL to share an S3 object
s3_client = boto3.client('s3')
try:
response = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket':bucket_name,'Key':object_name},
ExpiresIn=expiration
)
except ClientError as e:
return None
# The response contains the presigned URL
return response
Upvotes: 1