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);
}
}
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;
}
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);
}
}
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);
}
}
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();
}
}
Aggregations