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

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

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

GetAuthorizationToken配 AWS SDK或使用 CLI

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

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

CLI
AWS CLI

若要取得預設登錄的授權權杖

下列get-authorization-token範例命令會取得預設登錄的授權憑證。

aws ecr get-authorization-token

輸出:

{ "authorizationData": [ { "authorizationToken": "QVdTOkN...", "expiresAt": 1448875853.241, "proxyEndpoint": "https://123456789012.dkr.ecr.us-west-2.amazonaws.com" } ] }
Java
SDK對於爪哇 2.x
注意

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

/** * Retrieves the authorization token for Amazon Elastic Container Registry (ECR). * This method makes an asynchronous call to the ECR client to retrieve the authorization token. * If the operation is successful, the method prints the token to the console. * If an exception occurs, the method handles the exception and prints the error message. * * @throws EcrException if there is an error retrieving the authorization token from ECR. * @throws RuntimeException if there is an unexpected error during the operation. */ public void getAuthToken() { CompletableFuture<GetAuthorizationTokenResponse> response = getAsyncClient().getAuthorizationToken(); response.whenComplete((authorizationTokenResponse, ex) -> { if (authorizationTokenResponse != null) { AuthorizationData authorizationData = authorizationTokenResponse.authorizationData().get(0); String token = authorizationData.authorizationToken(); if (!token.isEmpty()) { System.out.println("The token was successfully retrieved."); } } else { if (ex.getCause() instanceof EcrException) { throw (EcrException) ex.getCause(); } else { String errorMessage = "Unexpected error occurred: " + ex.getMessage(); throw new RuntimeException(errorMessage, ex); // Rethrow the exception } } }); response.join(); }
Kotlin
SDK對於科特林
注意

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

/** * Retrieves the authorization token for Amazon Elastic Container Registry (ECR). * */ suspend fun getAuthToken() { EcrClient { region = "us-east-1" }.use { ecrClient -> // Retrieve the authorization token for ECR. val response = ecrClient.getAuthorizationToken() val authorizationData = response.authorizationData?.get(0) val token = authorizationData?.authorizationToken if (token != null) { println("The token was successfully retrieved.") } } }
  • 有API關詳細資訊,請參閱GetAuthorizationTokenAWS SDK的以取得 Kotlin API 的參考資料