0

我在 GAE/Python 上使用 Flask Web 框架。将文件上传到 Cloud Storage 后,我想获得对该文件的引用,以便提供该文件。我无法让 parse_file_info 工作。我已经进行了漫长而艰苦的搜索,并花了两天多的时间试图完成这项工作。我已经无计可施了!!你可以在下面看到我的处理程序:

@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
4

1 回答 1

1

数据在上传 blob 后检索的 upload_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
于 2013-09-07T09:49:41.873 回答