

# API Gateway API를 통해 Amazon S3에서 이진 파일에 액세스
<a name="api-gateway-content-encodings-examples-image-s3"></a>

다음 예에서는 Amazon S3에서 이미지에 액세스하는 데 사용되는 OpenAPI 파일, Amazon S3에서 이미지를 다운로드하는 방법, Amazon S3에 이미지를 업로드하는 방법을 보여줍니다.

**Topics**
+ [Amazon S3에서 이미지에 액세스하는 샘플 API의 OpenAPI 파일](#api-gateway-content-encodings-example-image-s3-swagger-file)
+ [Amazon S3에서 이미지 다운로드](#api-gateway-content-encodings-example-download-image-from-s3)
+ [Amazon S3에 이미지 업로드](#api-gateway-content-encodings-example-upload-image-to-s3)

## Amazon S3에서 이미지에 액세스하는 샘플 API의 OpenAPI 파일
<a name="api-gateway-content-encodings-example-image-s3-swagger-file"></a>

다음 OpenAPI 파일은 Amazon S3에서 이미지 파일을 다운로드하거나 Amazon S3에 업로드하는 방법을 설명하는 샘플 API를 보여줍니다. 이 API는 지정된 이미지 파일을 다운로드하거나 업로드하기 위한 `GET /s3?key={file-name}` 및 `PUT /s3?key={file-name}` 메서드를 노출합니다. `GET` 메서드는 200 OK 응답에서 제공된 매핑 템플릿에 따라 JSON 출력의 일부로서 이미지 파일을 Base64로 인코딩된 문자열 형태로 반환합니다. `PUT` 메서드는 원시 이진 BLOB을 입력으로 사용하여 빈 페이로드를 포함하는 200 OK 응답을 반환합니다.

------
#### [ OpenAPI 3.0 ]

```
{
   "openapi": "3.0.0",
   "info": {
      "version": "2016-10-21T17:26:28Z",
      "title": "ApiName"
   },
   "paths": {
      "/s3": {
         "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": {
               "credentials": "arn:aws:iam::123456789012:role/binarySupportRole",
               "responses": {
                  "default": {
                     "statusCode": "500"
                  },
                  "2\\d{2}": {
                     "statusCode": "200"
                  }
               },
               "requestParameters": {
                  "integration.request.path.key": "method.request.querystring.key"
               },
               "uri": "arn:aws:apigateway:us-west-2:s3:path/{key}",
               "passthroughBehavior": "when_no_match",
               "httpMethod": "GET",
               "type": "aws"
            }
         },
         "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": {
               "credentials": "arn:aws:iam::123456789012:role/binarySupportRole",
               "responses": {
                  "default": {
                     "statusCode": "500"
                  },
                  "2\\d{2}": {
                     "statusCode": "200"
                  }
               },
               "requestParameters": {
                  "integration.request.path.key": "method.request.querystring.key"
               },
               "uri": "arn:aws:apigateway:us-west-2:s3:path/{key}",
               "passthroughBehavior": "when_no_match",
               "httpMethod": "PUT",
               "type": "aws",
               "contentHandling": "CONVERT_TO_BINARY"
            }
         }
      }
   },
   "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"
         }
      }
   }
}
```

------
#### [ OpenAPI 2.0 ]

```
{
  "swagger": "2.0",
  "info": {
    "version": "2016-10-21T17:26:28Z",
    "title": "ApiName"
  },
  "host": "abcdefghi.execute-api.us-east-1.amazonaws.com",
  "basePath": "/v1",
  "schemes": [
    "https"
  ],
  "paths": {
    "/s3": {
      "get": {
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "required": false,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          },
          "500": {
            "description": "500 response"
          }
        },
        "x-amazon-apigateway-integration": {
          "credentials": "arn:aws:iam::123456789012:role/binarySupportRole",
          "responses": {
            "default": {
              "statusCode": "500"
            },
            "2\\d{2}": {
              "statusCode": "200"            }
          },
          "requestParameters": {
            "integration.request.path.key": "method.request.querystring.key"
          },
          "uri": "arn:aws:apigateway:us-west-2:s3:path/{key}",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "GET",
          "type": "aws"
        }
      },
      "put": {
        "produces": [
          "application/json", "application/octet-stream"
        ],
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "required": false,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          },
          "500": {
            "description": "500 response"
          }
        },
        "x-amazon-apigateway-integration": {
          "credentials": "arn:aws:iam::123456789012:role/binarySupportRole",
          "responses": {
            "default": {
              "statusCode": "500"
            },
            "2\\d{2}": {
              "statusCode": "200"
            }
          },
          "requestParameters": {
            "integration.request.path.key": "method.request.querystring.key"
          },
          "uri": "arn:aws:apigateway:us-west-2:s3:path/{key}",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "PUT",
          "type": "aws",
          "contentHandling" : "CONVERT_TO_BINARY"
        }
      }
    }
  },
  "x-amazon-apigateway-binary-media-types" : ["application/octet-stream", "image/jpeg"],
  "definitions": {
    "Empty": {
      "type": "object",
      "title": "Empty Schema"
    }
  }
}
```

------

## Amazon S3에서 이미지 다운로드
<a name="api-gateway-content-encodings-example-download-image-from-s3"></a>

Amazon S3에서 이미지 파일(`image.jpg`)을 이진 BLOB으로 다운로드하려면

```
GET /v1/s3?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]
```

`Accept` 헤더가 `application/octet-stream`의 이진 미디어 유형으로 설정되고 API에 대해 이진 지원이 활성화되어 있으므로 원시 바이트가 반환됩니다.

또는 Amazon S3에서 이미지 파일(`image.jpg`)을 JSON 속성으로 형식 지정되고 Base64로 인코딩된 문자열로 다운로드하려면, 아래 굵은 글꼴의 OpenAPI 정의 블록에서와 같이 200 통합 응답에 응답 템플릿을 추가합니다.

```
        "x-amazon-apigateway-integration": {
          "credentials": "arn:aws:iam::123456789012:role/binarySupportRole",
          "responses": {
            "default": {
              "statusCode": "500"
            },
            "2\\d{2}": {
              "statusCode": "200",
              "responseTemplates": {
                "application/json": "{\n   \"image\": \"$input.body\"\n}"
              }
            }
          },
```

이미지 파일 다운로드 요청은 다음과 같습니다.

```
GET /v1/s3?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="
}
```

## Amazon S3에 이미지 업로드
<a name="api-gateway-content-encodings-example-upload-image-to-s3"></a>

이미지 파일(`image.jpg`)을 Amazon S3에 이진 BLOB으로 업로드하려면

```
PUT /v1/s3?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 HTTP/1.1        
```

이미지 파일(`image.jpg`)을 Amazon S3에 Base64로 인코딩된 문자열로 업로드하려면

```
PUT /v1/s3?key=image.jpg HTTP/1.1
Host: abcdefghi.execute-api.us-east-1.amazonaws.com
Content-Type: application/json
Accept: application/json

W3JhdyBieXRlc10=
```

`Content-Type` 헤더 값이 `application/json`으로 설정되어 있으므로 입력 페이로드는 Base64로 인코딩된 문자열이어야 합니다. 성공적인 응답은 다음과 같습니다.

```
200 OK HTTP/1.1
```