Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Verwendung GetAuthorizationToken
mit einem AWS SDK oder CLI
Die folgenden Code-Beispiele zeigen, wie GetAuthorizationToken
verwendet wird.
Beispiele für Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Im folgenden Codebeispiel können Sie diese Aktion im Kontext sehen:
- CLI
-
- AWS CLI
-
Um ein Autorisierungstoken für Ihre Standardregistrierung zu erhalten
Mit dem folgenden get-authorization-token
Beispielbefehl wird ein Autorisierungstoken für Ihre Standardregistrierung abgerufen.
aws ecr get-authorization-token
Ausgabe:
{
"authorizationData": [
{
"authorizationToken": "QVdTOkN...",
"expiresAt": 1448875853.241,
"proxyEndpoint": "https://123456789012.dkr.ecr.us-west-2.amazonaws.com"
}
]
}
- Java
-
- SDK für Java 2.x
-
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.
/**
* 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 für Kotlin
-
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.
/**
* 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.")
}
}
}
- Python
-
- SDK für Python (Boto3)
-
Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.
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
Eine vollständige Liste der AWS SDK-Entwicklerhandbücher und Codebeispiele finden Sie unter. Amazon ECR mit einem AWS SDK verwenden Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.