文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS 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" } ] }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetAuthorizationToken
。
-
- Java
-
- SDK for Java 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(); }
-
如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 GetAuthorizationToken。
-
- Kotlin
-
- SDK for Kotlin
-
注意
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 詳細資訊,請參閱 SDK AWS for Kotlin API 參考中的 GetAuthorizationToken
。
-
- Python
-
- SDK for Python (Boto3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 class ECRWrapper: def __init__(self, ecr_client: client): self.ecr_client = ecr_client @classmethod def from_client(cls) -> "ECRWrapper": """ Creates a ECRWrapper instance with a default Amazon ECR client. :return: An instance of ECRWrapper initialized with the default Amazon ECR client. """ ecr_client = boto3.client("ecr") return cls(ecr_client) def get_authorization_token(self) -> str: """ Gets an authorization token for an ECR repository. :return: The authorization token. """ try: response = self.ecr_client.get_authorization_token() return response["authorizationData"][0]["authorizationToken"] except ClientError as err: logger.error( "Couldn't get authorization token. Here's why %s", err.response["Error"]["Message"], ) raise
-
如需 API 詳細資訊,請參閱《適用於 AWS Python 的 SDK (Boto3) API 參考》中的 GetAuthorizationToken。
-
DescribeRepositories
GetRepositoryPolicy