Python 将Azure Blob从流附加到SendGrid电子邮件

Python 将Azure Blob从流附加到SendGrid电子邮件,python,azure,azure-storage-blobs,sendgrid,Python,Azure,Azure Storage Blobs,Sendgrid,我正试图通过SendGrid将Azure Blob作为附件发送。我的第一步是下载blob,如下所示: download_client=BlobClient.from_connection_string( conn_str=az_str, container_name=container_name, blob_name=blob_name) download_stream = download_client.download_blob()

我正试图通过SendGrid将Azure Blob作为附件发送。我的第一步是下载blob,如下所示:

download_client=BlobClient.from_connection_string(
        conn_str=az_str, 
        container_name=container_name, 
        blob_name=blob_name) 

download_stream = download_client.download_blob()
我发现SendGrid具有使用NodeJS从内存添加文件的功能,但是我在Python中没有发现类似的功能

有人知道如何使用Python实现这一点吗

我还发现这篇关于堆栈的文章或多或少都是相同的问题,但不是在python中,也不是直接回答的。

在sendgrid.helpers.mail下有一个附件模块,您可以在此处参考:。下面是我的测试代码,也许你可以试试

import sendgrid
import os
from sendgrid.helpers.mail import *
import base64
from sendgrid import SendGridAPIClient
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from io import BytesIO

message = Mail(
    from_email='from_email@example.com',
    to_emails='to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')

connect_str ='storage connection string'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blobclient=blob_service_client.get_blob_client(container='test',blob='nodejschinesedoc.pdf')
streamdownloader =blobclient.download_blob()
stream = BytesIO()
streamdownloader.download_to_stream(stream)


encoded = base64.b64encode(stream.getvalue()).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient('sendgrid API key')
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.args)
导入sendgrid
导入操作系统
从sendgrid.helpers.mail导入*
导入base64
从sendgrid导入SendGridApicClient
从azure.storage.blob导入BlobServiceClient、BlobClient、ContainerClient
从io导入字节io
信息=邮件(
from_email='from_email@example.com',
到to@example.com',
主题=“使用Twilio SendGrid发送很有趣”,
html_content='并且在任何地方都可以轻松完成,即使使用Python也可以。)
connect\u str='存储连接字符串'
blob\u服务\u客户端=BlobServiceClient.from\u连接\u字符串(connect\u str)
blobclient=blob\u service\u client.get\u blob\u client(container='test',blob='nodejschinesedoc.pdf')
streamdownloader=blobclient.download_blob()
stream=BytesIO()
streamdownloader.下载到\u流(stream)
encoded=base64.b64encode(stream.getvalue()).decode()
附件=附件()
attachment.file_content=文件内容(编码)
attachment.file_type=FileType('application/pdf')
attachment.file\u name=FileName('test\u FileName.pdf')
attachment.disposition=处置(“附件”)
attachment.content\u id=ContentId(“示例内容id”)
message.attachment=附件
尝试:
sendgrid\u client=SendGridAPIClient('sendgrid API键'))
response=sendgrid\u client.send(消息)
打印(响应状态\ U代码)
打印(响应.正文)
打印(响应.标题)
例外情况除外,如e:
打印(e.args)

sendgrid.helpers.mail下有一个附件模块,您可以在此处参考:。下面是我的测试代码,也许你可以试试

import sendgrid
import os
from sendgrid.helpers.mail import *
import base64
from sendgrid import SendGridAPIClient
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from io import BytesIO

message = Mail(
    from_email='from_email@example.com',
    to_emails='to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')

connect_str ='storage connection string'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blobclient=blob_service_client.get_blob_client(container='test',blob='nodejschinesedoc.pdf')
streamdownloader =blobclient.download_blob()
stream = BytesIO()
streamdownloader.download_to_stream(stream)


encoded = base64.b64encode(stream.getvalue()).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient('sendgrid API key')
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.args)
导入sendgrid
导入操作系统
从sendgrid.helpers.mail导入*
导入base64
从sendgrid导入SendGridApicClient
从azure.storage.blob导入BlobServiceClient、BlobClient、ContainerClient
从io导入字节io
信息=邮件(
from_email='from_email@example.com',
到to@example.com',
主题=“使用Twilio SendGrid发送很有趣”,
html_content='并且在任何地方都可以轻松完成,即使使用Python也可以。)
connect\u str='存储连接字符串'
blob\u服务\u客户端=BlobServiceClient.from\u连接\u字符串(connect\u str)
blobclient=blob\u service\u client.get\u blob\u client(container='test',blob='nodejschinesedoc.pdf')
streamdownloader=blobclient.download_blob()
stream=BytesIO()
streamdownloader.下载到\u流(stream)
encoded=base64.b64encode(stream.getvalue()).decode()
附件=附件()
attachment.file_content=文件内容(编码)
attachment.file_type=FileType('application/pdf')
attachment.file\u name=FileName('test\u FileName.pdf')
attachment.disposition=处置(“附件”)
attachment.content\u id=ContentId(“示例内容id”)
message.attachment=附件
尝试:
sendgrid\u client=SendGridAPIClient('sendgrid API键'))
response=sendgrid\u client.send(消息)
打印(响应状态\ U代码)
打印(响应.正文)
打印(响应.标题)
例外情况除外,如e:
打印(e.args)

你想问个问题吗?明白了。为你编辑的你想问个问题吗?明白了。为你编辑的。