Search in sources :

Example 61 with S3Client

use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class PersonalizeDemoOnMovieLens20M method main.

public static void main(String[] args) throws Exception {
    // Pre-flight step - 1
    // Set credentials provider, s3 and personalize clients
    // AWSStaticCredentialsProvider cred = getCredentialsProvider();
    S3Client s3Client = S3Client.builder().region(region).build();
    PersonalizeEventsClient personalizeEventsClient = PersonalizeEventsClient.builder().region(region).build();
    PersonalizeClient personalizeClient = PersonalizeClient.builder().region(region).build();
    PersonalizeRuntimeClient personalizeRuntimeClient = PersonalizeRuntimeClient.builder().region(region).build();
    IamClient iamClient = IamClient.builder().region(Region.AWS_GLOBAL).build();
    // Pre-flight step - 2
    // Identify your datasets and export them to S3 bucket
    DatasetProvider datasetProvider = new MovieLensDatasetProvider();
    datasetProvider.exportDatasetToS3(DatasetType.INTERACTIONS, s3Client, S3_BUCKET, true);
    // Pre-flight step - 3
    // Personalize needs the ability to assume Roles in AWS in order to have the permissions to execute certain tasks, the lines below grant that.
    DemoUtils.ensurePersonalizePermissionsOnS3Bucket(s3Client, S3_BUCKET);
    // Pre-flight step - 4
    // Ensure S3 bucket is accessible by Personalize
    String role = PREFIX + "-role";
    String roleArn = DemoUtils.createPersonalizeRole(iamClient, role);
    // Step 1
    // Create dataset group
    // Create dataset schemas
    // Create datasets
    String datasetGroupArn = createSchemaAndDatasets(personalizeClient, datasetProvider, roleArn);
    // step 2
    // create solution and solution version
    final String userPersonalizationRecipeArn = "arn:aws:personalize:::recipe/aws-user-personalization";
    final String userPersonalizationSolutionName = PREFIX + "-user-personalization-solution";
    final String awsUserPersonalizeSVArn = createSolutionAndSolutionVersion(personalizeClient, datasetGroupArn, userPersonalizationRecipeArn, userPersonalizationSolutionName);
    System.out.println("AWS User Personalization solution and solution version created");
    final String simsRecipeArn = "arn:aws:personalize:::recipe/aws-sims";
    final String simsSolutionName = PREFIX + "-sims-solution";
    final String simsSVArn = createSolutionAndSolutionVersion(personalizeClient, datasetGroupArn, simsRecipeArn, simsSolutionName);
    System.out.println("SIMS solution and solution version created");
    // step 3
    // setup campaign
    final String userPersonalizationCampaignName = PREFIX + "-user-personalization-campaign";
    final CampaignManager userPersonalizationCM = new CampaignManager(personalizeClient, userPersonalizationCampaignName, awsUserPersonalizeSVArn);
    final String userPersonalizationCampaignArn = userPersonalizationCM.createAndWaitForResource(true);
    System.out.println("AWS User Personalization campaign deployed");
    final String simsCampaignName = PREFIX + "-sims-campaign";
    final CampaignManager simsCM = new CampaignManager(personalizeClient, simsCampaignName, simsSVArn);
    final String simsCampaignArn = simsCM.createAndWaitForResource(true);
    System.out.println("SIMS campaign deployed");
    // Step 4
    // Create event tracker for real time events
    // AmazonPersonalizeEvents personalizeEvents = AmazonPersonalizeEventsClientBuilder.standard().withCredentials(cred).build();
    final String eventTrackerName = PREFIX + "-event-tracker";
    final EventTrackerManager etm = new EventTrackerManager(personalizeClient, eventTrackerName, datasetGroupArn);
    final String eventTackerArn = etm.createAndWaitForResource(true);
    final String eventTrackingId = etm.getTrackingId(eventTackerArn);
    System.out.println("Event tracker created");
    // step 5
    // create runtime client for demo
    runWebDemo(personalizeRuntimeClient, personalizeEventsClient, userPersonalizationCampaignArn, simsCampaignArn, eventTrackingId, datasetProvider);
}
Also used : PersonalizeClient(software.amazon.awssdk.services.personalize.PersonalizeClient) PersonalizeRuntimeClient(software.amazon.awssdk.services.personalizeruntime.PersonalizeRuntimeClient) IamClient(software.amazon.awssdk.services.iam.IamClient) CampaignManager(com.amazonaws.personalize.client.resource.CampaignManager) MovieLensDatasetProvider(com.amazonaws.personalize.client.datasets.MovieLensDatasetProvider) S3Client(software.amazon.awssdk.services.s3.S3Client) PersonalizeEventsClient(software.amazon.awssdk.services.personalizeevents.PersonalizeEventsClient) EventTrackerManager(com.amazonaws.personalize.client.resource.EventTrackerManager) DatasetProvider(com.amazonaws.personalize.client.datasets.DatasetProvider) MovieLensDatasetProvider(com.amazonaws.personalize.client.datasets.MovieLensDatasetProvider)

