Example usage for com.amazonaws.util Base64 encodeAsString

List of usage examples for com.amazonaws.util Base64 encodeAsString

Introduction

In this page you can find the example usage for com.amazonaws.util Base64 encodeAsString.

Prototype

public static String encodeAsString(byte... bytes) 

Source Link

Document

Returns a base 64 encoded string of the given bytes.

Usage

From source file:alluxio.underfs.s3a.S3ALowLevelOutputStream.java

License:Apache License

/**
 * Initializes multipart upload./*from  w  w  w.  j a va 2  s. com*/
 */
private void initMultiPartUpload() throws IOException {
    // Generate the object metadata by setting server side encryption, md5 checksum,
    // and encoding as octet stream since no assumptions are made about the file type
    ObjectMetadata meta = new ObjectMetadata();
    if (mSseEnabled) {
        meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    }
    if (mHash != null) {
        meta.setContentMD5(Base64.encodeAsString(mHash.digest()));
    }
    meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM);

    AmazonClientException lastException;
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(mBucketName, mKey)
            .withObjectMetadata(meta);
    do {
        try {
            mUploadId = mClient.initiateMultipartUpload(initRequest).getUploadId();
            return;
        } catch (AmazonClientException e) {
            lastException = e;
        }
    } while (mRetryPolicy.attempt());
    // This point is only reached if the operation failed more
    // than the allowed retry count
    throw new IOException("Unable to init multipart upload to " + mKey, lastException);
}

From source file:com.amazon.sqs.javamessaging.message.SQSObjectMessage.java

License:Open Source License

/**
 * Serialize the <code>Serializable</code> object to <code>String</code>.
 *//*from w w w  . j a  v  a 2 s. c o  m*/
protected static String serialize(Serializable serializable) throws JMSException {
    if (serializable == null) {
        return null;
    }
    String serializedString;
    ObjectOutputStream objectOutputStream = null;
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(bytesOut);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        serializedString = Base64.encodeAsString(bytesOut.toByteArray());
    } catch (IOException e) {
        LOG.error("IOException: cannot serialize objectMessage", e);
        throw convertExceptionToMessageFormatException(e);
    } finally {
        if (objectOutputStream != null) {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                LOG.warn(e.getMessage());
            }
        }
    }
    return serializedString;
}

From source file:com.amazon.sqs.javamessaging.SQSMessageProducer.java

License:Open Source License

void sendInternal(Queue queue, Message message) throws JMSException {
    checkClosed();/*  w w  w. j a  v a 2 s .  co m*/
    String sqsMessageBody = null;
    String messageType = null;
    if (message instanceof SQSMessage) {
        message.setJMSDestination(queue);
        if (message instanceof SQSBytesMessage) {
            sqsMessageBody = Base64.encodeAsString(((SQSBytesMessage) message).getBodyAsBytes());
            messageType = SQSMessage.BYTE_MESSAGE_TYPE;
        } else if (message instanceof SQSObjectMessage) {
            sqsMessageBody = ((SQSObjectMessage) message).getMessageBody();
            messageType = SQSMessage.OBJECT_MESSAGE_TYPE;
        } else if (message instanceof SQSTextMessage) {
            sqsMessageBody = ((SQSTextMessage) message).getText();
            messageType = SQSMessage.TEXT_MESSAGE_TYPE;
        }
    } else {
        throw new MessageFormatException(
                "Unrecognized message type. Messages have to be one of: SQSBytesMessage, SQSObjectMessage, or SQSTextMessage");
    }
    if (sqsMessageBody == null || sqsMessageBody.isEmpty()) {
        throw new JMSException("Message body cannot be null or empty");
    }
    Map<String, MessageAttributeValue> messageAttributes = propertyToMessageAttribute((SQSMessage) message);
    addMessageTypeReservedAttribute(messageAttributes, (SQSMessage) message, messageType);
    SendMessageRequest sendMessageRequest = new SendMessageRequest(((SQSQueueDestination) queue).getQueueUrl(),
            sqsMessageBody);
    sendMessageRequest.setMessageAttributes(messageAttributes);

    String messageId = amazonSQSClient.sendMessage(sendMessageRequest).getMessageId();
    LOG.info("Message sent to SQS with SQS-assigned messageId: " + messageId);
    /** TODO: Do not support disableMessageID for now.*/
    message.setJMSMessageID(String.format(SQSMessagingClientConstants.MESSAGE_ID_FORMAT, messageId));
    ((SQSMessage) message).setSQSMessageId(messageId);
}

