2013-06-20 1 views
3

파일을 BLOBSTORE에 직접 쓸 수있게 해주는 API는 현재는 더 이상 사용되지 않으며 향후 삭제 될 예정입니다. 하지만이 API를 사용하여 외부 서비스의 사용자 사진을 저장합니다. 이 작업을 수행하려면 업로드 처리기를 사용할 수 없습니다.파일없이 urlfetch에서 이미지 저장 API

파일 API를 제거 할 때 BLOBSTORE에 직접 파일을 쓰는 다른 방법이 있습니까?

https://developers.google.com/appengine/docs/python/blobstore/#Writing_Files_to_the_Blobstore

+0

빨간색 상자에있는 메모는 혼란 스럽습니까? BLOBstore에 쓰는 것은 더 이상 사용되지 않으므로 Cloud Storage에 대신 써야합니다. –

+0

추가 라이브러리를 사용하고 싶지 않습니다. (GCS) –

+0

당신은 선택의 여지가 많지 않습니다. – Linuxios

답변

2

여기 내 문제를 해결하는 예입니다. 이제는 비례 파일 API를 피하기 위해 프로그래밍 방식으로 파일을 업로드 할 수 있습니다.

class BlobstoreUpload(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
    upload_files = self.get_uploads('file') 
    blob_info = upload_files[0] 
    return self.response.write(blob_info.key()) 

    @classmethod 
    def encode_multipart_formdata(cls, fields, files, mimetype='image/png'): 
    """ 
    Args: 
     fields: A sequence of (name, value) elements for regular form fields. 
     files: A sequence of (name, filename, value) elements for data to be 
     uploaded as files. 

    Returns: 
     A sequence of (content_type, body) ready for urlfetch. 
    """ 
    boundary = 'paLp12Buasdasd40tcxAp97curasdaSt40bqweastfarcUNIQUE_STRING' 
    crlf = '\r\n' 
    line = [] 
    for (key, value) in fields: 
     line.append('--' + boundary) 
     line.append('Content-Disposition: form-data; name="%s"' % key) 
     line.append('') 
     line.append(value) 
    for (key, filename, value) in files: 
     line.append('--' + boundary) 
     line.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)) 
     line.append('Content-Type: %s' % mimetype) 
     line.append('') 
     line.append(value) 
    line.append('--%s--' % boundary) 
    line.append('') 
    body = crlf.join(line) 
    content_type = 'multipart/form-data; boundary=%s' % boundary 
    return content_type, body 


class UserProfile(webapp2.RequestHandler): 
    def post(self): 
    picture = self.request.POST.get('picture') 

    # Write new picture to blob 
    content_type, body = BlobstoreUpload.encode_multipart_formdata(
     [], [('file', name, image)]) 
    response = urlfetch.fetch(
     url=blobstore.create_upload_url(self.uri_for('blobstore-upload')), 
     payload=body, 
     method=urlfetch.POST, 
     headers={'Content-Type': content_type}, 
     deadline=30 
    ) 
    blob_key = response.content 
+0

예제 코드보다 큼. blob을 청크에 게시 할 수 있습니까? – voscausa

+0

@voscausa 왜 이것을 필요로합니까? 그러나 나는 파일을 청크로 "분할"하고 각각을 다른 BLOB에 넣는 것이 가능하다고 생각한다. 또는 chunk1을 넣으려면 chunk2를 단일 BLOB에 넣으시겠습니까? –

+0

큰 얼룩을 읽고 버퍼링하고 게시/업로드해야합니다. 이제는 파일 API 및 작업을 사용하여 blob 청크를 작성합니다. – voscausa