2016-12-12 3 views
0

참고로 http://cloudinary.com/documentation/image_upload_api_reference을 사용하고 있습니다.이미지 바이트를 흐리게 업로드하는 올바른 방법

두 가지 경우로 파일을 클라우드에 업로드하려고합니다.

  1. 직접 URL 링크를 제공하여 이미지를 업로드하십시오.
  2. 이미지 바이트를 다른 원본에서 가져 와서 업로드합니다.

사례 1은 해결할 수 있지만 2 단계에서는 문제가 있습니다. 아래에 내 코드 흐름을 참조 용으로 붙여 넣습니다. img_src 다른 API에 의해 반환 된 이미지 덩어리 str 인 경우

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 

, 내가 예에 대한 외부 이미지 URL로 매우 유사한 방법으로 cloudinary 문서에 언급 된 file PARAM로 전달 : 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에 이미지 바이트를 업로드하는 올바른 방법이다 모른다.

+0

그 중 "작동하지 않는"정의는 무엇입니까? –

+0

이'StringIO.StringIO (img_src)'및'io.BytesIO (img_src)'를 전달하려고 시도했지만 작동하지 않았습니다. 그리고'upload_image'와'upload' 정의를 시도했습니다. –

+1

큰 소리로 철자를 쓰는 경우 : "작동하지 않음"이란 정의는 무엇입니까? "작동하지 않습니다"는 문제에 대한 가장 쓸모없는 설명입니다. 어떤 도움을 받기를 희망한다면, 당신이 예상했던 것과 정확히 무슨 일이 일어 났는지 기술 해주십시오. –

답변

0

img_src 매개 변수는 file을 나타내며 바이트 배열 버퍼 (bytearray) 또는 Base64 URI로 채워야합니다. 다음과 같이 시도 할 수 있습니다 :

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

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

1. 예가 있습니다 : https://github.com/cloudinary/pycloudinary/blob/master/cloudinary/uploader.py#L43 2. 전달 된 객체가 파일과 유사한 객체 (호출 가능한 '읽기' 속성) 파일로 사용 cf https://github.com/cloudinary/pycloudinary/blob/master/cloudinary/uploader.py#L234 –

+0

'upload_iamge' 메서드를 가리켜 주셔서 감사합니다. 완전히 놓쳤습니다. –