john misoskian
john misoskian

Reputation: 584

google storage api put file problem in python

I'm trying to upload a file to Google storage, 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()

Upvotes: 1

Views: 355

Answers (2)

john misoskian
john misoskian

Reputation: 584

i resolve my problem myself :) my code :

        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

Upvotes: 2

dtlussier
dtlussier

Reputation: 3129

I know this is a bit more of a comment than an answer to your issue, but I'm putting this in an answer because I can't comment yet.

It would really help if you could narrow down the place in your function where it hangs. Although adding print functions/statements will change the state of memory slightly it could be worth a try here, as a likely source of hanging is the network calls you are making.

Also - sounds simple, but are you sure that you are on the network and able to access the Google storage site?

Upvotes: 0

Related Questions