Search in sources :

Example 1 with SdkBytes

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

the class PutFile method uploadFile.

// snippet-start:[codecommit.java2.put_file.main]
public static void uploadFile(CodeCommitClient codeCommitClient, String filePath, String repoName, String branchName, String email, String name, String repoPath, String commitId) {
    try {
        // Create an SdkBytes object that represents the file to upload
        File myFile = new File(filePath);
        InputStream is = new FileInputStream(myFile);
        SdkBytes fileToUpload = SdkBytes.fromInputStream(is);
        PutFileRequest fileRequest = PutFileRequest.builder().fileContent(fileToUpload).repositoryName(repoName).commitMessage("Uploaded via the Java API").branchName(branchName).filePath(repoPath).parentCommitId(commitId).email(email).name(name).build();
        // Upload file to the branch
        PutFileResponse fileResponse = codeCommitClient.putFile(fileRequest);
        System.out.println("The commit ID is " + fileResponse.commitId());
    } catch (CodeCommitException | FileNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : PutFileRequest(software.amazon.awssdk.services.codecommit.model.PutFileRequest) CodeCommitException(software.amazon.awssdk.services.codecommit.model.CodeCommitException) SdkBytes(software.amazon.awssdk.core.SdkBytes) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PutFileResponse(software.amazon.awssdk.services.codecommit.model.PutFileResponse) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with SdkBytes

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

the class KMSEncryptionExample method encryptData.

// Encrypt the data passed as a byte array
private static byte[] encryptData(String keyId, byte[] data) {
    try {
        KmsClient kmsClient = getKMSClient();
        SdkBytes myBytes = SdkBytes.fromByteArray(data);
        EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
        EncryptResponse response = kmsClient.encrypt(encryptRequest);
        String algorithm = response.encryptionAlgorithm().toString();
        System.out.println("The encryption algorithm is " + algorithm);
        // Return the encrypted data
        SdkBytes encryptedData = response.ciphertextBlob();
        return encryptedData.asByteArray();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) KmsClient(software.amazon.awssdk.services.kms.KmsClient) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 3 with SdkBytes

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

the class KMSEncryptionExample method decryptData.

// Decrypt the data passed as a byte array
private static byte[] decryptData(byte[] data, String keyId) {
    try {
        KmsClient kmsClient = getKMSClient();
        SdkBytes encryptedData = SdkBytes.fromByteArray(data);
        DecryptRequest decryptRequest = DecryptRequest.builder().ciphertextBlob(encryptedData).keyId(keyId).build();
        DecryptResponse decryptResponse = kmsClient.decrypt(decryptRequest);
        SdkBytes plainText = decryptResponse.plaintext();
        return plainText.asByteArray();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) DecryptResponse(software.amazon.awssdk.services.kms.model.DecryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) KmsClient(software.amazon.awssdk.services.kms.KmsClient) DecryptRequest(software.amazon.awssdk.services.kms.model.DecryptRequest)

Example 4 with SdkBytes

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

the class SendNotifications method sendEmailMessage.

private void sendEmailMessage(SesClient client, String recipient) throws MessagingException, IOException {
    // The email body for non-HTML email clients.
    String bodyText = "Hello,\r\n" + "Please be advised that your student was marked absent from school today. ";
    // The HTML body of the email.
    String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>" + "<p>Please be advised that your student was marked absent from school today.</p>" + "</body>" + "</html>";
    String sender = "scmacdon@amazon.com";
    String subject = "School Attendence";
    Session session = Session.getDefaultInstance(new Properties());
    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 5 with SdkBytes

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

the class SendMessages method send.

public void send(byte[] attachment, String emailAddress) throws MessagingException, IOException {
    MimeMessage message = null;
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object.
    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(emailAddress));
    // 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);
    // Define the attachment.
    MimeBodyPart att = new MimeBodyPart();
    DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    att.setDataHandler(new DataHandler(fds));
    String reportName = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message.
    msg.addBodyPart(att);
    // Send the email.
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        Region region = Region.US_WEST_2;
        SesClient client = SesClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
        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);
    }
    System.out.println("Email sent with attachment");
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SesClient(software.amazon.awssdk.services.ses.SesClient) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Region(software.amazon.awssdk.regions.Region) 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) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

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