Reputation: 324
I have some images in my S3 bucket and I want to display them in my django application.
import boto3
s3=boto3.client('s3')
list=s3.list_objects(Bucket='my_bucket_name')['Contents']
for l in list:
print(l[u'Key'])
Using this code I'm getting the names of those images. How do I get the URL of the images, so that I can pass it to HTML pages to display it, using this same approach?
Upvotes: 0
Views: 982
Reputation: 726
URLs to access files in S3 follow this format:
https://<region>.amazonaws.com/<bucket-name>/<key>
See here: https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAPI.html
And then utilize the django Template language (see here: https://docs.djangoproject.com/en/2.0/ref/templates/language/#) to inject what you need.
If your buckets were stored in a list, and keys for for each bucket were stored in a dictionary, you could do something like this:
{% for bucket in buckets %}
{% for key in keys[bucket] %}
<a href="https://<region>.amazonaws.com/{{ bucket }}/{{ key }}">S3 link here</a>
{% endfor %}
{% endfor %}
Upvotes: 1