我在 python cdk 中定義了一個 API 網關,它將接受 CURL Restful 請求以從 S3 存盤桶上傳/讀取/洗掉檔案:
api = api_gw.RestApi(self, "file-api",
rest_api_name="File REST Service")
file = api.root.add_resource("{id}")
get_files_integration = api_gw.LambdaIntegration(handler,
request_templates={"application/json": '{ "statusCode": "200" }'})
post_file_integration = api_gw.LambdaIntegration(handler)
get_file_integration = api_gw.LambdaIntegration(handler)
delete_file_integration = api_gw.LambdaIntegration(handler)
api.root.add_method("GET", get_files_integration, authorization_type=api_gw.AuthorizationType.COGNITO, authorizer=auth)
file.add_method("POST", post_file_integration); # POST /{id}
file.add_method("GET", get_file_integration); # GET /{id}
file.add_method("DELETE", delete_file_integration); # DELETE /{id}
是否可以在 API 網關上啟用 CORS,以便它執行飛行前檢查并允許從另一臺機器上的本地主機進行外部訪問?
我曾嘗試使用我可以找到的檔案中定義的現有 add_core_preflight() 方法,但我認為這可能從 CDK 2.0 開始不再有效。
uj5u.com熱心網友回復:
是的,IResource.add_cors_preflight()正是這樣做的。
您還可以使用default_cors_preflight_options屬性RestApi指定默認 CORS 配置。
以下是檔案中的示例。它們在 Typescript 中,但在 Python 中也一樣。
以下示例將為 API 的所有資源上的所有方法和所有來源啟用 CORS:
new apigateway.RestApi(this, 'api', {
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS,
allowMethods: apigateway.Cors.ALL_METHODS // this is also the default
}
})
以下示例將向 myResource API 資源添加一個 OPTIONS 方法,該方法僅允許來自源https://amazon.com的 GET 和 PUT HTTP 請求。
declare const myResource: apigateway.Resource;
myResource.addCorsPreflight({
allowOrigins: [ 'https://amazon.com' ],
allowMethods: [ 'GET', 'PUT' ]
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/428081.html
上一篇:基于另一個屬性值的動態屬性型別
