كيف يمكن لتخزين ملف على تخزين Google من عنوان URL على محرك تطبيق Google؟

StackOverflow https://stackoverflow.com/questions/4105901

سؤال

أرغب في إنشاء خدمة على Google App Engine (Python) التي ستتلقى عنوان URL لصورة وتخزينها على Google Storage. تمكنت من التحميل من ملف محلي باستخدام boto أو gsutil سطر الأوامر ، ولكن ليس عن طريق استرداد الملف عبر عنوان URL. حاولت القيام بذلك باستخدام طلبات HTTP (PUT) وأتلقى ردود الخطأ على التوقيعات الخاطئة. من الواضح أنني أفعل شيئًا خاطئًا ، لكن للأسف ليس لدي أي فكرة عن مكان.

لذا فإن سؤالي هو: كيف يمكنني استرداد ملف من عنوان URL وتخزينه على Google Storage باستخدام Python for Google App Anضاء؟

هذا ما فعلته (باستخدام آخر إجابه):

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>
هل كانت مفيدة؟

المحلول

هل قرأت المستندات كيفية التوقيع على الطلبات؟ يجب أن تتضمن السلسلة للتوقيع Content-MD5, Content-Type و Date الرؤوس ، بالإضافة إلى الرؤوس المخصصة ومسار المورد.

نصائح أخرى

Content-MD5 الرأس اختياري ل طلب الطلبات. حاول ترك هذا للاختبار.

أيضا ، الرؤوس المطلوبة Authorization, Date و Host. يبدو أن طلبك مفقود Host رأس.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top