如何在Lambda中使用Nodejs从S3下载PDF?

huangapple go评论70阅读模式
英文:

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 存储桶中执行 getObjectputObject 的执行角色。

对于 Node.js,我建议使用 AWS SDK 的 v3 版本。具体来说,请查看 GetObjectCommand

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 09:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定