Example 62 with S3Client

use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method getObjectBytes.

public byte[] getObjectBytes(String bucketName, String keyName) {
    S3Client s3 = getClient();
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        // Return the byte[] from this object.
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        return objectBytes.asByteArray();
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Client(software.amazon.awssdk.services.s3.S3Client) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 63 with S3Client

use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method listBucketObjects.

// Returns the names of all images in the given bucket.
public List<String> listBucketObjects(String bucketName) {
    S3Client s3 = getClient();
    String keyName;
    List<String> keys = new ArrayList<>();
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            keyName = myValue.key();
            keys.add(keyName);
        }
        return keys;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 64 with S3Client

use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method tagAssets.

// tag assets with labels in the given list.
public void tagAssets(List myList, String bucketName) {
    try {
        S3Client s3 = getClient();
        int len = myList.size();
        String assetName = "";
        String labelName = "";
        String labelValue = "";
        // tag all the assets in the list.
        for (Object o : myList) {
            // Need to get the WorkItem from each list.
            List innerList = (List) o;
            for (Object value : innerList) {
                WorkItem workItem = (WorkItem) value;
                assetName = workItem.getKey();
                labelName = workItem.getName();
                labelValue = workItem.getConfidence();
                tagExistingObject(s3, bucketName, assetName, labelName, labelValue);
            }
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ArrayList(java.util.ArrayList) List(java.util.List) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 65 with S3Client

use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class IAMScenario method assumeGivenRole.

// Invoke an Amazon S3 operation using the Assumed Role.
public static void assumeGivenRole(String roleArn, String roleSessionName, String bucketName) {
    StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();
    try {
        AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
        AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
        Credentials myCreds = roleResponse.credentials();
        String key = myCreds.accessKeyId();
        String secKey = myCreds.secretAccessKey();
        String secToken = myCreds.sessionToken();
        // List all objects in an Amazon S3 bucket using the temp creds.
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(key, secKey, secToken))).region(region).build();
        System.out.println("Created a S3Client using temp credentials.");
        System.out.println("Listing objects in " + bucketName);
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            System.out.println("The name of the key is " + myValue.key());
            System.out.println("The owner is " + myValue.owner());
        }
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) AssumeRoleRequest(software.amazon.awssdk.services.sts.model.AssumeRoleRequest) ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) StsException(software.amazon.awssdk.services.sts.model.StsException) AssumeRoleResponse(software.amazon.awssdk.services.sts.model.AssumeRoleResponse) Region(software.amazon.awssdk.regions.Region) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) Credentials(software.amazon.awssdk.services.sts.model.Credentials)

Aggregations

S3Client (software.amazon.awssdk.services.s3.S3Client)63 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)53 Region (software.amazon.awssdk.regions.Region)43 ArrayList (java.util.ArrayList)15 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)12 S3Object (software.amazon.awssdk.services.s3.model.S3Object)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)10 List (java.util.List)7 IOException (java.io.IOException)6 RequestBody (software.amazon.awssdk.core.sync.RequestBody)6 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)6 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)5 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)5 SupplierEx (com.hazelcast.function.SupplierEx)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3