简体   繁体   中英

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.

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. 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

    SW4gZGVzY3JpcHRpdmUgd3JpdGluZywgdGhlIGF1dGhvciBkb2VzIG5vdCB0ZWxsIHRoZSByZWFkZXIgd2hhdCB3YXMgc2VlbiwgZmVsdCwgdGVzdGVkLCBzbWVsbGVkLCBvciBoZWFyZC4gIFJhdGhlciwgaGUgZGVzY3JpYmVzIHNvbWV0aGluZyB0aGF0IGhlIGV4cGVyaWVuY2VkIGFuZCwgdGhyb3VnaCBoaXMg...

  2. Configuration of the method response and integration of API Gateway Method Response Integration Resonse

  3. Execute the test of API Gateway with the needed parameters.

    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.

Execute API Gateway in browser

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.

From there you can either upload it to S3 or send it back as response using API Gateway.

Note: I'm assuming that your Lambda function is written in Node. If not, you should be able to apply the same approach in other languages anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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