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

时间:2013-05-04 20:35:10

标签: 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)

4 个答案:

答案 0 :(得分: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>

<强>作品

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

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

答案 1 :(得分:0)

要创建存储桶,您需要执行HTTP PUT:

https://developers.google.com/storage/docs/reference-methods#putbucket

您的表单方法设置为“POST”。

答案 2 :(得分:0)

我找到了上述问题的答案。

self.response.write('<input type="hidden" name="bucket" value="www.xyz.com">')

在值中,我尝试了value =“Bucket / filename-i-want-to-put”,我能够看到存储桶中创建的文件。

答案 3 :(得分:0)

我的解决方案是添加<input type="hidden" name="key" value="${filename}">

<form action="http://container-name.storage.googleapis.com" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="acl" value="public-read">
    <input type="hidden" name="Content-Type" value="application/pdf">
    <input type="hidden" name="key" value="${filename}">
    <input type="hidden" name="bucket" value="container-name">
    <input type="hidden" name="GoogleAccessId" value="XXXXXXXXX">
    <input type="hidden" name="policy" value="XXXXXXXXXXXXXXXXXXXXXXX=">
    <input type="hidden" name="signature" value="XXXXXX==">
    <input type="file" name="file" />
    <input type="submit" value="Upload">
<form>

请参阅Upload file using POST/PUT Object of Google Cloud Storage HTML Form

相关问题