代码之家  ›  专栏  ›  技术社区  ›  Lipis

如何从Google App Engine上的URL将文件存储在Google存储上?

  •  5
  • Lipis  · 技术社区  · 14 年前

    boto gsutil HTTP requests ( PUT ) 我得到了错误签名的错误响应。显然我做错了什么,但不幸的是我不知道在哪里。

    所以我的问题是:如何从URL中检索文件,并使用Python for Google App Angine将其存储在Google存储中?

    以下是我所做的(使用另一个 answer

    class ImportPhoto(webapp.RequestHandler):
        def get(self):
            self.response.headers['Content-Type'] = 'text/plain'
            srow = self.response.out.write
            url = self.request.get('url')
            srow('URL: %s\n' % (url))
            image_response = urlfetch.fetch(url)
            m = md5.md5()
            m.update(image_response.content)
            hash = m.hexdigest()
            time = "%s" % datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
            str_to_sig = "PUT\n" + hash + "\n\n" + 
                          time + "\nx-goog-acl:public-read\n/lipis/8418.png"
            sig = base64.b64encode(hmac.new(
                                      config_credentials.GS_SECRET_ACCESS_KEY,
                                      str_to_sig, hashlib.sha1).digest())
            total = len(image_response.content) 
            srow('Size: %d bytes\n' % (total))
    
            header = {"Date": time,
                      "x-goog-acl": "public-read",
                      "Content-MD5": hash,
                      'Content-Length': total,
                      'Authorization': "GOOG1 %s:%s" % 
                                        (config_credentials.GS_ACCESS_KEY_ID, sig)}
    
            conn = httplib.HTTPConnection("lipis.commondatastorage.googleapis.com")
            conn.set_debuglevel(2)
    
            conn.putrequest('PUT', "/8418.png")
            for h in header:
                conn.putheader(h, header[h])
            conn.endheaders()
            conn.send(image_response.content + '\r\n')
            res = conn.getresponse()
    
            srow('\n\n%d: %s\n' % (res.status, res.reason))
            data = res.read()
            srow(data)
            conn.close()
    

    我得到的回应是:

    URL: https://stackoverflow.com/users/flair/8418.png
    Size: 9605 bytes
    
    400: Bad Request
    <?xml version='1.0' encoding='UTF-8'?><Error><Code>BadDigest</Code><Message>The Content-MD5 you specified did not match what we received.</Message><Details>lipis/hello.jpg</Details></Error>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Nick Johnson    14 年前

    你看了上面的文件了吗 how to sign requests ? 要签名的字符串必须包含 Content-MD5 , Content-Type Date 除了自定义头和资源路径之外,还有头。

        2
  •  1
  •   Peter Knego    14 年前

    Content-MD5 标题是可选的 PUT requests

    此外,所需的标题是 Authorization , Date Host . 你的请求好像不见了 头球。