【问题标题】:GAE + Cloud Storage - Unable to get FileInfo after File is uploadedGAE + 云存储 - 上传文件后无法获取 FileInfo
【发布时间】:2013-08-22 09:04:11
【问题描述】:

我在 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

【问题讨论】:

    标签: google-app-engine flask google-cloud-storage


    【解决方案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
    

    【讨论】:

    • 嘿,尼克,非常感谢!但是,你怎么不能使用fileInfo Class
    • parse_file_info 解析来自 cgi.FieldStorage 的数据。 Flask 只提供了一个 FileStorage,它更容易检索信息,而不是创建一个包含所有必要属性的 FieldStorage。
    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2016-12-29
    • 2012-10-15
    • 2012-07-15
    • 1970-01-01
    相关资源
    最近更新 更多