英文:
How to download a PDF from S3 in Lambda using Nodejs?
问题
我使用AWS Amplify创建了一个React应用程序,需要实现的功能之一是在用户在网站上购买后执行的后处理Lambda函数。
该函数需要(1) 从S3中检索PDF文件,(2) 对文件进行一些修改,然后(3) 将文件以新名称上传回S3。在Lambda中下载文件作为blob时,我遇到了问题。
我按照Amplify文档中的建议进行操作,文档链接如下:
Amplify文档
import { Storage } from "@aws-amplify/storage";
await Storage.get('test.txt', {
level: 'public'
});
我意识到AWS创建的函数使用ES5而不是ES6,无法以这种方式导入包。我需要使用const something = require('something');
。
我尝试按照上面列出的文档操作,但似乎找不到良好的解决方案。Lambda是否无法快速运行流水线并从S3中检索文件?
谢谢帮助!
英文:
I created a React application using AWS Amplify, and part of the functionality that I need to implement is a a post-processing lambda function that happens after a user has made a purchase on the site.
The function needs to (1) retrieve a PDF file from S3, (2) alter the file in a few ways, and then (3) upload the file back to S3 under a new name. I am getting stuck in that first part when it comes to downloading the file as a blob in Lambda.
I followed the documentation in Amplify that suggested the following code:
import { Storage } from "@aws-amplify/storage"
await Storage.get('test.txt', {
level: 'public'
});
I realized that the function that AWS created uses ES5 and not ES6, and I cannot import packages that way. I need to use const something = require('something');
instead.
I tried following the documentation that I listed above, but I can't seem to find a good path forward. Is Lambda not supposed to be able to run a quick pipeline and retrieve a file from S3?
Thanks for the help!
答案1
得分: 1
你可以使用 AWS SDK for Node 来从 S3 检索对象并发布更新。Amplify 模块专为浏览器使用。您的函数还需要具备允许在适当的 S3 存储桶中执行 getObject
和 putObject
的执行角色。
对于 Node.js,我建议使用 AWS SDK 的 v3 版本。具体来说,请查看 GetObjectCommand
。
- https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/index.html
- https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/getobjectcommand.html
英文:
You can use the AWS SDK for Node to retrieve the object from S3 and post the update. Amplify modules are targeted for browser use. You function will also require an execution role that permits the function to getObject
and putObject
in the appropriate S3 bucket.
For Node.js, I would recommend using v3 of the AWS SDK. Specifically, look at the GetObjectCommand
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论