【发布时间】:2016-08-23 06:26:30
【问题描述】:
我正在设置一个服务:
- 收到带有附件文件的电子邮件
- 将此文件上传到云存储
- 将该文件用作进一步处理的源
我到达了发生错误的第 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 返回 "上传请求必须包含一个 uploadType URL 参数和一个 URL 以 /upload/">
开头的路径
我知道信息没有发布到正确的端点并且缺少 URL 参数,但是我在 Google 文档中挖掘的越多,我就越无法找到关于如何使用发现模块中的 .build在服务“存储”之前添加 URL 路径。
-- 更新解决方案--
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`
【问题讨论】:
标签: python google-app-engine flask google-cloud-storage google-cloud-platform