Wrong url with Django Sorl thumbnail with Amazon s3
Asked Answered
Q

1

11

I am having a very strange error in using sorl thumbnails in my django project which uses S3 for media files. I have done my settings as pointed out in this answer https://mcmap.net/q/1013679/-how-do-i-use-django-sorl-thumbnail-memcached-and-s3-together and in fact all my other media files are being generated correctly.

But for the images generated through sorl thumbnails I am getting a url like this

https://he-s3.s3.amazonaws.com/cache/6f/cb/6fcb83175cb63f754fba9facec5dda7f.jpg?Signature=tgDEXyRV3gl3QtK%2BvwliqAtWqpM%3D&Expires=1357853609&AWSAccessKeyId=AKIAJLE6MUHDYS3HN6YQ

The problem is strange because its appending the S3 storage path to the image url, but its not adding /media/ in between. If you check

https://he-s3.s3.amazonaws.com/media/cache/6f/cb/6fcb83175cb63f754fba9facec5dda7f.jpg

the image actually exists there, but since the url is being generated wrong, I am getting a broken image. In the settings file I have declared the DEFAULT_FILE_STORAGE using s3boto and it contains

S3_URL = 'http://he-s3.s3-website-ap-southeast-1.amazonaws.com'
MEDIA_URL = S3_URL + '/media/'

I fail to understand why does the path in the sorl thumbnail image not contain media.

Any help is appreciated.

Update

Instead of being solved, the problem has in fact compounded. What I did was that I cleared the KVStore in thumbnail and all the database was cleared. I was happy because I thought the problem was solved, I was getting the correct url now. But then I refreshed the page, and again I was getting the wrong url. I don't understand what's happening, if I clear the thumbnail db, it shows the correct url once and after that it again shows the wrong url.

Quinate answered 10/1, 2013 at 20:45 Comment(2)
The first are secure urls, first make sure the setting AWS_S3_SECURE_URLS is False, or the AWS_S3_CUSTOM_DOMAIN setting is setupIrkutsk
I also faced similar problem. If your DEFAULT_FILE_STORAGE is set as S3, you can keep MEDIA_URL = '/media/'. But add AWS setting AWS_LOCATION = '/media/'. I think this will solve your issue.Kourtneykovac
P
31

I'll bet that you're using something like this:

MediaS3BotoStorage = lambda: S3BotoStorage(location='media')

However this causes problems in sorl-thumbnail because it serializes the storage class into cache using the class name. Later when it deserializes, it instantiates as S3BotoStorage() without the location parameter. That's why it works the first time for you but then fails later.

You can fix it by using a full-fledged class instead of a factory:

class MediaS3BotoStorage(S3BotoStorage):
    location = 'media'

Hope that helps!

Phenformin answered 4/8, 2013 at 18:25 Comment(1)
If having this issue in 2017 with boto3==1.4.3 you'd better to use MediaRootS3BotoStorage instead.Surround

© 2022 - 2024 — McMap. All rights reserved.