Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use GetAuthorizationToken
com um AWS SDK ou CLI
Os exemplos de códigos a seguir mostram como usar GetAuthorizationToken
.
Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação no contexto no seguinte exemplo de código:
- CLI
-
- AWS CLI
-
Para obter um token de autorização para seu registro padrão
O comando de get-authorization-token
exemplo a seguir obtém um token de autorização para seu registro padrão.
aws ecr get-authorization-token
Saída:
{
"authorizationData": [
{
"authorizationToken": "QVdTOkN...",
"expiresAt": 1448875853.241,
"proxyEndpoint": "https://123456789012.dkr.ecr.us-west-2.amazonaws.com"
}
]
}
- Java
-
- SDK para Java 2.x
-
/**
* 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 para Kotlin
-
/**
* 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.")
}
}
}