使用AWS Lambda下载文件 [英] Download files using AWS Lambda

查看:528
本文介绍了使用AWS Lambda下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用AWS Lambda函数的iOS应用.我希望Lambda函数通过Lambda代理功能从服务器获取一些文件,然后将其发送回iOS应用.

I have an iOS app that calls a AWS Lambda function. I would like the Lambda function to get some files from a server and send it back to the iOS app, through the Lambda proxy feature.

我正在使用生成的SDK从我的应用程序直接调用Lambda函数.我找不到说明如何交换除JSON编码请求以外的数据的文档.

I am directly invoking the Lambda function from my app using the generated SDK. I couldn't find a documentation explaining how to exchange data other than JSON encoded requests.

我应该怎么做?

推荐答案

Lambda与外界的接口是JSON.

Lambda's only interface to the outside world is JSON.

要从Lambda返回文本数据,您必须将其作为字符串返回,并从JSON响应中反序列化该字符串.

To return text data from Lambda, you have to return it as a string, and deserialize the string from the JSON response.

要从Lambda返回二进制数据,必须首先使用无法产生一系列字节(也不是有效的UTF-8字符序列)的编码对数据进行转换(编码),因为JSON无法序列化非字符数据(并非字节的所有可能组合都对应一个或多个有效字符).这样做的常用策略是使用base-64编码. Base-64使用8:6转换率(每个字节编码6位)将一系列字节(八位字节)无损地转换为也总是有效的7位ASCII字符的不同字节系列.然后,您需要将该数据从base-64解码回二进制文件.

To return binary data from Lambda, the data must be first transformed (encoded) using an encoding that cannot ever produce a series of bytes that is not also a valid sequence of UTF-8 characters, because JSON cannot serialize non-character data (and not every possible combination of bytes corresponds to a valid character or characters). The common strategy for doing this is to use base-64 encoding. Base-64 losslessly converts a series of bytes (octets) into a different series of bytes that are also always valid 7 bit ASCII characters, using an 8:6 conversion ratio (coding 6 bits per byte). You would then need to decode this data back from base-64 to binary.

您可以在客户端上对JSON和base-64进行解码,但是,如果您不喜欢该主意,还有其他一些选择.

You can do this decoding of JSON and base-64 on the client, but there are also a couple of other options, if you don't like that idea.

API网关和CloudFront的Lambda @ Edge功能都提供了内置的转换功能,如果您不想在客户端上将base-64有效负载(来自JSON Lambda响应)转换回二进制.

Both API Gateway and CloudFront's Lambda@Edge feature provide a built-in conversion capability to convert the base-64 payload (from the JSON Lambda response) back to binary, if you don't want to do it on the client.

API网关支持所有Lambda运行时,并且

API Gateway supports all Lambda runtimes, and expects this format...

"isBase64Encoded": true,
"body": "the-base-64-encoded-body",

Lambda @ Edge仅支持Node.js Lambda函数,但比API Gateway便宜.它期望以64为底的响应. ..

Lambda@Edge supports only Node.js Lambda functions, but is less expensive than API Gateway. It expects a base-64 response to include...

"body": "the-base-64-encoded-body",
"bodyEncoding": "base64",

这两种方法的可行性取决于您的安全需求. API网关支持通过IAM以及其他机制进行身份验证. CloudFront + Lambda @ Edge不支持IAM身份验证,但可以与CloudFront签名的URL或Cognito或其他自定义授权机制一起使用.

The viability of either approach depends on your security needs. API Gateway supports authentication via IAM as well as other mechanisms. CloudFront + Lambda@Edge doesn't support IAM auth but can be used with CloudFront signed URLs or Cognito or other custom authorization mechanisms.

如果您提到的文件"来自服务器,API Gateway也可以直接从服务器代理这些文件,而无需Lambda函数来处理内容(尽管根据您的安全需求,可能需要Lambda自定义授权程序,以对请求进行身份验证,然后直接告诉API网关以允许将请求转发到后端.

If the "files" you mentioned are coming from a server, API Gateway can also proxy these files directly from the server, without a Lambda function handling the content (though depending on your security needs, a Lambda Custom Authorizer might be desirable, to authenticate the request but then simply tell API Gateway to allow the request to be forwarded to the backend).

或者,如果文件是S3中的对象,则可以直接访问S3,类似于现在访问Lambda的方式.

Or, if the files are objects from S3, then you can access S3 directly, similiar to the way you are accessing Lambda, now.

这篇关于使用AWS Lambda下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