将图像字节上传到cloudinary的正确方法 [英] Correct way for uploading image bytes to cloudinary

查看:143
本文介绍了将图像字节上传到cloudinary的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://cloudinary.com/documentation/image_upload_api_reference 作为参考。 p>

在两种情况下,我要将文件上传到cloudinary。


  1. 通过直接提供URL链接来上传图像。

  2. 通过从其他来源获取图像字节来上传图像字节。

我可以解决情况1,但在第二次遇到麻烦。我在下面粘贴我的代码流以供参考。

  import cloudinary 
import cloudinary.uploader

从io导入BytesIO
从StringIO导入StringIO

def upload_image_to_cloudinary(img_tag):

logging.debug(将映像上传到cloudinary:%s% img_tag)

如果'src'不在img_tag.attrs中:
del img_tag
返回

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()#返回str对象。
#img_src =字节IO(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)

返回img_tag

如果 img_src 是另一个API返回的图像斑点 str ,我将其传递为 cloudinary doc中提到的文件参数与任何外部图像URL的方式非常相似,例如: rel = nofollow noreferrer> https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAIkAAAAJGRhNzJiYjY1LTUxOTctNDI4NC1hOGIwLWQ1OTVlNmZlZmVmYw.jpg $ , 通用上传流程如何像boto f或s3,我检查以下回购代码。
引用了 https://github.com /boto/boto/blob/develop/boto/vendored/six.py#L633 也是如此。



错误日志:


上载无效的URL
回溯(最近一次调用为最新):
File / base / data / home / apps / s〜pagalguy- staging / namita:v1.397698162588746989 / articleslib / article_util.py,第68行,位于upload_images_n_publish
标签= image_util.upload_image_to_cloudinary(tag)
文件 / base / data / home / apps / s〜pagalguy -staging / namita:v1.397698162588746989 / api / image_util.py,第133行,位于upload_image_to_cloudinary中
crop = limit
File / base / data / home / apps / s〜pagalguy-staging /namita:v1.397698162588746989/libs/cloudinary/uploader.py,第23行,位于upload_image
结果= upload(file,** options)
文件 / base / data / home / apps / s〜pagalguy-stage / namita:v1.397698162162588746989 / libs / cloudinary / uploader .py,第17行,在上传
中,返回call_api( upload,params,file = file,** options)
File / base / data / home / apps / s〜pagalguy-staging /namita:v1.397698162588746989/libs/cloudinary/uploader.py,第226行,在call_api
中引发Error(result [ error] [ message])
错误:上载网址无效


最后我不知道哪种方法是将图像字节上传到cloudinary的正确方法。

解决方案

您的 img_src 参数,它表示 file ,应该使用字节数组缓冲区( bytearray )或Base64 URI填充。您可以尝试以下操作:

 并以open(img_src_str, rb)as imageFile:
f = imageFile.read ()
img_src = bytearray(f)

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


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.

解决方案

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,
      ...
    )

这篇关于将图像字节上传到cloudinary的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