From source file:com.appdynamics.connectors.AWSConnector.java

License:Apache License

public IMachine createMachine(IComputeCenter computeCenter, IImage image, IMachineDescriptor machineDescriptor)
        throws InvalidObjectException, ConnectorException {
    boolean succeeded = false;
    Exception createFailureRootCause = null;
    Instance instance = null;//from   www. j  ava  2  s  . c  om

    try {
        IProperty[] macProps = machineDescriptor.getProperties();

        AmazonEC2 connector = getConnector(image, computeCenter, controllerServices);
        String amiName = Utils.getAMIName(image.getProperties(), controllerServices);
        List<String> securityGroups = getSecurityGroup(macProps);
        validateAndConfigureSecurityGroups(securityGroups, connector);

        controllerServices.getStringPropertyByName(macProps, Utils.SECURITY_GROUP)
                .setValue(getSecurityGroupsAsString(securityGroups));

        String keyPair = Utils.getKeyPair(macProps, controllerServices);

        InstanceType instanceType = getInstanceType(macProps);

        String zone = Utils.getZone(macProps, controllerServices);
        String kernel = Utils.getKernel(macProps, controllerServices);
        String ramdisk = Utils.getRamDisk(macProps, controllerServices);

        String controllerHost = System.getProperty(CONTROLLER_SERVICES_HOST_NAME_PROPERTY_KEY,
                InetAddress.getLocalHost().getHostName());

        int controllerPort = Integer.getInteger(CONTROLLER_SERVICES_PORT_PROPERTY_KEY,
                DEFAULT_CONTROLLER_PORT_VALUE);

        IAccount account = computeCenter.getAccount();

        String accountName = account.getName();
        String accountAccessKey = account.getAccessKey();

        AgentResolutionEncoder agentResolutionEncoder = new AgentResolutionEncoder(controllerHost,
                controllerPort, accountName, accountAccessKey);

        String userData = agentResolutionEncoder.encodeAgentResolutionInfo();

        String instanceName = Utils.getInstanceName(macProps, controllerServices);

        logger.info("Starting EC2 machine of Image :" + amiName + " Name :" + instanceName + " security :"
                + securityGroups + " keypair :" + keyPair + " instance :" + instanceType + " zone :" + zone
                + " kernel :" + kernel + " ramdisk :" + ramdisk + " userData :" + userData);

        RunInstancesRequest runInstancesRequest = new RunInstancesRequest(amiName, 1, 1);
        runInstancesRequest.setSecurityGroups(securityGroups);
        runInstancesRequest.setUserData(Base64.encodeAsString(userData.getBytes()));
        runInstancesRequest.setKeyName(keyPair);
        runInstancesRequest.setInstanceType(instanceType);
        runInstancesRequest.setKernelId(kernel);
        runInstancesRequest.setRamdiskId(ramdisk);

        Reservation reservation = connector.runInstances(runInstancesRequest).getReservation();
        List<Instance> instances = reservation.getInstances();

        if (instances.size() == 0)
            throw new ConnectorException("Cannot create instance for image :" + image.getName());

        instance = instances.get(0);

        //Set name for the instance
        if (!Strings.isNullOrEmpty(instanceName)) {
            CreateTagsRequest createTagsRequest = new CreateTagsRequest();
            createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag("Name", instanceName));
            connector.createTags(createTagsRequest);
        }

        logger.info("EC2 machine started; id:" + instance.getInstanceId());

        IMachine machine;

        if (Strings.isNullOrEmpty(instance.getPublicDnsName())) {
            machine = controllerServices.createMachineInstance(instance.getInstanceId(),
                    agentResolutionEncoder.getUniqueHostIdentifier(), computeCenter, machineDescriptor, image,
                    getAgentPort());
        } else {
            machine = controllerServices.createMachineInstance(instance.getInstanceId(),
                    agentResolutionEncoder.getUniqueHostIdentifier(), instance.getPublicDnsName(),
                    computeCenter, machineDescriptor, image, getAgentPort());
        }

        if (kernel == null) {
            controllerServices.getStringPropertyByName(macProps, Utils.KERNEL).setValue(instance.getKernelId());
        }

        if (zone == null) {
            DescribeAvailabilityZonesResult describeAvailabilityZonesResult = connector
                    .describeAvailabilityZones();
            List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones();
            controllerServices.getStringPropertyByName(macProps, Utils.ZONE)
                    .setValue(availabilityZones.get(0).getZoneName());
        }

        controllerServices.getStringPropertyByName(macProps, Utils.INSTANCE_TYPE)
                .setValue(instance.getInstanceType());

        succeeded = true;

        return machine;

    } catch (InvalidObjectException e) {
        createFailureRootCause = e;
        throw e;
    } catch (ConnectorException e) {
        createFailureRootCause = e;
        throw e;
    } catch (Exception e) {
        createFailureRootCause = e;
        throw new ConnectorException(e.getMessage(), e);
    } finally {
        // We have to make sure to terminate any orphan EC2 instances if 
        // the machine create fails.
        if (!succeeded && instance != null) {
            try {
                ConnectorLocator.getInstance().getConnector(computeCenter, controllerServices)
                        .terminateInstances(
                                new TerminateInstancesRequest(Lists.newArrayList(instance.getInstanceId())));
            } catch (Exception e) {
                throw new ConnectorException("Machine create failed, but terminate failed as well! "
                        + "We have an orphan EC2 instance with id: " + instance.getInstanceId()
                        + " that must be shut down manually. Root cause for machine "
                        + "create failure is following: ", createFailureRootCause);
            }
        }
    }
}

