简体   繁体   English

将文件上传到Appengine上的Google Cloud Storage(Python)

[英]File Upload To Google Cloud Storage on Appengine(Python)

I have been trying to upload a file to google cloud storage from my google appengine application. 我一直在尝试从我的Google Appengine应用程序将文件上传到Google云存储。 but i keep getting this error message: 但我不断收到此错误消息:

File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/cgi.py", line 540, in __getitem__
raise KeyError, key KeyError: 'files'

This is my client side upload form: 这是我的客户端上传表格:

<form id="fileupload" action="/uploads" method="POST" enctype="multipart/form-data">
    <!-- Redirect browsers with JavaScript disabled to the origin page -->
    <noscript><input type="hidden" name="redirect" value="http://2479ja.tv/jqueryupload"></noscript>
    <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
    <div class="row fileupload-buttonbar">
        <div class="col-lg-7">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="glyphicon glyphicon-plus"></i>
                <span>Add files...</span>
                <input type="file" name="files" value="" required="required">
            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="glyphicon glyphicon-upload"></i>
                <span>Start upload</span>
            </button>
            <button type="reset" class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel upload</span>
            </button>
            <button type="button" class="btn btn-danger delete">
                <i class="glyphicon glyphicon-trash"></i>
                <span>Delete</span>
            </button>
            <input type="checkbox" class="toggle" value="">
            <!-- The global file processing state -->
            <span class="fileupload-process"></span>
        </div>
        <!-- The global progress state -->
        <div class="col-lg-5 fileupload-progress fade">
            <!-- The global progress bar -->
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                <div class="progress-bar progress-bar-success" style="width:0%;"></div>
            </div>
            <!-- The extended global progress state -->
            <div class="progress-extended">&nbsp;</div>
        </div>
    </div>
    <!-- The table listing the files available for upload/download -->
    <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
</form>**

This is my server side python script: 这是我的服务器端python脚本:

class UploadHandler2(webapp.RequestHandler):

  def PrintWithCarriageReturn(s):
     sys.stdout.write('\r' + s)
     sys.stdout.flush()
  def post(self):
 form = cgi.FieldStorage(keep_blank_values = 1)
     print form.keys()
     fileitem = form['files']
 if fileitem.filename:
       filename = os.path.basename(fileitem.filename)**

Can someone please advise what is wrong with this code? 有人可以告知此代码有什么问题吗? Thanks 谢谢

You're using webapp to handle your requests, it looks like your cgi.FieldStorage isn't receiving the data, so there is no 'files' in your form object, hence the KeyError. 您正在使用webapp来处理您的请求,看起来您的cgi.FieldStorage没有收到数据,因此表单对象中没有“文件”,因此没有KeyError。

For webapp, you can just do: 对于webapp,您可以执行以下操作:

def post(self):
    fileitem = self.request.POST.get('files', None)

    fileblob = fileitem.file.read()
    filename = fileitem.filename
    mimetype = fileitem.type

(With the appropriate error checks for None, of course). (当然,通过适当的错误检查是否为None)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM