获取gmail附件的下载URL?

问题描述

我正在使用get_user_message_attachment Gmail Attachment通过Gmail API获取method,但是它发送的数据是Base64编码形式。我想获取附件的可下载URL,可以将其存储并发送到应用程序的前端。是否可以通过某种方式获取附件的URL?我不想将Base64字符串转换为服务器上的文件,然后将其上传到某个地方,然后将该上传链接发送到前端。

@gmail_service = Google::Apis::GmailV1::GmailService.new
#authorization stuff is done and then I fetch the attachment which is received as a Google::Apis::GmailV1::MessagePartBody response
resp = @gmail_service.get_user_message_attachment("myemail@google.com",message_id,attachment_id)
# resp.data contains the base64 encoded string

我希望将附件作为可下载的URL,这样就不必手动进行文件转换上传内容

解决方法

message.get方法返回一个Message object

此对象包含已编码的base64消息的正文。

enter image description here

如果您想要可下载的URL,建议您将Base64字符串转换为服务器上的文件,然后将其上传到某处,并将该上传的链接发送到前端。

没有其他选择,这是API返回的数据。

,

您可能要考虑在服务器上为前端提供代理终结点。然后,为前端提供一个带有messageId的指向此终结点的链接,并让您的服务器下载Gmail邮件并将其流式传输到客户端。

这是渡槽(飞镖)示例代码:

  1. 具有专用的附件路由(tempToken&fileName是可选的,也可以从附件数据派生contentType):

.. route('/ attachments /:tempToken /:msgId /:attachmentId /:fileName')。link(() => AttachmentsController(ctx))

  1. 控制器本质上通过msgId&attachmentId加载附件并将其内容提供给客户端:
@Operation.get('tempToken','msgId','attachmentId','fileName') Future<Response> getAttachment(
  @Bind.path('tempToken') String tempToken,//
  @Bind.path('msgId') int msgId,@Bind.path('attachmentId') String attachmentId,@Bind.query('contentType') String contentType) async {

...

final attachment = await mailbox.readAttachment(messageId,attachmentId);

return Response.ok(attachment)
  ..encodeBody = false
  ..contentType = ContentType.parse(contentType);   }
  1. 我们使用Google提供的Gmail API库。这是mailbox.readAttachment:
@override
Future<List<int>> readAttachment(String messageId,String attachmentId) async => (await _gmail.users.messages.attachments.get('me',messageId,attachmentId)).dataAsBytes;