簡體   English   中英

API 網關 S3 代理 - 獲得部分響應

[英]API Gateway S3 proxy - get partial response

我正在嘗試將 API 網關設置為 S3 存儲桶的代理。 我已經使用了亞馬遜指南並成功部署了 API,您可以在其中指定存儲桶和資源路徑中的 object 鍵 - 即調用HTTPS://<API>/{bucket name}/{object key}獲得相關內容的 200 響應。

由於我在 S3 存儲桶上有一些大文件,我想支持字節范圍提取。 如果我直接調用 S3,我可以在 GET 請求上使用 Range header 來執行此操作。 我在我的 API 代理上嘗試了同樣的方法,它似乎只是忽略了參數。 我總是收到包含完整內容的 200 響應。

有沒有辦法可以配置 API 網關以通過范圍請求並返回 206 響應?

我最初的 API 定義如下所示。

編輯:澄清 - 我知道預簽名的 URL 是管理較大文件的推薦方法。 在這種情況下,這不是我可用的選項。

{
  "openapi": "3.0.1",
  "info": {
    "title": "GenericS3Reader",
    "version": "2016-10-13T23:04:43Z"
  },
  "servers": [
    {
      "url": "https://XXXXXXXXXX.execute-api.eu-west-2.amazonaws.com/{basePath}",
      "variables": {
        "basePath": {
          "default": "/test"
        }
      }
    }
  ],
  "paths": {
    "/{folder}": {
      "get": {
        "parameters": [
          {
            "name": "folder",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "Content-Length": {
                "schema": {
                  "type": "string"
                }
              },
              "Date": {
                "schema": {
                  "type": "string"
                }
              },
              "Content-Type": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Empty"
                }
              }
            }
          },
          "400": {
            "description": "400 response",
            "content": {}
          },
          "500": {
            "description": "500 response",
            "content": {}
          }
        }
      }
    },
    "/{folder}/{item}": {
      "get": {
        "parameters": [
          {
            "name": "item",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "folder",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "content-type": {
                "schema": {
                  "type": "string"
                }
              },
              "Content-Type": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Empty"
                }
              },
              "audio/wav": {
                "schema": {
                  "$ref": "#/components/schemas/Empty"
                }
              }
            }
          },
          "400": {
            "description": "400 response",
            "content": {}
          },
          "500": {
            "description": "500 response",
            "content": {}
          }
        }
      },
      "head": {
        "parameters": [
          {
            "name": "item",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "folder",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "Content-Length": {
                "schema": {
                  "type": "string"
                }
              },
              "Content-Type": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Empty"
                }
              }
            }
          },
          "400": {
            "description": "400 response",
            "content": {}
          },
          "500": {
            "description": "500 response",
            "content": {}
          }
        },
        "security": [
          {
            "sigv4": []
          }
        ]
      }
    },
    "/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "Content-Length": {
                "schema": {
                  "type": "string"
                }
              },
              "Timestamp": {
                "schema": {
                  "type": "string"
                }
              },
              "Content-Type": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Empty"
                }
              }
            }
          },
          "400": {
            "description": "400 response",
            "content": {}
          },
          "500": {
            "description": "500 response",
            "content": {}
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Empty": {
        "title": "Empty Schema",
        "type": "object"
      }
    },
    "securitySchemes": {
      "sigv4": {
        "type": "apiKey",
        "name": "Authorization",
        "in": "header",
        "x-amazon-apigateway-authtype": "awsSigv4"
      }
    }
  }
}

如果你仔細閱讀文檔,你會看到:

有效負載大小限制為 10 MB。 請參閱 API 網關配額以配置和運行 REST API。

因為有效負載也是 base64 編碼的,這意味着在實踐中,即使文件小於 10MB,您也會達到限制。

API 網關不應該用於服務大文件。 推薦的方法是使用 API 網關和 Lambda 集成來創建預簽名的 url 並將您的用戶重定向到那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM