Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get AWS ECR Login Token Using Java and AWS SDK

DZone's Guide to

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.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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);


Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
aws ,ecr ,java ,tokens ,aws tokens ,login ,sdk

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}