Iraklis Inguana
Iraklis Inguana

Reputation: 89

retrieve AWS data during gradle build

I'm trying to get some values from the AWS (from cognito or s3 for example). What I think is considered as appropriate is that you define a task within build.gradle that executes during build time when gradle is running. I followed the instructions written in documentation (https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/creating-clients.html) but i get a 'cannot resolve symbol' error when I'm trying to use methods from the AWS dependencies I've imported.

These imported dependencies follow the example given in the tutorial:

...
dependencies {
  implementation platform('software.amazon.awssdk:bom:2.X.X')

  implementation 'software.amazon.awssdk:kinesis'
  ...
}

error occurs within gradle in code (doesn't recognise KinesisClient for example):

def task = {
KinesisClient kinesisClient = KinesisClient.builder()
                .region(ARANDROMREGION)
                .build();
}

How can I access AWS values from gradle during build time? This is the bottom question. If the information i gave is unclear please let me know.

Upvotes: 1

Views: 2017

Answers (2)

Iraklis Inguana
Iraklis Inguana

Reputation: 89

Turns out that defining dependencies should be done in the buildscript in order for them to be accessible from gradle during build time:

buildscript {
    repositories {
...
    }
    dependencies {
...
        classpath 'software.amazon.awssdk:kinesis:VERSION'
    }
}

Then you have to import before the task the classes you're going to use, just like you would do in a java class and you're good to go.

Upvotes: 1

smac2020
smac2020

Reputation: 10704

Here is a complete example (both V1 and V2) of using the Java S3 API and a build.gradle file that works

JAVA V1 Here is the S3 Java code:

package com.amazon.s3;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.GetBucketLocationRequest;


import java.io.IOException;

public class CreateBucket {

    public static void main(String[] args) throws IOException {
        Regions clientRegion = Regions.DEFAULT_REGION;
        String bucketName = "My Bucket";

        try {

            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new ProfileCredentialsProvider())
                    .withRegion(clientRegion)
                    .build();

            if (!s3Client.doesBucketExistV2(bucketName)) {
                // Because the CreateBucketRequest object doesn't specify a region, the
                // bucket is created in the region specified in the client.
                s3Client.createBucket(new CreateBucketRequest(bucketName));

                // Verify that the bucket was created by retrieving it and checking its location.
                String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
                System.out.println("Bucket location: " + bucketLocation);
            }
        } catch (AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process
            // it and returned an error response.
            e.printStackTrace();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}

Here is the build.gradle file:

plugins {
    id 'java'
}

group 'AWSS3_Gra'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.228')
    implementation 'com.amazonaws:aws-java-sdk-s3'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

JAVA V2

Here is the S3 Java code:

package com.example.s3;


import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import java.util.List;
import java.util.ListIterator;


public class ListObjects {

    public static void main(String[] args) {


        String bucketName = "BucketName";
        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder()
                .region(region)
                .build();

        ListBucketObjects(s3, bucketName);
    }


    public static void ListBucketObjects(  S3Client s3, String bucketName ) {

        try {
            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
            }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
    //convert bytes to kbs
    private static long calKb(Long val) {
        return val/1024;

    }

}

build file is:

group 'aws.test'
version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation platform('software.amazon.awssdk:bom:2.10.30')
    implementation 'software.amazon.awssdk:s3'
    testImplementation group: 'junit', name: 'junit', version: '4.11'
}

Hope this helps...

PS - here is the link to the AWS DEV guide for this information: https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/setup-project-gradle.html

Upvotes: 2

Related Questions