0
votes
  1. I am trying to upload an image file to cloudinary which I have sent from my django template to a function in views.py
  2. The file is in request.FILES['image']

            cloudinary.config(
                cloud_name="p*****",
                api_key="33************",
                api_secret="4***-S***_o*********"
            )
            img_obj = request.FILES['image']
            cloudinary_response = cloudinary.uploader.upload(img_obj)
            image_url = cloudinary_response['url']
    
  3. Printing img_obj gives the name of the image (Like : "tree.jpg")

  4. cloudinary upload doc is as follows https://cloudinary.com/documentation/image_upload_api_reference#upload

  5. The type of img_obj is InMemoryUploadedFile. Now is there a way to convert it to base64 or somthing like that so I can upload.

  6. Or any other solution ??

1

1 Answers

2
votes

You have a few options:

  1. img_obj.file is the actual file object, so you could try uploading that directly.
  2. Since an InMemoryUploadedFile is a subclass of File, you can also just open(mode='rb') the file, using standard python file io functions.
  3. Or you can try img_obj.file.read()

I would go for the second option:

import base64

with img_obj.open("rb") as image_file:
     encoded_string = base64.b64encode(image_file.read())