다음 OpenAPI 예에서는 API Gateway API를 통해 AWS Lambda에서 이진 파일에 액세스하는 방법을 보여줍니다. 이 API는 지정된 이미지 파일을 다운로드하거나 업로드하기 위한 GET /lambda?key={file-name}
및 PUT /lambda?key={file-name}
메서드를 노출합니다. GET
메서드는 200 OK 응답에서 제공된 매핑 템플릿에 따라 JSON 출력의 일부로서 이미지 파일을 Base64로 인코딩된 문자열 형태로 반환합니다. PUT
메서드는 원시 이진 BLOB을 입력으로 사용하여 빈 페이로드를 포함하는 200 OK 응답을 반환합니다.
API가 직접적으로 호출하는 Lambda 함수를 생성합니다. 이 함수는 Content-Type
헤더가 application/json
인 base64 인코딩 문자열을 반환해야 합니다.
Lambda에서 이미지에 액세스하는 샘플 API의 OpenAPI 파일
다음 OpenAPI 파일은 Lambda에서 이미지 파일을 다운로드하거나 Lambda에 업로드하는 방법을 설명하는 예제 API를 보여줍니다.
{ "openapi": "3.0.0", "info": { "version": "2016-10-21T17:26:28Z", "title": "ApiName" }, "paths": { "/lambda": { "get": { "parameters": [ { "name": "key", "in": "query", "required": false, "schema": { "type": "string" } } ], "responses": { "200": { "description": "200 response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Empty" } } } }, "500": { "description": "500 response" } }, "x-amazon-apigateway-integration": { "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:image/invocations", "type": "AWS", "credentials": "arn:aws:iam::123456789012:role/Lambda", "httpMethod": "POST", "requestTemplates": { "application/json": "{\n \"imageKey\": \"$input.params('key')\"\n}" }, "responses": { "default": { "statusCode": "500" }, "2\\d{2}": { "statusCode": "200", "responseTemplates": { "application/json": "{\n \"image\": \"$input.body\"\n}" } } } } }, "put": { "parameters": [ { "name": "key", "in": "query", "required": false, "schema": { "type": "string" } } ], "responses": { "200": { "description": "200 response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Empty" } }, "application/octet-stream": { "schema": { "$ref": "#/components/schemas/Empty" } } } }, "500": { "description": "500 response" } }, "x-amazon-apigateway-integration": { "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:image/invocations", "type": "AWS", "credentials": "arn:aws:iam::123456789012:role/Lambda", "httpMethod": "POST", "contentHandling": "CONVERT_TO_TEXT", "requestTemplates": { "application/json": "{\n \"imageKey\": \"$input.params('key')\", \"image\": \"$input.body\"\n}" }, "responses": { "default": { "statusCode": "500" }, "2\\d{2}": { "statusCode": "200" } } } } } }, "x-amazon-apigateway-binary-media-types": [ "application/octet-stream", "image/jpeg" ], "servers": [ { "url": "https://abcdefghi.execute-api.us-east-1.amazonaws.com/{basePath}", "variables": { "basePath": { "default": "/v1" } } } ], "components": { "schemas": { "Empty": { "type": "object", "title": "Empty Schema" } } } }
Lambda에서 이미지 다운로드
Lambda에서 이미지 파일(image.jpg
)을 이진 BLOB으로 다운로드하려면
GET /v1/lambda?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/json Accept: application/octet-stream
성공적인 응답은 다음과 같습니다.
200 OK HTTP/1.1 [raw bytes]
Lambda에서 이미지 파일(image.jpg
)을 JSON 속성으로 형식 지정되고 Base64로 인코딩된 문자열로 다운로드하려면
GET /v1/lambda?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/json Accept: application/json
성공적인 응답은 다음과 같습니다.
200 OK HTTP/1.1 { "image": "W3JhdyBieXRlc10=" }
Lambda에 이미지 업로드
이미지 파일(image.jpg
)을 Lambda에 이진 BLOB으로 업로드하려면
PUT /v1/lambda?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/octet-stream Accept: application/json [raw bytes]
성공적인 응답은 다음과 같습니다.
200 OK
이미지 파일(image.jpg
)을 Lambda에 base64로 인코딩된 문자열로 업로드하려면
PUT /v1/lambda?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/json Accept: application/json W3JhdyBieXRlc10=
성공적인 응답은 다음과 같습니다.
200 OK