API Gateway API を介して Amazon S3 のバイナリファイルにアクセスする - Amazon API Gateway

API Gateway API を介して Amazon S3 のバイナリファイルにアクセスする

次の例は、Amazon S3 でのイメージのアクセスに使用される OpenAPI ファイル、Amazon S3 からイメージをダウンロードする方法、イメージを Amazon S3 にアップロードする方法を示しています。

Amazon S3 でイメージにアクセスするためのサンプル API の OpenAPI ファイル

次の OpenAPI ファイルは、Amazon S3 からのイメージファイルのダウンロードと Amazon S3 へのイメージファイルのアップロードを示すサンプル API を示しています。この API は、指定されたイメージファイルをダウンロードおよびアップロードするための GET /s3?key={file-name} メソッドと PUT /s3?key={file-name} メソッドを開示します。GET メソッドは、200 OK レスポンスにおいて、提供されたマッピングテンプレートに従って、JSON 出力の一部である Base64 でエンコードされた文字列としてイメージファイルを返します。PUT メソッドは、入力として raw バイナリ 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 からイメージをダウンロードする

イメージファイル (image.jpg) を Amazon S3 からバイナリ 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 のバイナリサポートが有効になっているため、raw バイトが返されます。

または、イメージファイル (image.jpg) を、Amazon S3 から、(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 にイメージをアップロードする

イメージファイル (image.jpg) をバイナリ BLOB として Amazon S3 にアップロードするには

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) を Base64 でエンコードされた文字列として Amazon S3 にアップロードするには

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