Google App EngineのURLからGoogleストレージにファイルを保存するにはどうすればよいですか?
-
29-09-2019 - |
質問
Google App Engine(Python)にサービスを作成し、画像のURLを受け取り、Googleストレージに保存します。使用してローカルファイルからアップロードすることができました boto
また gsutil
コマンドラインですが、URL経由でファイルを取得することではありません。私はそれを使ってやってみました httpリクエスト(PUT
) そして、私は間違った署名のエラー応答を取得しています。明らかに私は何か間違ったことをしていますが、残念ながらどこにあるのかわかりません。
私の質問は、Google App Angine用のPythonを使用してGoogleストレージにファイルを取得して、Google Angineを使用してGoogleストレージに保存するにはどうすればよいですか?
これが私がやったことです(別のものを使用してください 答え):
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
ヘッダ。
所属していません StackOverflow