在S3存储桶中附加图像以使用SendGrid发送
我正在使用Xcode 7.3.1在Swift 2.2中编写一个iOS应用程序。 那么,对于我使用AWS Mobilehub ( S3和Lambda )的后端服务,
应用程序应该执行的操作:截取屏幕截图,将其发送到AWS S3存储桶,使用AWS Lambda函数触发器通过SendGrid发送屏幕截图。
我的问题:我似乎无法将S3存储桶中该死的图像附加到电子邮件。 本地它工作正常,但上传到Lambda时会引发以下错误:
错误:ENOENT:没有这样的文件或目录,打开'…'
使用下面的代码:
'use strict'; var fs = require('fs'); console.log('Loading function'); let aws = require('aws-sdk'); let s3 = new aws.S3({ apiVersion: '2006-03-01' }); exports.handler = (event, context, callback) => { // Get the object from the event and show its content type const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); var helper = require('sendgrid').mail; let from_email = new helper.Email("from@gmail.com"); let to_email = new helper.Email("to@gmail.com"); let subject = "Hello World from the SendGrid Node.js Library"; let content = new helper.Content("text/plain", "Email content"); let mail = new helper.Mail(from_email, subject, to_email, content); var bse64 = base64_encode(INeedThisFrigginPath); let attachment = new helper.Attachment(); attachment.setContent(bse64); attachment.setType("image/png"); attachment.setFilename(key); attachment.setDisposition("attachment"); mail.addAttachment(attachment); var sg = require('sendgrid')('SG.sengridkey'); var requestBody = mail.toJSON(); var emptyRequest = require('sendgrid-rest').request; var requestPost = JSON.parse(JSON.stringify(emptyRequest)); requestPost.method = 'POST'; requestPost.path = '/v3/mail/send'; requestPost.body = requestBody; sg.API(requestPost, function (error, response) { console.log(response.statusCode); console.log(response.body); console.log(response.headers); } ); }; // function to encode file data to base64 encoded string function base64_encode(file) { // read binary data var bitmap = fs.readFileSync(file); // convert binary data to base64 encoded string return new Buffer(bitmap).toString('base64'); }
具体来说这一行:
var bse64 = base64_encode(INeedThisFrigginPath);
这个问题很明显,所以我需要知道的是, 我的形象的正确道路是什么。
我已经尝试使用键值和图像链接:
https://s3.eu-central-1.amazonaws.com/bucketname/public/test0.png
没有运气。
如果有人能够通过提供代码,教程或者一般指针来帮助我,那将是非常棒的。 也许使用AWS S3,AWS Lambda和SendGrid不一定是在这里使用的最好的技术?
谢谢一堆!
您正尝试构build该文件的path,然后尝试将其作为本地文件打开。 该文件不是本地的,它在S3上,所以你不能这样做。 你有两个select:
- 首先将文件从S3下载到Lambda(到/ tmp目录中),然后在打开文件时引用本地path。
- 使用AWS S3 SDK打开一个ReadStream文件。
- S3.putObject – callback永远不会被调用
- 确定AWS S3上传的部件大小和队列大小参数
- 如何确定S3.getObject()中的数据types
- AWS S3 JavaScript SDK getSignedUrl仅返回基本path
- 错误:configuration文件丢失 – /nodeapp/node_modules/aws-sdk/lib/request.js:31
- 使用请求从节点stream上传到S3的问题
- 解密s3文件卸载使用unload命令与对称密钥encryption
- S3设置Access-Control-Allow-Origin头
- 使用nodejs knox删除s3桶中的所有文件?