Easy Thumbnail with Django raising access denied e

2020-04-16 05:30发布

问题:

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

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

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

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

回答1:

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'