2013-08-22 5 views
0

GAE/Python에서 Flask Web Framework를 사용하고 있습니다. Cloud Storage에 파일을 업로드 한 후 해당 파일을 참조 할 수 있도록 파일에 대한 참조를 얻고 싶습니다. parse_file_info를 사용할 수 없습니다. 나는 길고 열심히 수색했으며 이틀 동안이 일을하려고 노력했다. 나는 지혜로 끝이야! 아래에서 내 핸들러를 볼 수 있습니다 :GAE + Cloud Storage - 파일 업로드 후 FileInfo를 가져올 수 없습니다.

@app.route('/upload_form', methods = ['GET']) 
def upload_form(): 
    blobupload_url = blobstore.create_upload_url('/upload', gs_bucket_name = 'mystorage')   
    return render_template('upload_form.html', blobupload_url = blobupload_url)  

@app.route('/upload', methods = ['POST']) 
def blobupload():  
    file_info = blobstore.parse_file_info(cgi.FieldStorage()['file']) 
    return file_info.gs_object_name 

답변

1

데이터는 blob을 업로드 한 후 검색 한 uploaded_file의 페이로드로 인코딩됩니다. 다음은 이름을 추출하는 방법에 대한 예제 코드입니다.

import email 
from google.appengine.api.blobstore import blobstore 

def extract_cloud_storage_meta_data(file_storage): 
    """ Exctract the cloud storage meta data from a file. """ 
    uploaded_headers = _format_email_headers(file_storage.read()) 
    storage_object_url = uploaded_headers.get(blobstore.CLOUD_STORAGE_OBJECT_HEADER, None) 
    return tuple(_split_storage_url(storage_object_url)) 

def _format_email_headers(raw_headers): 
    """ Returns an email message containing the headers from the raw_headers. """ 
    message = email.message.Message() 
    message.set_payload(raw_headers) 
    payload = message.get_payload(decode=True) 
    return email.message_from_string(payload) 

def _split_storage_url(storage_object_url): 
    """ Returns a list containing the bucket id and the object id. """ 
    return storage_object_url.split("/")[2:4] 

@app.route('/upload', methods = ['POST']) 
def blobupload():  
    uploaded_file = request.files['file'] 
    storage_meta_data = extract_cloud_storage_meta_data(uploaded_file) 
    bucket_name, object_name = storage_meta_data 
    return object_name 
+1

안녕하세요, Nik this, thanks for this this !! 하지만 어떻게 [fileInfo Class] (https://developers.google.com/appengine/docs/python/blobstore/fileinfoclass?hl=ko)를 사용할 수 없습니까? – jess

+0

parse_file_info는 cgi.FieldStorage의 데이터를 구문 분석합니다. Flask는 FileStorage만을 제공하며 필요한 모든 속성을 가진 FieldStorage를 만드는 대신 정보를 검색하는 것이 더 쉬웠습니다. –

관련 문제