上のPDFファイルをアップロードしようとしている私は、サービス設定していたとき:HttpError 400 Googleクラウドストレージ
- が
- があることを利用し、クラウドストレージへの添付ファイルで
- アップロードこのファイルを電子メールを受信しますファイルを次の処理のためのソースとして追加します。
エラーが発生しているステップ2に到着しました。私はGoogleサービスの検出APIを使用して認証しています。私は単純なフラスコのアプリケーションであり、以下は着信電子メールハンドラです。
handle_incoming_email.py
__author__ = 'ciacicode'
import logging
import webapp2
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build
credentials = GoogleCredentials.get_application_default()
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
# upload attachment to cloud storage
filename = 'any'
try:
attachment = mail_message.attachments
filename = attachment[0].filename
body = str(attachment[0].payload)
except IndexError:
print len(attachment)
storage = build('storage', 'v1', credentials=credentials)
req = storage.objects().insert(bucket='ocadopdf', body={'body': body, 'contentType': 'application/pdf', 'name': str(filename), 'contentEncoding': 'base64'})
resp = req.execute()
return resp
app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)
私はログに次のエラーが表示されるサービスに電子メールを送信した後:
HttpError: https://www.googleapis.com/storage/v1/b/ocadopdf/o?alt=json returned "Upload requests must include an uploadType URL parameter and a URL path beginning with /upload/">
私は情報がに投稿されていませんことを理解しURLパラメータが欠落していますが、Googleのドキュメントでは、発見モジュールからの.buildを使用してサービスの保存前にURLパスを追加する方法についてはっきりとわかりませんe '。
- 溶液でアップデート -
file = cStringIO.StringIO()
file.write(body)
media = http.MediaIoBaseUpload(file, mimetype='application/pdf')
storage = build('storage', 'v1', credentials=credentials)
req = storage.objects().insert(bucket='ocadopdf', body={'name': str(filename), 'contentEncoding': 'base64'}, media_body=media)
resp = req.execute()
return resp`
こんにちはAngus、残念なことに、この例ではローカルマシン上のファイルを扱う方法しか示していません。電子メールの添付ファイルは、通常のpythonファイルオブジェクトではなく、Googleオブジェクトです。これまでのところ、問題は本体の内容ではなく、エンドポイント呼び出し自体にありました。 – ciacicode
正しい - 2番目のリンクは、io.BytesIOと任意のバイトのMediaIoUploadの使い方を示しますが、簡単にStringIOなどにすることもできます。 –
@Angus Davisのおかげで、上記の変更に取り組んでいます。アップロード後にpdfを正常に開くことができません。これはエンコーディングと関係がありますが、この質問自体は答えられると思います。このようなエラー400が表示されると、適切なファイルストリームオブジェクトで満たされたmedia_bodyパラメータがないことを示します。 – ciacicode