Upload files to Google cloud storage from appengine app

前端 未结 2 1035
孤街浪徒
孤街浪徒 2021-01-02 23:40

I\'m sure the answer to this question is easy, but for me it\'s proven to be very frustrating since I can\'t put any solution I\'ve found into practical code for my own use.

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-02 23:59

    I have created a gist showing how to upload and use GCS files: https://gist.github.com/voscausa/9541133

    This is the code to handle the multipart form post:

    class GcsUpload(webapp2.RequestHandler):
    
        def post(self):
    
            field_storage = self.request.POST.get("file", None)
            if isinstance(field_storage, cgi.FieldStorage):
                file_name = field_storage.filename
    
                dyn = gcs_data.Dynamics(id=file_name, filename=file_name)
                gcs_file_name = gcs_data.gcs_write_blob(dyn, field_storage.file.read())
                gcs_data.gcs_serving_url(dyn)
                dyn.put()
                logging.info('Uploaded and saved in default GCS bucket : ' + gcs_file_name)
    
                self.response.headers[b'Content-Type'] = gcs_data.gcs_content_type(dyn)
                self.response.write(gcs_data.gcs_read_blob(dyn))
    
            else:
                logging.error('GCS Upload failed')
    

提交回复
热议问题