Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Contoh berikut menunjukkan file OpenAPI yang digunakan untuk mengakses gambar di Amazon S3, cara mengunduh gambar dari Amazon S3, dan cara mengunggah gambar ke Amazon S3.
Topik
File OpenAPI dari API sampel untuk mengakses gambar di Amazon S3
File OpenAPI berikut menunjukkan contoh API yang menggambarkan mengunduh file gambar dari Amazon S3 dan mengunggah file gambar ke Amazon S3. API ini mengekspos GET /s3?key={file-name}
dan PUT /s3?key={file-name}
metode untuk mengunduh dan mengunggah file gambar tertentu. GET
Metode mengembalikan file gambar sebagai string yang dikodekan base64 sebagai bagian dari output JSON, mengikuti template pemetaan yang disediakan, dalam respons 200 OK. PUT
Metode ini mengambil gumpalan biner mentah sebagai input dan mengembalikan respons 200 OK dengan muatan kosong.
{ "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" } } } }
Unduh gambar dari Amazon S3
Untuk mengunduh file gambar (image.jpg
) sebagai gumpalan biner dari Amazon S3:
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
Respons yang berhasil terlihat seperti ini:
200 OK HTTP/1.1 [raw bytes]
Byte mentah dikembalikan karena Accept
header disetel ke jenis media biner application/octet-stream
dan dukungan biner diaktifkan untuk API.
Atau, untuk mengunduh file gambar (image.jpg
) sebagai string yang disandikan base64 (diformat sebagai properti JSON) dari Amazon S3, tambahkan template respons ke respons integrasi 200, seperti yang ditunjukkan pada blok definisi OpenAPI berwajah tebal berikut:
"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}" } } },
Permintaan untuk mengunduh file gambar terlihat seperti berikut:
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
Respons yang berhasil terlihat seperti berikut:
200 OK HTTP/1.1 { "image": "W3JhdyBieXRlc10=" }
Unggah gambar ke Amazon S3
Untuk mengunggah file gambar (image.jpg
) sebagai gumpalan biner ke 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]
Respons yang berhasil terlihat seperti berikut:
200 OK HTTP/1.1
Untuk mengunggah file gambar (image.jpg
) sebagai string yang dikodekan base64 ke 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=
Muatan input harus berupa string yang dikodekan base64 karena nilai Content-Type
header diatur ke. application/json
Respons yang berhasil terlihat seperti berikut:
200 OK HTTP/1.1