tags:

views:

59

answers:

2

Hi,

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()
A: 

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?

dtlussier
i'm access the google storage site and get methots no problem.
john misoskian
Is it possible to get a more verbose output from the HTTP connection? Maybe by throwing in a verbose or logging flag? You might also give a network analyzer like WireShark a try to see what is actually going i and out of your machine when you run the program/script.
dtlussier
i watch http analayzer a request and i see waiting response. I'm add to my output screenshot
john misoskian
I see that screenshot now. Honestly, deciphering that is outside my experience, hopefully someone else will see it now though!
dtlussier
+1  A: 

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