Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
L'exemple OpenAPI suivant montre comment accéder à un fichier binaire AWS Lambda via une API API Gateway. Cette API expose les méthodes GET /lambda?key={file-name}
et PUT /lambda?key={file-name}
de téléchargement et de chargement d’un fichier image spécifié. La méthode GET
renvoie le fichier image sous la forme d'une chaîne encodée en base64 dans le cadre d'une sortie JSON, selon le modèle de mappage fourni, dans une réponse 200 OK. La méthode PUT
prend un blob binaire brut comme entrée et renvoie une réponse 200 OK avec une charge utile vide.
Vous créez la fonction Lambda que votre API appelle, et elle doit renvoyer une chaîne codée en base64 avec l’en-tête Content-Type
application/json
.
Rubriques
Fichier OpenAPI d’un exemple d’API pour accéder aux images dans Lambda
Le fichier OpenAPI suivant présente un exemple d’API qui illustre le téléchargement d’un fichier image depuis Lambda et le chargement d’un fichier image vers Lambda.
{ "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" } } } }
Téléchargement d’une image depuis Lambda
Pour télécharger un fichier image (image.jpg
) comme blob binaire depuis Lambda :
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
La réponse de réussite ressemble à ce qui suit :
200 OK HTTP/1.1 [raw bytes]
Pour télécharger un fichier image (image.jpg
) sous la forme d’une chaîne encodée en base64, au format d’une propriété JSON, depuis Lambda :
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
La réponse de réussite ressemble à ce qui suit :
200 OK HTTP/1.1 { "image": "W3JhdyBieXRlc10=" }
Chargement d’une image vers Lambda
Pour charger un fichier image (image.jpg
) comme blob binaire vers Lambda :
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]
La réponse de réussite ressemble à ce qui suit :
200 OK
Pour charger un fichier image (image.jpg
) sous la forme d’une chaîne encodée en base64 vers Lambda :
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=
La réponse de réussite ressemble à ce qui suit :
200 OK