Django Sorl的错误url和Amazon s3的缩略图

时间:2022-07-07 23:02:36

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://*.com/a/12848650/538191 and in fact all my other media files are being generated correctly.

我在我的django项目中使用sorl缩略图时犯了一个非常奇怪的错误,这个项目将S3用于媒体文件。我已经完成了这个答案中所指出的设置,https://*.com/a/12848650/538191,事实上,我所有的其他媒体文件都被正确地生成了。

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

但是对于通过sorl缩略图生成的图像,我得到的url是这样的

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

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

这个问题很奇怪,因为它将S3存储路径附加到图像url,但不添加/media/在两者之间。如果你检查

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

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

图像确实存在,但是由于url生成错误,我得到的是一个损坏的图像。在设置文件中,我使用s3boto声明了DEFAULT_FILE_STORAGE并包含它。

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.

我不明白为什么sorl缩略图中的路径不包含媒体。

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.

问题非但没有得到解决,反而变得更加复杂。我所做的就是用缩略图清除KVStore,并清除所有数据库。我很高兴,因为我认为问题解决了,我现在得到了正确的url。然后我刷新了页面,再次得到了错误的url。我不明白发生了什么,如果我清除缩略图db,它会显示一次正确的url,然后它会再次显示错误的url。

1 个解决方案

#1


29  

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.

然而,这在sorl-thumbnail中导致了问题,因为它使用类名将存储类序列化为缓存。稍后当它反序列化时,它实例化为S3BotoStorage(),没有位置参数。这就是为什么它第一次对你有效,但之后就失败了。

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

您可以使用一个完整的类而不是工厂来修复它:

class MediaS3BotoStorage(S3BotoStorage):
    location = 'media'

Hope that helps!

希望会有帮助!

#1


29  

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.

然而,这在sorl-thumbnail中导致了问题,因为它使用类名将存储类序列化为缓存。稍后当它反序列化时,它实例化为S3BotoStorage(),没有位置参数。这就是为什么它第一次对你有效,但之后就失败了。

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

您可以使用一个完整的类而不是工厂来修复它:

class MediaS3BotoStorage(S3BotoStorage):
    location = 'media'

Hope that helps!

希望会有帮助!