Problem with google file system for api in python

I am trying to upload a file to Google repository, but my code freezes and does not respond. Please help me.

Mycode:

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()
+1
source share
2 answers

I will solve my problem myself :) my code is:

        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
source

I know this is more of a comment than an answer to your problem, but I put it in the answer because I cannot comment.

It would really help if you could narrow the space in your function where it hangs. Although adding printfunctions / statements slightly changes the state of the memory, it might be worth a try here, as the likely source of the hanging is the network calls you make.

- , , Google?

0

All Articles