带有 Django 的简单缩略图引发访问被拒绝错误
Easy Thumbnail with Django raising access denied error
我正在使用 S3Boto3Storage 将文档保存在我的 aws s3 中,并尝试使用 easy-thumbnails 生成缩略图,请在下面找到代码
型号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)
查看class
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":"https://wizcounsel-dev.s3.amazonaws.com/sample_document/None/add_team_2.png.150x100_q85_crop.png"}]
但是访问生成的缩略图 url returns 访问被拒绝错误,同一文件夹中的所有对象实际上 public,在检查 AWS 文件夹时似乎没有有缩略图文件
我是 Django 的超级新手,因此这个问题可能显得幼稚,谢谢
显然缩略图是在本地创建的,这是错误的原因,通过在设置中添加以下行来修复
THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
我正在使用 S3Boto3Storage 将文档保存在我的 aws s3 中,并尝试使用 easy-thumbnails 生成缩略图,请在下面找到代码
型号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)
查看class
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":"https://wizcounsel-dev.s3.amazonaws.com/sample_document/None/add_team_2.png.150x100_q85_crop.png"}]
但是访问生成的缩略图 url returns 访问被拒绝错误,同一文件夹中的所有对象实际上 public,在检查 AWS 文件夹时似乎没有有缩略图文件
我是 Django 的超级新手,因此这个问题可能显得幼稚,谢谢
显然缩略图是在本地创建的,这是错误的原因,通过在设置中添加以下行来修复
THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'