Search in sources :

Example 76 with S3Client

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

the class DisplayFacesFrame method displayAllFaces.

// snippet-start:[rekognition.java2.display_faces.main]
public static void displayAllFaces(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
    int height = 0;
    int width = 0;
    byte[] data = getObjectBytes(s3, bucketName, sourceImage);
    InputStream is = new ByteArrayInputStream(data);
    try {
        SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
        image = ImageIO.read(sourceBytes.asInputStream());
        width = image.getWidth();
        height = image.getHeight();
        // Create an Image object for the source image
        software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectFacesRequest facesRequest = DetectFacesRequest.builder().attributes(Attribute.ALL).image(souImage).build();
        result = rekClient.detectFaces(facesRequest);
        // Show the bounding box info for each face.
        List<FaceDetail> faceDetails = result.faceDetails();
        for (FaceDetail face : faceDetails) {
            BoundingBox box = face.boundingBox();
            float left = width * box.left();
            float top = height * box.top();
            System.out.println("Face:");
            System.out.println("Left: " + (int) left);
            System.out.println("Top: " + (int) top);
            System.out.println("Face Width: " + (int) (width * box.width()));
            System.out.println("Face Height: " + (int) (height * box.height()));
            System.out.println();
        }
        // Create the frame and panel.
        JFrame frame = new JFrame("RotateImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DisplayFacesFrame panel = new DisplayFacesFrame(image);
        panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale));
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) SdkBytes(software.amazon.awssdk.core.SdkBytes) Image(software.amazon.awssdk.services.rekognition.model.Image) software.amazon.awssdk.services.rekognition.model(software.amazon.awssdk.services.rekognition.model)

Example 77 with S3Client

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

the class DeleteWebsiteConfiguration method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage: " + "   <bucketName>\n\n" + "Where:\n" + "   bucketName - the Amazon S3 bucket to delete the website configuration from.\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    System.out.format("Deleting website configuration for Amazon S3 bucket: %s\n", bucketName);
    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    deleteBucketWebsiteConfig(s3, bucketName);
    System.out.println("Done!");
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 78 with S3Client

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

the class MovieLensDatasetProvider method getObjectBytes.

// Checks to see if the dataset is already uploaded to s3.
public static boolean getObjectBytes(S3Client s3Client, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data.length > 0;
    } catch (NoSuchKeyException | NoSuchBucketException ex) {
        return false;
    } catch (S3Exception s3Exception) {
        System.err.println(s3Exception.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return false;
}
Also used : NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) NoSuchBucketException(software.amazon.awssdk.services.s3.model.NoSuchBucketException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 79 with S3Client

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

the class MovieLensDatasetProvider method uploadMovieLensDatasetToS3.

public static void uploadMovieLensDatasetToS3(S3Client s3Client, String bucket, DatasetType type, String path) throws IOException {
    CSVReader reader = new CSVReader(new BufferedReader(new InputStreamReader(new FileInputStream("./movie-lens-ds/ml-100k/u.data"))), '\t');
    StringWriter sw = new StringWriter();
    CSVWriter pw = new CSVWriter(sw);
    pw.writeNext(new String[] { "USER_ID", "ITEM_ID", "TIMESTAMP" });
    String[] row = null;
    while ((row = reader.readNext()) != null) {
        int rating = Integer.parseInt(row[2]);
        if (rating > 3) {
            pw.writeNext(new String[] { row[0], row[1], row[3] });
        }
    }
    reader.close();
    pw.close();
    String data = sw.toString();
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucket).key(path).build();
        s3Client.putObject(objectRequest, RequestBody.fromBytes(data.getBytes()));
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) CSVReader(au.com.bytecode.opencsv.CSVReader) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) BufferedReader(java.io.BufferedReader) CSVWriter(au.com.bytecode.opencsv.CSVWriter) FileInputStream(java.io.FileInputStream) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Example 80 with S3Client

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

the class DemoUtils method ensurePersonalizePermissionsOnS3Bucket.

public static void ensurePersonalizePermissionsOnS3Bucket(S3Client s3, String bucket) {
    final String bucketPolicy = BUCKET_POLICY_TEMPLATE.replace("{bucket}", bucket);
    // System.out.println("Bucket policy: " + bucketPolicy);
    PutBucketPolicyRequest policyRequest = PutBucketPolicyRequest.builder().bucket(bucket).policy(bucketPolicy).build();
    s3.putBucketPolicy(policyRequest);
}
Also used : PutBucketPolicyRequest(software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest)

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