I have a problem uploading images to DO Spaces resized with PILL

Viewed 34

I'm uploading images to Digital Ocean Spaces using boto3. It's working really good until I add PILL.

In django view I have this code when I get the image:

from digitalocean_spaces import DigitalOceanSpaces
from helpers import resize_maintain_its_aspect_ratio

def images_view(request):
    if request.method == "POST":
        images = request.FILES.getlist('images')
        for index, image in enumerate(images):
            size = image.size
            content_type = image.content_type
            file_name = image.name
            # TODO: fix method
            # image = resize_maintain_its_aspect_ratio(image, 500)
            DigitalOceanSpaces().upload_file(
                key=key, 
                file=image, 
                content_type=content_type, 
                acl='private'
            )

I can see all the information of each image.

To upload the image I use this method that is working too:

class DigitalOceanSpaces:

    def default_session_client(self):
        session = boto3.session.Session()
        client = session.client(
            's3',
            region_name=REGION_NAME,
            endpoint_url=ENDPOINT_URL,
            aws_access_key_id=ACCESS_KEY_ID,
            aws_secret_access_key=ACCESS_SECRET_KEY
        )
        return client

    def upload_file(self, key, file, content_type, acl='private'):
        client = self.default_session_client()
        client.put_object(
            Bucket=BUCKET_NAME,
            Key=key,
            Body=file,
            ACL=acl,
            ContentType=content_type,
            Metadata={
                'x-amz-meta-my-key': '*****'
            }
        )

The problem start when I call this another method to resize the image

from PIL import Image

def resize_maintain_its_aspect_ratio(image, base_width):
    pillow_image = Image.open(image)
    width_percent = (base_width / float(pillow_image.size[0]))
    height_size = int((float(pillow_image.size[1]) * float(width_percent)))
    resized_image = pillow_image.resize((base_width, height_size), Image.ANTIALIAS)
    return resized_image

I see the error even if resize_maintain_its_aspect_ratio method just have:

pillow_image = Image.open(image)

So, the error is:

  • An error occurred (BadDigest) when calling the PutObject operation (reached max retries: 4): Unknown

Does anyone know what the problem is ?

0 Answers
Related