From source file:com.shareplaylearn.models.UserItemManager.java

License:Open Source License

public Response getItem(String contentType, ItemSchema.PresentationType presentationType, String name,
        String encoding) {// w w w .j av  a  2 s . c  o m
    if (encoding != null && encoding.length() > 0 && !AvailableEncodings.isAvailable(encoding)) {
        return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
                .entity("Inner Encoding Type: " + encoding + " not available").build();
    }

    AmazonS3Client s3Client = new AmazonS3Client(
            new BasicAWSCredentials(SecretsService.amazonClientId, SecretsService.amazonClientSecret));

    try {
        S3Object object = s3Client.getObject(ItemSchema.S3_BUCKET,
                getItemLocation(name, contentType, presentationType));
        try (S3ObjectInputStream inputStream = object.getObjectContent()) {
            long contentLength = object.getObjectMetadata().getContentLength();
            if (contentLength > Limits.MAX_RETRIEVE_SIZE) {
                throw new IOException("Object is to large: " + contentLength + " bytes.");
            }
            int bufferSize = Math.min((int) contentLength, 10 * 8192);
            byte[] buffer = new byte[bufferSize];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int bytesRead = 0;
            int totalBytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            }
            log.debug("GET in file resource read: " + totalBytesRead + " bytes.");
            if (encoding == null || encoding.length() == 0 || encoding.equals(AvailableEncodings.IDENTITY)) {
                return Response.status(Response.Status.OK).entity(outputStream.toByteArray()).build();
            } else if (encoding.equals(AvailableEncodings.BASE64)) {
                return Response.status(Response.Status.OK)
                        .entity(Base64.encodeAsString(outputStream.toByteArray())).build();
            } else {
                return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
                        .entity("Inner Encoding Type: " + encoding + " not available").build();
            }
        }
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        pw.println("\nFailed to retrieve: " + name);
        e.printStackTrace(pw);
        log.warn("Failed to retrieve: " + name);
        log.info(Exceptions.asString(e));
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(sw.toString()).build();
    }
}

