Django提供的简易缩略图引发访问被拒绝错误 [英] Easy Thumbnail with Django raising access denied error
问题描述
我正在使用S3Boto3Storage将文档保存在aws s3中,并尝试使用易缩略图生成缩略图,请在下面找到代码
I'm using S3Boto3Storage to save docs in my aws s3 and tried to use easy-thumbnails to generate thumbnail images, please find the code below
模型类
class ThumbnailTestModel(models.Model):
sample1 = models.FileField(
storage=S3Boto3Storage(),
help_text="Field to store the sample document of Professional",
null=True,
blank=True,
upload_to=s3_professional_sample_storage_path)
sample1_file_name = models.CharField(blank=True,null=True,max_length=1000, default=True)
查看课程
class ThumbnailTestModelView(mixins.CreateModelMixin, mixins.ListModelMixin,
mixins.UpdateModelMixin, viewsets.GenericViewSet):
queryset = ThumbnailTestModel.objects.all()
permission_classes = (AllowAny, )
serializer_class = ThumbnailSerializer
和序列化
class ThumbnailSerializer(serializers.ModelSerializer):
sample1 = serializers.FileField(read_only=True, required=False, allow_null=True)
sample1_base64 = serializers.CharField(write_only=True, required=False, allow_null=True)
sample1_thumbnail = serializers.SerializerMethodField(required=False, read_only=True, allow_null=True)
class Meta:
model = ThumbnailTestModel
fields = ['id','sample1', 'sample1_file_name', 'sample1_base64', 'sample1_thumbnail']
def validate(self, validated_data):
validated_data = super(ProductProfessionalSerializer,
self).validate(validated_data)
sample1_base64 = validated_data.pop('sample1_base64', None)
if sample1_base64:
validated_data['sample1'] = ContentFile(
base64.b64decode(sample1_base64),
name=validated_data["sample1_file_name"])
def get_sample1_thumbnail(self, instance):
return AWS_URL + get_thumbnailer(instance.sample1)['avatar'].url
这是我得到的答复
[{"id":5,"sample1":" https://wizcounsel-dev.s3.amazonaws.com/sample_document/None/add_team_2.png ," sample1_file_name:" add_team_2.png," sample1_thumbnail:"
[{"id":5,"sample1":"https://wizcounsel-dev.s3.amazonaws.com/sample_document/None/add_team_2.png","sample1_file_name":"add_team_2.png","sample1_thumbnail":"https://wizcounsel-dev.s3.amazonaws.com/sample_document/None/add_team_2.png.150x100_q85_crop.png"}]
但是,访问生成的缩略图URL会返回拒绝访问错误,实际上同一文件夹中的所有对象都是公共的,在检查AWS文件夹中似乎没有缩略图文件时
However accessing the generated thumbnail url returns an access denied error, all objects in the same folder are in fact public, on inspecting the AWS folder doesn't seem to have the thumbnail file
我是Django的超级新手,因此问题可能看起来很幼稚,谢谢
I'm super new to Django and hence the question might appear naive, Thanks
推荐答案
显然,缩略图是在本地创建的,这是错误的原因,可以通过在设置中添加以下行来解决
Apparently the thumbnails were getting created locally and this was the cause of the error, fixed by adding the following line to settings
THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
这篇关于Django提供的简易缩略图引发访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!