【问题标题】:google storage api put file problem in pythongoogle storage api在python中放置文件问题
【发布时间】:2010-10-01 19:01:58
【问题描述】:

我正在尝试将文件上传到 Google 存储,但我的代码冻结并且没有响应。请帮我。

我的代码:

def PutFile(self,filename):
    conn = httplib.HTTPConnection("%s.commondatastorage.googleapis.com" % self.bucket)
    conn.set_debuglevel(2)

    dd = "%s" % datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
    strToSign = "PUT\n"+"\nimage/jpeg\n"+dd+"\nx-goog-acl:public-read\n/%s/x.jpg" % self.bucket
    f = open(filename,"r")
    m = hashlib.md5()
    m.update(f.read())
    h = m.hexdigest()
    sig = base64.b64encode(hmac.new(self.secret, strToSign, hashlib.sha1).digest())
    total = os.path.getsize(filename)

    header = {"Date":dd,"x-goog-acl":"public-read","Content-MD5":h,'Content-Length':total,'Content-Type':'image/jpeg','Authorization':"GOOG1 %s:%s" % (self.key,sig)}


    r1 = conn.getresponse()
    print r1.status, r1.reason
    print r1.read()
    conn.close()

【问题讨论】:

  • 冻结在哪一行?
  • r1 = conn.getresponse() 行被冻结

标签: python


【解决方案1】:

我自己解决了我的问题:)我的代码:

        conn = httplib.HTTPConnection("mustafa-yontar.commondatastorage.googleapis.com")
        conn.set_debuglevel(2)
        f = open(filename,"r")
        m = hashlib.md5()
        m.update(f.read())
        h = m.hexdigest()
        has = h
        dd = "%s" % datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
        strToSign = "PUT\n"+h+"\n\n"+dd+"\nx-goog-acl:public-read\n/mustafa-yontar/x.jpg"

        sig = base64.b64encode(hmac.new(self.secret, strToSign, hashlib.sha1).digest())
        total = os.path.getsize(filename)

        header = {"Date":dd,"x-goog-acl":"public-read","Content-MD5":h,'Content-Length':total,'Authorization':"GOOG1 %s:%s" % (self.key,sig)}

        conn.putrequest('PUT', "/x.jpg")
        for h in header:
            conn.putheader(h, header[h])
        conn.endheaders()
        bytess = open('x.jpg', 'rb').read()
        f = StringIO(bytess)
        f.seek(0)


        while True:
            bytes = f.read(1024)
            if not bytes: break

            length = len(bytes)
            conn.send('%X\r\n' % length)
            conn.send(bytes + '\r\n')
        conn.send('0\r\n\r\n')

        #errcode, errmsg, headers = conn.getresponse()
        #h.close()


        #conn.request("PUT","/mustafa-yontar/x.jpg",f.read(),header)
        r1 = conn.getresponse()
        print r1.status, r1.reason
        print r1.read()
        conn.close()
        print has

【讨论】:

    【解决方案2】:

    我知道这更像是评论而不是对您的问题的回答,但我将其放入答案中,因为我还不能发表评论。

    如果您可以缩小函数中挂起的位置,那将非常有帮助。虽然添加 print 函数/语句会稍微改变内存状态,但在这里值得一试,因为挂起的可能来源是您正在进行的网络调用。

    另外 - 听起来很简单,但您确定您在网络上并且能够访问 Google 存储站点吗?

    【讨论】:

    • 我正在访问 google 存储站点并获取方法没问题。
    • 是否可以从 HTTP 连接获得更详细的输出?也许通过输入详细或记录标志?您还可以尝试使用 WireShark 之类的网络分析器,看看在您运行程序/脚本时,您的机器到底发生了什么。
    • 我看 http 分析器一个请求,我看到等待响应。我添加到我的输出屏幕截图
    • 我现在看到了那个截图。老实说,破译这超出了我的经验,但希望其他人现在能看到它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多