Über eine API Gateway-API auf Binärdateien in Amazon S3 zugreifen
Die folgenden Beispiele zeigen die OpenAPI-Datei, die für den Zugriff auf Bilder in Amazon S3 verwendet wird, wie man ein Bild von Amazon S3 herunterlädt und wie man ein Bild in Amazon S3 hochlädt.
OpenAPI-Datei einer Beispiel-API für den Zugriff auf Bilder in Amazon S3
Die folgende OpenAPI-Datei zeigt eine Beispiel-API, die das Herunterladen einer Bilddatei von Amazon S3 und das Hochladen einer Bilddatei in Amazon S3 illustriert. Diese API stellt die Methoden GET /s3?key={file-name}
und PUT /s3?key={file-name}
zum Herunter- und Hochladen einer bestimmten Bilddatei bereit. Die Methode GET
gibt die Bilddatei als base64-kodierte Zeichenfolge als Teil einer JSON-Ausgabe zurück, und zwar nach der bereitgestellten Mapping-Vorlage, in einer Antwort "200 OK". Die Methode PUT
übernimmt einen unformatierten binären Blob als Eingabe und gibt eine Antwort "200 OK" mit einer leeren Nutzlast zurück.
- 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"
}
}
}
Bilddatei von Amazon S3 herunterladen
So laden Sie eine Bilddatei (image.jpg
) als binären Blob von Amazon S3 herunter:
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
Die erfolgreiche Antwort schaut wie folgt aus:
200 OK HTTP/1.1
[raw bytes]
Die Rohbytes werden zurückgegeben, da der Accept
-Header auf den binären Medientyp application/octet-stream
und die Binärunterstützung für die API aktiviert ist.
Eine Alternative besteht darin, die Bilddatei (image.jpg
) als eine base64-kodierte Zeichenfolge (formatiert als JSON-Eigenschaft) von Amazon S3 herunterzuladen. Fügen Sie der 200-Integrationsantwort eine Antwortvorlage hinzu, wie im folgenden fettgedruckten OpenAPI-Definitionsblock gezeigt:
"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}"
}
}
},
Die Anforderung zum Herunterladen der Bilddatei sieht folgendermaßen aus:
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
Die erfolgreiche Antwort sieht folgendermaßen aus:
200 OK HTTP/1.1
{
"image": "W3JhdyBieXRlc10="
}
Bilde in Amazon S3 hochladen
So laden Sie eine Bilddatei (image.jpg
) als binären Blob in Amazon S3 hoch:
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]
Die erfolgreiche Antwort sieht folgendermaßen aus:
200 OK HTTP/1.1
So laden Sie eine Bilddatei (image.jpg
) als base64-kodierte Zeichenfolge in Amazon S3 hoch:
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=
Die Eingabenutzlast muss eine base64-kodierte Zeichenfolge sein, da der Content-Type
-Headerwert auf application/json
festgelegt ist. Die erfolgreiche Antwort sieht folgendermaßen aus:
200 OK HTTP/1.1