Search in sources :

Example 6 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class SendMessage method send.

public static void send(SesClient client, String sender, String recipient, String subject, String bodyText, String bodyHTML) throws AddressException, MessagingException, IOException {
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object.
    MimeMessage message = new MimeMessage(session);
    // Add subject, from and to lines.
    message.setSubject(subject, "UTF-8");
    message.setFrom(new InternetAddress(sender));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
    // Create a multipart/alternative child container.
    MimeMultipart msgBody = new MimeMultipart("alternative");
    // Create a wrapper for the HTML and text parts.
    MimeBodyPart wrap = new MimeBodyPart();
    // Define the text part.
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(bodyText, "text/plain; charset=UTF-8");
    // Define the HTML part.
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
    // Add the text and HTML parts to the child container.
    msgBody.addBodyPart(textPart);
    msgBody.addBodyPart(htmlPart);
    // Add the child container to the wrapper object.
    wrap.setContent(msgBody);
    // Create a multipart/mixed parent container.
    MimeMultipart msg = new MimeMultipart("mixed");
    // Add the parent container to the message.
    message.setContent(msg);
    // Add the multipart/alternative part to the message.
    msg.addBodyPart(wrap);
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        message.writeTo(outputStream);
        ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
        byte[] arr = new byte[buf.remaining()];
        buf.get(arr);
        SdkBytes data = SdkBytes.fromByteArray(arr);
        RawMessage rawMessage = RawMessage.builder().data(data).build();
        SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
        client.sendRawEmail(rawEmailRequest);
    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) Session(javax.mail.Session)

Example 7 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class AnalyzePhotos method DetectLabels.

public ArrayList DetectLabels(byte[] bytes, String key) {
    Region region = Region.US_EAST_2;
    RekognitionAsyncClient rekAsyncClient = RekognitionAsyncClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    try {
        final AtomicReference<ArrayList<WorkItem>> reference = new AtomicReference<>();
        SdkBytes sourceBytes = SdkBytes.fromByteArray(bytes);
        // Create an Image object for the source image.
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(souImage).maxLabels(10).build();
        CompletableFuture<DetectLabelsResponse> futureGet = rekAsyncClient.detectLabels(detectLabelsRequest);
        futureGet.whenComplete((resp, err) -> {
            try {
                if (resp != null) {
                    List<Label> labels = resp.labels();
                    System.out.println("Detected labels for the given photo");
                    ArrayList list = new ArrayList<WorkItem>();
                    WorkItem item;
                    for (Label label : labels) {
                        item = new WorkItem();
                        // identifies the photo
                        item.setKey(key);
                        item.setConfidence(label.confidence().toString());
                        item.setName(label.name());
                        list.add(item);
                    }
                    reference.set(list);
                } else {
                    err.printStackTrace();
                }
            } finally {
                // Only close the client when you are completely done with it
                rekAsyncClient.close();
            }
        });
        futureGet.join();
        // Use the AtomicReference object to return the ArrayList<WorkItem> collection.
        return reference.get();
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : RekognitionAsyncClient(software.amazon.awssdk.services.rekognition.RekognitionAsyncClient) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) SdkBytes(software.amazon.awssdk.core.SdkBytes) Region(software.amazon.awssdk.regions.Region)

Example 8 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class PutItemEncrypt method putItemInTable.

// snippet-start:[dynamodb.java2.put_item_enc.main]
public static void putItemInTable(DynamoDbClient ddb, KmsClient kmsClient, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal, String keyId) {
    HashMap<String, AttributeValue> itemValues = new HashMap<String, AttributeValue>();
    // Encrypt the albumTitleValue before writing it to the table.
    SdkBytes myBytes = SdkBytes.fromUtf8String(albumTitleValue);
    EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
    EncryptResponse response = kmsClient.encrypt(encryptRequest);
    // Get the encrypted data.
    SdkBytes encryptedData = response.ciphertextBlob();
    // Add content to the table.
    itemValues.put(key, AttributeValue.builder().s(keyVal).build());
    itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
    itemValues.put(albumTitle, AttributeValue.builder().bs(encryptedData).build());
    itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
    PutItemRequest request = PutItemRequest.builder().tableName(tableName).item(itemValues).build();
    try {
        ddb.putItem(request);
        System.out.println(tableName + " was successfully updated");
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 9 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class PutRecord method putSingleRecord.

// snippet-start:[firehose.java2.put_record.main]
public static void putSingleRecord(FirehoseClient firehoseClient, String textValue, String streamName) {
    try {
        SdkBytes sdkBytes = SdkBytes.fromByteArray(textValue.getBytes());
        Record record = Record.builder().data(sdkBytes).build();
        PutRecordRequest recordRequest = PutRecordRequest.builder().deliveryStreamName(streamName).record(record).build();
        PutRecordResponse recordResponse = firehoseClient.putRecord(recordRequest);
        System.out.println("The record ID is " + recordResponse.recordId());
    } catch (FirehoseException e) {
        System.out.println(e.getLocalizedMessage());
        System.exit(1);
    }
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) PutRecordResponse(software.amazon.awssdk.services.firehose.model.PutRecordResponse) PutRecordRequest(software.amazon.awssdk.services.firehose.model.PutRecordRequest) FirehoseException(software.amazon.awssdk.services.firehose.model.FirehoseException) Record(software.amazon.awssdk.services.firehose.model.Record)

Example 10 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class DetectLabels method detectImageLabels.

// snippet-start:[rekognition.java2.detect_labels.main]
public static void detectImageLabels(RekognitionClient rekClient, String sourceImage) {
    try {
        InputStream sourceStream = new URL("https://images.unsplash.com/photo-1557456170-0cf4f4d0d362?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bGFrZXxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80").openStream();
        // InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        // Create an Image object for the source image.
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(souImage).maxLabels(10).build();
        DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
        List<Label> labels = labelsResponse.labels();
        System.out.println("Detected labels for the given photo");
        for (Label label : labels) {
            System.out.println(label.name() + ": " + label.confidence().toString());
        }
    } catch (RekognitionException | FileNotFoundException | MalformedURLException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Label(software.amazon.awssdk.services.rekognition.model.Label) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Image(software.amazon.awssdk.services.rekognition.model.Image) URL(java.net.URL) DetectLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) DetectLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)

Aggregations

SdkBytes (software.amazon.awssdk.core.SdkBytes)37 InputStream (java.io.InputStream)14 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)13 Image (software.amazon.awssdk.services.rekognition.model.Image)13 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ByteBuffer (java.nio.ByteBuffer)9 Properties (java.util.Properties)9 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)9 MimeBodyPart (javax.mail.internet.MimeBodyPart)9 MimeMessage (javax.mail.internet.MimeMessage)9 MimeMultipart (javax.mail.internet.MimeMultipart)9 Region (software.amazon.awssdk.regions.Region)9 RawMessage (software.amazon.awssdk.services.ses.model.RawMessage)9 SendRawEmailRequest (software.amazon.awssdk.services.ses.model.SendRawEmailRequest)9 SesException (software.amazon.awssdk.services.ses.model.SesException)9 DataHandler (javax.activation.DataHandler)6 DataSource (javax.activation.DataSource)6