我用http://cloudinary.com/documentation/image_upload_api_reference作为参考。在
有两种情况下,我想将文件上传到cloudinary。在
我可以解决第一个案子,但第二个案子有麻烦。我在下面粘贴我的代码流以供参考。在
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
如果img_src
是另一个api返回的图像blob str
,我将其作为在cloudinary doc中提到的file
param,其传递方式与任何外部图像url(例如:https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAIkAAAAJGRhNzJiYjY1LTUxOTctNDI4NC1hOGIwLWQ1OTVlNmZlZmVmYw.jpg)非常相似
为了检查一般上传流如何像s3的boto一样工作,我检查了下面的repo代码。 也提到了https://github.com/boto/boto/blob/develop/boto/vendored/six.py#L633这个。在
错误日志:
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
最后,我不知道上传图片字节到cloudinary的正确方法是什么。在
表示
img_src
的img_src
参数应使用字节数组缓冲区(bytearray
)或Base64 URI填充。你可以尝试一下:相关问题 更多 >
编程相关推荐