Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

django - Django提供的简易缩略图引发访问被拒绝错误(Easy Thumbnail with Django raising access denied error)

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

(我正在使用S3Boto3Storage将文档保存在aws s3中,并尝试使用易缩略图生成缩略图,请在下面找到代码)

Model class

(模型类)

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)

View class

(查看课程)

class ThumbnailTestModelView(mixins.CreateModelMixin, mixins.ListModelMixin,
            mixins.UpdateModelMixin, viewsets.GenericViewSet):
queryset = ThumbnailTestModel.objects.all()
permission_classes = (AllowAny, )
serializer_class = ThumbnailSerializer

and the serialize

(和序列化)

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

Here's the response I get

(这是我得到的回应)

[{"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 "}]

([{“ 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 ”}“)

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

(但是,访问生成的缩略图URL会返回拒绝访问错误,同一文件夹中的所有对象实际上都是公共的,在检查AWS文件夹时似乎没有缩略图文件)

I'm super new to Django and hence the question might appear naive, Thanks

(我是Django的超级新手,因此问题可能看起来很幼稚,谢谢)

  ask by Royce Raju Beena translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

显然是在本地创建了缩略图,这是错误的原因,已通过在设置中添加以下行来解决

THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...