From source file:com.upplication.s3fs.S3OutputStream.java

License:Open Source License

private void uploadPart(final InputStream content, final long contentLength, final byte[] checksum,
        final int partNumber, final boolean lastPart) throws IOException {

    if (aborted)/*from www .ja  v  a 2s.co  m*/
        return;

    final UploadPartRequest request = new UploadPartRequest();
    request.setBucketName(objectId.getBucket());
    request.setKey(objectId.getKey());
    request.setUploadId(uploadId);
    request.setPartNumber(partNumber);
    request.setPartSize(contentLength);
    request.setInputStream(content);
    request.setLastPart(lastPart);
    request.setMd5Digest(Base64.encodeAsString(checksum));

    final PartETag partETag = s3.uploadPart(request).getPartETag();
    log.trace("Uploaded part {} with length {} for {}: {}", partETag.getPartNumber(), contentLength, objectId,
            partETag.getETag());
    partETags.add(partETag);

}

From source file:com.upplication.s3fs.S3OutputStream.java

License:Open Source License

/**
 * Stores the given buffer using a single-part upload process
 *
 * @param contentLength//  w  w  w.  ja v a 2  s .co  m
 * @param content
 * @throws IOException
 */
private void putObject(final InputStream content, final long contentLength, byte[] checksum)
        throws IOException {

    final ObjectMetadata meta = metadata.clone();
    meta.setContentLength(contentLength);
    meta.setContentMD5(Base64.encodeAsString(checksum));

    final PutObjectRequest request = new PutObjectRequest(objectId.getBucket(), objectId.getKey(), content,
            meta);

    if (storageClass != null) {
        request.setStorageClass(storageClass);
    }

    try {
        s3.putObject(request);
    } catch (final AmazonClientException e) {
        throw new IOException("Failed to put data into Amazon S3 object", e);
    }
}

From source file:org.apache.beam.sdk.io.aws.s3.S3TestUtils.java

License:Apache License

@Nullable
static String getSSECustomerKeyMd5(S3Options options) {
    SSECustomerKey sseCostumerKey = options.getSSECustomerKey();
    if (sseCostumerKey != null) {
        return Base64.encodeAsString(DigestUtils.md5(Base64.decode(sseCostumerKey.getKey())));
    }//from  w ww.j  a v a2  s .  c o m
    return null;
}

From source file:org.apache.beam.sdk.io.aws.s3.S3WritableByteChannel.java

License:Apache License

private void flush() throws IOException {
    uploadBuffer.flip();// w w w.  ja va2s .  c  om
    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer.array());

    UploadPartRequest request = new UploadPartRequest().withBucketName(path.getBucket()).withKey(path.getKey())
            .withUploadId(uploadId).withPartNumber(partNumber++).withPartSize(uploadBuffer.remaining())
            .withMD5Digest(Base64.encodeAsString(md5.digest())).withInputStream(inputStream);
    request.setSSECustomerKey(options.getSSECustomerKey());

    UploadPartResult result;
    try {
        result = amazonS3.uploadPart(request);
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
    uploadBuffer.clear();
    md5.reset();
    eTags.add(result.getPartETag());
}

From source file:org.elasticsearch.cloud.aws.blobstore.MockAmazonS3.java

License:Apache License

@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest)
        throws AmazonClientException, AmazonServiceException {
    String blobName = putObjectRequest.getKey();
    DigestInputStream stream = (DigestInputStream) putObjectRequest.getInputStream();

    if (blobs.containsKey(blobName)) {
        throw new AmazonS3Exception("[" + blobName + "] already exists.");
    }//w ww  . j  av a  2s  . co m

    blobs.put(blobName, stream);

    // input and output md5 hashes need to match to avoid an exception
    String md5 = Base64.encodeAsString(stream.getMessageDigest().digest());
    PutObjectResult result = new PutObjectResult();
    result.setContentMd5(md5);

    return result;
}