无法使用post方法将文件上传到Google云端存储

use*_*868 2 google-cloud-storage

以下是我的代码.我复制了可互操作的访问密钥..来自同一页面的客户端和秘密.我在ClientAccess ID中使用客户端密钥以形式和秘密进行散列.但我得到错误说无效的论点.

详细错误显示为"无法使用POST创建存储桶"

下面是我使用的python代码.

import webapp2
import cgi
import datetime
import urllib
import base64
import hmac, hashlib
import sha


policy_document = '''{"expiration": "2016-06-16T11:11:11Z",
                    "conditions": [
                        ["starts-with", "$key", "" ],
                        {"acl": "public-read" },
                    ]
                }'''


policy = base64.b64encode(policy_document).strip()
h = hmac.new('xxx', digestmod=sha.sha)
h.update(policy)
signature = base64.b64encode(h.digest())


class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('<html><body>')
        self.response.write('<form action="http://storage.googleapis.com/www.xyz.com" method="post" enctype="multipart/form-data">')
        self.response.write('<input type="text" name="key" value="">')
        self.response.write('<input type="hidden" name="bucket" value="www.xyz.com">')
        #self.response.write('<input type="hidden" name="Content-Type" value="image/jpeg">')
        self.response.write('<input type="hidden" name="GoogleAccessId" value="GOOGxxxxx">')
        self.response.write('<input type="hidden" name="acl" value="public-read">')
        self.response.write('<input type="hidden" name="success_action_redirect" value="http://www.linklip.com/storage.html">')
        self.response.write('<input type="hidden" name="policy" value="%s">' % policy )
        self.response.write('<input type="hidden" name="signature" value="%s">' % signature )
        self.response.write('<input name="file" type="file">')
        self.response.write('<input type="submit" value="Upload">')
        self.response.write('</form>')
        self.response.write('</body></html>')


app = webapp2.WSGIApplication([
    ('/', MainHandler)
    ], debug=True)
Run Code Online (Sandbox Code Playgroud)

Ale*_*xis 6

我有同样的问题,在尝试尽可能接近文档后,我发现错误的是我的文件输入是表单的第一个子而不是最后.

难道工作:

<form action="https://***.storage.googleapis.com" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="hidden" name="signature" value="***" />
    <input type="hidden" name="key" value="test/alexis.png" />
    <input type="hidden" name="policy" value="***" />
    <input type="hidden" name="Content-Type" value="image/png" />
    <input type="hidden" name="GoogleAccessId" value="***@developer.gserviceaccount.com" />
    <input type="hidden" name="bucket" value="***" />
    <input type="hidden" name="success_action_status" value="201" />
    <input type="submit" value="Upload">
</form>
Run Code Online (Sandbox Code Playgroud)

作品:

<form action="https://***.storage.googleapis.com" method="post" enctype="multipart/form-data">
    <input type="hidden" name="signature" value="***" />
    <input type="hidden" name="key" value="test/alexis.png" />
    <input type="hidden" name="policy" value="***" />
    <input type="hidden" name="Content-Type" value="image/png" />
    <input type="hidden" name="GoogleAccessId" value="***@developer.gserviceaccount.com" />
    <input type="hidden" name="bucket" value="***" />
    <input type="hidden" name="success_action_status" value="201" />
    <input type="file" name="file" />
    <input type="submit" value="Upload">
</form>
Run Code Online (Sandbox Code Playgroud)

我仍然对为什么它以这种方式工作而不是其他方式感到困惑......

  • 老兄,您救了我:)我已经尝试解决这个问题了将近6个月。幸运的是我找到了你的帖子。很高兴 :) (2认同)