Correct way for uploading image bytes to cloudinary

我只是一个虾纸丫 提交于 2020-01-07 04:01:31

问题


I am using http://cloudinary.com/documentation/image_upload_api_reference as reference.

There are two cases in which I want to upload the files to cloudinary.

  1. Upload image by directly giving url link.
  2. Upload image bytes by taking them from different source.

I could solve case 1, but had trouble in 2nd. I am pasting my code flow below for reference.

import cloudinary
import cloudinary.uploader

from io import BytesIO
from StringIO import StringIO

def upload_image_to_cloudinary(img_tag):

  logging.debug("Uploading Image to cloudinary : %s"%img_tag)

  if 'src' not in img_tag.attrs:
    del img_tag
    return

  img_src = img_tag['src']

  if img_src.startswith('/blob'):

    quip_client = pgquip.get_client()
    blob_ids = img_src.split('/')
    blob_response = quip_client.get_blob(blob_ids[2], blob_ids[3])

    img_src_str = blob_response.read()  # this returns str object.
    # img_src = BytesIO(img_src_str)
    img_src = StringIO(img_src_str)

  cloudinary_response = cloudinary.uploader.upload_image(
    img_src,
    use_filename=True,
    folder="/pagalguy/articles",
    width=546,
    crop="limit"
  )

  img_tag['src'] = cloudinary_response.metadata.get("url")

  return img_tag

In case where img_src is a image blob str returned by another api, I passed it as file param mentioned in cloudinary doc in a very similar way as any external image url for eg: https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAIkAAAAJGRhNzJiYjY1LTUxOTctNDI4NC1hOGIwLWQ1OTVlNmZlZmVmYw.jpg

And, for checking how generic upload flows work like boto for s3, I check below repo code. Refered https://github.com/boto/boto/blob/develop/boto/vendored/six.py#L633 this too.

Error Log:

Invalid URL for upload Traceback (most recent call last): File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/articleslib/article_util.py", line 68, in upload_images_n_publish tag = image_util.upload_image_to_cloudinary(tag) File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/api/image_util.py", line 133, in upload_image_to_cloudinary crop="limit" File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 23, in upload_image result = upload(file, **options) File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 17, in upload return call_api("upload", params, file = file, **options) File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 226, in call_api raise Error(result["error"]["message"]) Error: Invalid URL for upload

Finally I don't know which is the correct way to upload image bytes to cloudinary.


回答1:


Your img_src parameter, which represents file, should be populated with either a byte array buffer (bytearray) or a Base64 URI. You can try something like:

    with open(img_src_str, "rb") as imageFile:
      f = imageFile.read()
      img_src = bytearray(f)

    cloudinary_response = cloudinary.uploader.upload(
      img_src,
      ...
    )


来源:https://stackoverflow.com/questions/41098273/correct-way-for-uploading-image-bytes-to-cloudinary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!