我在GAE / Python上使用Flask Web Framework。将文件上传到云存储后,我希望获得对该文件的引用,以便可以提供该文件。我无法让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
答案 0 :(得分: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