Django - saving image to Supabase bucket, but image is blank

I'm uploading image through Angular - django - Supabase s3. To send image to Supabase bucket im using django-storages module. When i get image from supabase image gets corrupted with additional bytes.

Here is my settings of django-storages

STORAGES = {
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
    "default": {
        "BACKEND": "storages.backends.s3.S3Storage",
        "OPTIONS": {
            "bucket_name": os.getenv('SUPABASE_STORAGE_BUCKET_NAME'),
            "region_name": os.getenv('SUPABASE_S3_REGION_NAME'),
            "access_key": os.getenv('SUPABASE_S3_ACCESS_KEY_ID'),
            "secret_key": os.getenv('SUPABASE_S3_SECRET_ACCESS_KEY'),
            "endpoint_url": os.getenv('SUPABASE_S3_ENDPOINT_URL')
        },
    }
}

Here is my django code of image upload method of Viewset.

@action(detail=True, methods=['post'], url_path='image', parser_classes=[FormParser, MultiPartParser])
    def image_upload(self, request, pk=None):
        project = self.get_object()
        image = request.FILES['image']
        
        project.image = image
        project.save()
        projectReadSerializer = ProjectReadSerializer(project)
        return Response(data = projectReadSerializer.data)

whenever I post the image, I get blank image from the supabase bucket, which has more bytes than the original.

Any solutions?

I've tried to download the image, here is the result: original and bucket image

I've also checked the image using PIL and it seemed fine:

def image_upload(self, request, pk=None):
        project = self.get_object()
        image = request.FILES['image']

        img = Image.open(image)
        img.show()

        project.image = image
        project.save()
        projectReadSerializer = ProjectReadSerializer(project)
        return Response(data = projectReadSerializer.data)

so the problem is in project.image = image part of code, as it seems.

So, I found the solution. The problem was that boto3 of version 1.36 corrupted the file, by adding x-amz-checksum-crc32 to the end of it. I downgraded the boto3 by using pip install "boto3<1.36" and everything works fine!

Вернуться на верх