D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples GitHub .
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Utilisation GetAuthorizationToken
avec un AWS SDK ou une CLI
Les exemples de code suivants montrent comment utiliserGetAuthorizationToken
.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- CLI
-
- AWS CLI
-
Pour obtenir un jeton d'autorisation pour votre registre par défaut
L'get-authorization-token
exemple de commande suivant permet d'obtenir un jeton d'autorisation pour votre registre par défaut.
aws ecr get-authorization-token
Sortie :
{
"authorizationData": [
{
"authorizationToken": "QVdTOkN...",
"expiresAt": 1448875853.241,
"proxyEndpoint": "https://123456789012.dkr.ecr.us-west-2.amazonaws.com"
}
]
}
- Java
-
- SDK pour 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 pour 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.")
}
}
}