搭CreateDeployment配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateDeployment配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用CreateDeployment

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

CLI
AWS CLI

若要將已設定的資源部署API到新的階段

命令:

aws apigateway create-deployment --rest-api-id 1234123412 --stage-name dev --stage-description 'Development Stage' --description 'First deployment to the dev stage'

將已設定的資源部署API到現有階段

命令:

aws apigateway create-deployment --rest-api-id 1234123412 --stage-name dev --description 'Second deployment to the dev stage'

將已設定的資源部署API至具有階段變數的現有階段

aws apigateway 創建部署--rest-api-id 1234123412-階段名稱開發-描述「第三次部署到開發階段」-變量鍵 =「值」,=' 'otherKeyotherValue

  • 如需詳API細資訊,請參閱AWS CLI 指令參考CreateDeployment中的。

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

public static String createNewDeployment(ApiGatewayClient apiGateway, String restApiId, String stageName) { try { CreateDeploymentRequest request = CreateDeploymentRequest.builder() .restApiId(restApiId) .description("Created using the AWS API Gateway Java API") .stageName(stageName) .build(); CreateDeploymentResponse response = apiGateway.createDeployment(request); System.out.println("The id of the deployment is " + response.id()); return response.id(); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • 如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考CreateDeployment中的。

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class ApiGatewayToService: """ Encapsulates Amazon API Gateway functions that are used to create a REST API that integrates with another AWS service. """ def __init__(self, apig_client): """ :param apig_client: A Boto3 API Gateway client. """ self.apig_client = apig_client self.api_id = None self.root_id = None self.stage = None def deploy_api(self, stage_name): """ Deploys a REST API. After a REST API is deployed, it can be called from any REST client, such as the Python Requests package or Postman. :param stage_name: The stage of the API to deploy, such as 'test'. :return: The base URL of the deployed REST API. """ try: self.apig_client.create_deployment( restApiId=self.api_id, stageName=stage_name ) self.stage = stage_name logger.info("Deployed stage %s.", stage_name) except ClientError: logger.exception("Couldn't deploy stage %s.", stage_name) raise else: return self.api_url() def api_url(self, resource=None): """ Builds the REST API URL from its parts. :param resource: The resource path to append to the base URL. :return: The REST URL to the specified resource. """ url = ( f"https://{self.api_id}.execute-api.{self.apig_client.meta.region_name}" f".amazonaws.com/{self.stage}" ) if resource is not None: url = f"{url}/{resource}" return url
  • 如需詳API細資訊,請參閱CreateDeploymentAWS SDK的〈〉以取得 Python (Boto3) API 參考資料。