简体   繁体   English

通过AWS Lambda和API网关下载Amazon S3的文本文件

[英]Download text files of Amazon S3 through an AWS Lambda and API Gateway

We upload some files of text to S3 and we want to download these through API Gateway using Lambda function, we did the following configuration. 我们将一些文本文件上传到S3,并希望使用Lambda函数通过API网关下载这些文件,我们进行了以下配置。

For example in this case the file download.txt has this content In descriptive writing, the author does not tell the reader what was seen, felt, tested, smelled, or heard. 例如,在这种情况下,文件download.txt具有此内容。在描述性写作中,作者没有告诉读者所看到,感受到,测试,闻到或听到的东西。 Rather, he describes something that he experienced... 相反,他描述了他经历过的东西。

  1. The Lambda Function responds the bytes of the file from S3 in this case is a txt file Lambda函数从S3响应文件的字节,在这种情况下是txt文件

    SW4gZGVzY3JpcHRpdmUgd3JpdGluZywgdGhlIGF1dGhvciBkb2VzIG5vdCB0ZWxsIHRoZSByZWFkZXIgd2hhdCB3YXMgc2VlbiwgZmVsdCwgdGVzdGVkLCBzbWVsbGVkLCBvciBoZWFyZC4gIFJhdGhlciwgaGUgZGVzY3JpYmVzIHNvbWV0aGluZyB0aGF0IGhlIGV4cGVyaWVuY2VkIGFuZCwgdGhyb3VnaCBoaXMg... SW4gZGVzY3JpcHRpdmUgd3JpdGluZywgdGhlIGF1dGhvciBkb2VzIG5vdCB0ZWxsIHRoZSByZWFkZXIgd2hhdCB3YXMgc2VlbiwgZmVsdCwgdGVzdGVkLCBzbWVsbGVkLCBvciBoZWFyZC4gIFJhdGhlciwgaGUgZGVzY3JpYmVzIHNvbWV0aGluZyB0aGF0IGhlIGV4cGVyaWVuY2VkIGFuZCwgdGhyb3VnaCBoaXMg ...

  2. Configuration of the method response and integration of API Gateway Method Response Integration Resonse 方法响应的配置和API网关方法响应 集成共振的集成

  3. Execute the test of API Gateway with the needed parameters. 使用所需的参数执行API网关的测试。

    In descriptive writing, the author does not tell the reader what was seen, felt, tested, smelled, or heard. 在描述性写作中,作者不会告诉读者所看到,感觉到的,经过测试的,闻到的或听到的是什么。 Rather, he describes something that he experienced... 相反,他描述了他经历过的东西。

The response is the content of the file from S3 but when we execute the API Gateway in a browser this return a txt file but the content of this is the bytes that return the lambda function and should be the same response of the test in API Gateway test. 响应是来自S3的文件的内容,但是当我们在浏览器中执行API网关时,这将返回txt文件,但是此内容是返回lambda函数的字节,应与API Gateway中的测试相同测试。

Execute API Gateway in browser 在浏览器中执行API网关

https://xxxxxx.execute-api.us-west-2.amazonaws.com/file/download?key=download.txt&bucketName=xxxxxxxxxxxxxxxxxxxx&isMultipart=false

The file content is 文件内容是

SW4gZGVzY3JpcHRpdmUgd3JpdGluZywgdGhlIGF1dGhvciBkb2VzIG5vdCB0ZWxsIHRoZSByZWFkZXIgd2hhdCB3YXMgc2VlbiwgZmVsdCwgdGVzdGVkLCBzbWVsbGVkLCBvciBoZWFyZC4gIFJhdGhlciwgaGUgZGVzY3JpYmVzIHNvbWV0aGluZyB0aGF0IGhlIGV4cGVyaWVuY2VkIGFuZCwgdGhyb3VnaCBoaXMg...

Do you have any ideas that should be made to receive the correct content file? 您对接收正确的内容文件有什么想法吗?

Thank you 谢谢

You should be able to use Buffer.from in order to transform your base64 data into binary. 您应该能够使用Buffer.from ,以便将base64数据转换为二进制数据。

From there you can either upload it to S3 or send it back as response using API Gateway. 您可以从那里将其上传到S3或使用API​​网关将其作为响应发送回去。

Note: I'm assuming that your Lambda function is written in Node. 注意:我假设您的Lambda函数是用Node编写的。 If not, you should be able to apply the same approach in other languages anyway. 如果没有,您仍然应该能够在其他语言中应用相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM