简体   繁体   English

API 带子目录的 S3 网关代理

[英]API Gateway Proxy for S3 with subdirectories

I created an API Gateway method GET with integration type "AWS Service" for "S3".我为“S3”创建了一个集成类型为“AWS 服务”的 API 网关方法 GET。 I defined a Path override: {object} that is mapped from method.request.path.item我定义了一个 Path override: {object} 从 method.request.path.item 映射

整合请求

As long as the path contains only the bucket itself (./bucket/ mybucketname ), it works and the value of {item} is mybucketname .只要路径仅包含存储桶本身 (./bucket/ mybucketname ),它就可以工作并且 {item} 的值为mybucketname

But when i specify for example../bucket/ mybucketname/foo/bar/test.txt as path the {item} value should be mybucketname/foo/bar/test.txt but it is empty .但是当我指定例如../bucket/mybucketname/foo/bar/test.txt作为路径时,{item} 值应该是 mybucketname/foo/bar/test.txt 但它是的。 I think it's because of the nested path我认为这是因为嵌套路径

When i choose catch-all path variables {proxy+} I'm not able to choose integration type "AWS Service" any longer.当我选择包罗万象的路径变量 {proxy+} 时,我无法再选择集成类型“AWS 服务”。

Any hints what I'm doing wrong or what i should do in order to retrieve files/folders in subfolders from S3 via API Gateway?任何提示我做错了什么或我应该做什么才能通过 API 网关从 S3 检索子文件夹中的文件/文件夹?

This can be done using proxy resource .这可以使用 代理资源来完成。

Here are my steps to achieve what @Johnny90 wants:以下是我实现@Johnny90 想要的目标的步骤:

  1. Click on your API in the API Gateway console.在 API Gateway 控制台中单击您的 API。

  2. Choose the /bucket resource and then choose Create Resource from the Actions drop-down menu.选择/bucket资源,然后从操作下拉菜单中选择创建资源 Then, do the following in the New Child Resource pane.然后,在“新建子资源”窗格中执行以下操作。

    a.一种。 Tick "Configure as proxy resource".勾选“配置为代理资源”。

    b.Use the default proxy for Resource Name.使用资源名称的默认proxy

    c. C。 Use {proxy+} for Resource Path.使用{proxy+}作为资源路径。

    d. d. Choose Create Resource .选择创建资源

  3. Choose HTTP Proxy for Integration type and type the Endpoint URL as any website (eg, https://my-website.com/ {proxy} ).为集成类型选择 HTTP 代理并键入端点 URL 作为任何网站(例如, https://my-website.com/ {proxy} )。 Then choose Save .然后选择保存

  4. Choose Integration Request选择集成请求

    a.一种。 Select AWS Service as the Integration type.选择AWS 服务作为集成类型。

    b.From the AWS Region drop-down list, choose the region that your S3 bucket resides in.从 AWS 区域下拉列表中,选择您的 S3 存储桶所在的区域。

    c. C。 From AWS Service, choose S3.从 AWS 服务中,选择 S3。 For AWS Subdomain, leave it blank.对于 AWS 子域,将其留空。

    d. d. For HTTP method, choose GET.对于 HTTP 方法,选择 GET。

    e. e. Choose Use path override for Action Type .Action Type选择Use path override and type bucket/{proxy} .并输入bucket/{proxy}

    f. F。 Paste an IAM role that has enough permissions.粘贴具有足够权限的 IAM 角色。

    g. G。 Click Save .点击保存

After changing the integration request from HTTP Proxy integration to AWS Service , we have to add some settings for the API.将集成请求从HTTP 代理集成更改为AWS Service 后,我们必须为 API 添加一些设置。 First, you must set up the URL Path Parameters so that API Gateway can understand the {proxy} variable defined in resource path in Integration Request .首先,您必须设置 URL 路径参数,以便 API Gateway 可以理解Integration Request资源路径中定义的{proxy}变量。

  1. Extend URL Path Parameters in Integration Request and then choose Add path .在集成请求中扩展URL 路径参数,然后选择添加路径

  2. Type proxy in the Name column and method.request.path.proxy in the Mapped from column.在 Name 列中键入proxy ,在Mapped from列中键入method.request.path.proxy

Second, choose Method Response from Method Execution .其次,从Method Execution 中选择Method Response

  1. Choose Add Response.选择添加响应。 Type 200 for HTTP status.为 HTTP 状态键入 200。

  2. Expand the response of the 200 code.展开 200 代码的响应。

    a.一种。 Choose Add Header .选择添加标题 Type Content-Type for the Name.为名称键入Content-Type

    b.Click Add Response Model .单击添加响应模型 Type application/json for Content type and choose Empty from the Models drop-down menu.Content type application/json并从 Models 下拉菜单中选择Empty

Finally, choose Integration Response from Method Execution .最后,从Method Execution 中选择Integration Response

  1. Extend the 200 Method response status扩展 200 Method 响应状态

  2. For Header Mapping , you should see the Content-Type in the Response header column.对于Header Mapping ,您应该在 Response 标题列中看到Content-Type Type integration.response.header.Content-Type in the Mapping value column.映射值列中输入integration.response.header.Content-Type

The following are my configurations:以下是我的配置:

Integration Request:集成请求: 在此处输入图片说明

Integration Response:集成响应: 在此处输入图片说明

Method Response:方法响应: 在此处输入图片说明

The key is how you pass the value for {object} variable in Path Override.关键是如何在路径覆盖中传递 {object} 变量的值。 I think you set url path parameter for integration request from request path, like this:我认为您为来自请求路径的集成请求设置了 url 路径参数,如下所示:

object = method.request.path.object     

I believe the problem was because the object has "/" in it, so does the api path (resource).我认为问题是因为对象中有“/”,api 路径(资源)也是如此。 It cause the api gateway can only recognized object in root path.它导致 api 网关只能识别根路径中的对象。

What I do is set url path parameter for integration request from request querystring like this:我所做的是为来自请求查询字符串的集成请求设置 url 路径参数,如下所示:

object = method.request.querystring.object

It works有用

Or you can try encode the object path using url encode, replace '/' with '%2F'.或者您可以尝试使用 url encode 对对象路径进行编码,将 '/' 替换为 '%2F'。 I never tried though.我从来没有尝试过。

Path override : bucket_name/{folder} pattern as per level, it's for first {folder}.路径覆盖:bucket_name/{folder} 模式按照级别,用于第一个 {folder}。

在此处输入图片说明

you can access s3 nested path like this.您可以像这样访问 s3 嵌套路径。 在此处输入图片说明

this is for last {object}, so mentioned all path map.这是最后一个{object},所以提到了所有路径图。 this link will helpfull https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html此链接将有帮助https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html

@johnny90,首先在创建资源 {proxy+} 时配置为 lambda 代理...创建后..将集成类型更新为“AWS 服务”。然后您可以将任何 AWS 服务配置到您的 API 网关。

You might use "%2F" as path separator in your requesting URL, eg:您可以在请求 URL 中使用“%2F”作为路径分隔符,例如:

foo%2Fbar in the URL will be translated into foo/bar S3 path. URL 中的foo%2Fbar将被转换为foo/bar S3 路径。

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

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