DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Microservices With Apache Camel and Quarkus (Part 4)
  • MultiCloudJ: Building Cloud-Agnostic Applications in Java
  • How to Migrate from Java 8 to Java 17+ Using Amazon Q Developer
  • Video Generation With Java: Leverage Nova and Amazon Bedrock

Trending

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Going All In On the JVM 25 Virtual Threads
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Get AWS ECR Login Token Using Java and AWS SDK

Get AWS ECR Login Token Using Java and AWS SDK

Want to learn more about how to set up an AWS ECR login token with Java? Check out this post to learn more about using the ECR registry in Rancher.

By 
Rajan Patel
·
Sep. 12, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Hi, guys,

Today, I am going to describe how to get AWS ECR login token with Java. I used this token for the ECR registry in Rancher.  

When you want to get the ECR login token with Java and the AWS SDK, then you can achieve this through the following steps.

  • In pom.xml, add the AWS SDK ECR dependency. You can pass the latest version in it, as shown below:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-ecr</artifactId>
    <version>1.11.375</version>
</dependency>


  • In order to use Amazon web services, the AWSCredential needs to be supplied in AWS SDK. It can be done by following way.  

AWSCredentials awsCredentials = new BasicAWSCredentials(
  <<AWS_ACCESS_KEY>>, <<AWS_SECRET_KEY>>);


  • Build an ECR client with configuring properties:

AmazonECR amazonECR = AmazonECRClientBuilder.standard().withRegion(<<AWS_REGION>>)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
  .build();


  • Now, you need to get the authorization token, which is valid for 12 hours. A list of authorization token data objects that correspond to the registryIds values will be returned by the following code:

GetAuthorizationTokenResult authorizationToken = amazonECR
.getAuthorizationToken(new GetAuthorizationTokenRequest());


  • Now, you will need to fetch the authorization data from the list and then get the encoded token from authorization data. NOTE: Token will be encoded with base64.

List<AuthorizationData> authorizationData = authorizationToken.getAuthorizationData();
String encodedToken = authorizationData.get(0).getAuthorizationToken();


  • This token is encoded with a base64 format. So, it is necessary to decode it into a plain string by the following code.

String token=encodeUtils.decodeStr(encodedToken);


  • Now, this token can be used in the Rancher registry credentials and also for the docker CLI to push and pull images with Amazon ECR.

AWS Software development kit Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Microservices With Apache Camel and Quarkus (Part 4)
  • MultiCloudJ: Building Cloud-Agnostic Applications in Java
  • How to Migrate from Java 8 to Java 17+ Using Amazon Q Developer
  • Video Generation With Java: Leverage Nova and Amazon Bedrock

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook