question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

S3 Links Expire for Images

See original GitHub issue

My S3 image links are expiring after one hour. This is because you store the URL to the backend within the textbox as the location it stores the file at with the Signature/Expiration that was given as a return from S3. This signature expires after 1 hour. I can make the Signature last 7 days at max, but then we’re back to the same issue.

<img src="https://bucketname.s3.amazonaws.com/django-summernote/2020-03-04/5cb7c679-2143-434d-a6e2-5cec03225411.jpg?AWSAccessKeyId=AAAAAAAAAAAAAAAAAAAAAAA&amp;Signature=BvNIMcOJurphnCKZaarGQDDnHc8%3D&amp;Expires=1583302245" style="width: 25%;">

To solve this issue, couldn’t you just reference the image location from the Attachments saved to the database?

https://bucketname.s3.amazonaws.com/django-summernote/2020-03-04/5cb7c679-2143-434d-a6e2-5cec03225411.jpg?AWSAccessKeyId=AAAAAAAAAAAAAAAAAAAAAAA&Signature=VowPCxFZ9ZjMbb7WdnhoMYk46Q8%3D&Expires=1583351249

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
jasonhoward64commented, Jun 25, 2020

This is the code to resolve the issue. Been using this for a couple years.

pip install beautifulsoup4

At the top of views.py

from bs4 import BeautifulSoup 
from urllib.parse import urlsplit

Within your view:

    if form.is_valid():
        instance = form.save(commit=False)
           
        #remove s3 query info in media files uploaded to summernote. This is because the file name is not being updated when served and thus, the link expires if we don't do this.

        soup = BeautifulSoup(instance.body, "html.parser")
        for img in soup.find_all('img'):
            img_url = img['src']
            new_url = urlsplit(img_url)._replace(query=None).geturl()
            img['src'] = new_url
        instance.body = str(soup) 

        instance.save()

I should mention that the field ‘body’ is the field with the summernote widget.

0reactions
aleemnazercommented, Apr 30, 2022

Well, there is another simple way to do this. which is recommended by documentation.
just override django storage class like this.

from storages.backends.s3boto3 import S3Boto3Storage
class SummerNoteStorage(S3Boto3Storage):
    location = 'static'
    default_acl = 'public-read'
    querystring_auth=False

look at this option, it will do magic. querystring_auth=False
and then use this class in your settings file. like this.

SUMMERNOTE_CONFIG = {
    'attachment_absolute_uri': True,
    'attachment_storage_class': 'gradingly.storage_backends.SummerNoteStorage',
}

I hope this will be helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS S3 link expired after a few minutes - Stack Overflow
If you want to make the links to your images permanent and accessible from your website you have to make them public as...
Read more >
Presigned URL for Amazon S3 bucket expires before ...
I created a presigned URL for an Amazon Simple Storage Service (Amazon S3) bucket using a temporary token. However, the URL expired before...
Read more >
Expiring objects - Amazon Simple Storage Service
If you create an S3 Lifecycle expiration rule that causes objects that have been in S3 Standard-IA or S3 One Zone-IA storage for...
Read more >
After storing photos to s3 and getting urls back, urls expire
The url expires according to the expiry time specified in it. Currently Amplify doesnt provide an option of increasing the ttl for the...
Read more >
The workaround for extending the expiration date for ... - Medium
A user who does not have AWS credentials or permission to access an S3 object can be granted temporary access by using a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found