Example usage for com.amazonaws.services.s3.model ObjectMetadata setHeader

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setHeader

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model ObjectMetadata setHeader.

Prototype

public void setHeader(String key, Object value) 

Source Link

Document

For internal use only.

Usage

From source file:gov.cdc.sdp.cbr.aphl.AphlS3Producer.java

License:Apache License

private ObjectMetadata determineMetadata(final Exchange exchange) {
    ObjectMetadata objectMetadata = new ObjectMetadata();

    Long contentLength = exchange.getIn().getHeader(S3Constants.CONTENT_LENGTH, Long.class);
    if (contentLength != null) {
        objectMetadata.setContentLength(contentLength);
    }//from w  w  w. j  a  v  a2s.  co m

    String contentType = exchange.getIn().getHeader(S3Constants.CONTENT_TYPE, String.class);
    if (contentType != null) {
        objectMetadata.setContentType(contentType);
    }

    String cacheControl = exchange.getIn().getHeader(S3Constants.CACHE_CONTROL, String.class);
    if (cacheControl != null) {
        objectMetadata.setCacheControl(cacheControl);
    }

    String contentDisposition = exchange.getIn().getHeader(S3Constants.CONTENT_DISPOSITION, String.class);
    if (contentDisposition != null) {
        objectMetadata.setContentDisposition(contentDisposition);
    }

    String contentEncoding = exchange.getIn().getHeader(S3Constants.CONTENT_ENCODING, String.class);
    if (contentEncoding != null) {
        objectMetadata.setContentEncoding(contentEncoding);
    }

    String contentMD5 = exchange.getIn().getHeader(S3Constants.CONTENT_MD5, String.class);
    if (contentMD5 != null) {
        objectMetadata.setContentMD5(contentMD5);
    }

    Date lastModified = exchange.getIn().getHeader(S3Constants.LAST_MODIFIED, Date.class);
    if (lastModified != null) {
        objectMetadata.setLastModified(lastModified);
    }

    Map<String, String> userMetadata = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.USER_METADATA, Map.class));
    if (userMetadata != null) {
        objectMetadata.setUserMetadata(userMetadata);
    }

    Map<String, String> s3Headers = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.S3_HEADERS, Map.class));
    if (s3Headers != null) {
        for (Map.Entry<String, String> entry : s3Headers.entrySet()) {
            objectMetadata.setHeader(entry.getKey(), entry.getValue());
        }
    }

    String encryption = exchange.getIn().getHeader(S3Constants.SERVER_SIDE_ENCRYPTION,
            getConfiguration().getServerSideEncryption(), String.class);
    if (encryption != null) {
        objectMetadata.setSSEAlgorithm(encryption);
    }

    return objectMetadata;
}

From source file:io.stallion.services.S3StorageService.java

License:Open Source License

public void uploadFile(File file, String bucket, String fileKey, boolean isPublic, String contentType,
        Map<String, String> headers) {
    client.putObject(bucket, fileKey, file);
    PutObjectRequest req = new PutObjectRequest(bucket, fileKey, file);
    if (isPublic) {
        req.withCannedAcl(CannedAccessControlList.PublicRead);
    }//w  w w . j av  a2  s.  co  m
    ObjectMetadata meta = new ObjectMetadata();

    if (headers != null) {
        for (String key : headers.keySet()) {
            meta.setHeader(key, headers.get(key));
        }
    }
    if (!empty(contentType)) {
        meta.setContentType(contentType);
    }
    req.setMetadata(meta);
    client.putObject(req);

}

From source file:jenkins.plugins.itemstorage.s3.S3BaseUploadCallable.java

License:Open Source License

protected ObjectMetadata buildMetadata(File file) throws IOException {
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType(Mimetypes.getInstance().getMimetype(file.getName()));
    metadata.setContentLength(file.length());
    metadata.setLastModified(new Date(file.lastModified()));

    if (storageClass != null && !storageClass.isEmpty()) {
        metadata.setHeader("x-amz-storage-class", storageClass);
    }/*from   w  w  w  .j a  v  a2s. com*/
    if (useServerSideEncryption) {
        metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    }

    for (Map.Entry<String, String> entry : userMetadata.entrySet()) {
        final String key = entry.getKey().toLowerCase();
        switch (key) {
        case "cache-control":
            metadata.setCacheControl(entry.getValue());
            break;
        case "expires":
            try {
                final Date expires = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z")
                        .parse(entry.getValue());
                metadata.setHttpExpiresDate(expires);
            } catch (ParseException e) {
                metadata.addUserMetadata(entry.getKey(), entry.getValue());
            }
            break;
        case "content-encoding":
            metadata.setContentEncoding(entry.getValue());
            break;
        case "content-type":
            metadata.setContentType(entry.getValue());
        default:
            metadata.addUserMetadata(entry.getKey(), entry.getValue());
            break;
        }
    }
    return metadata;
}

From source file:org.apache.usergrid.apm.util.CrashLogUploadToS3Simulator.java

License:Apache License

public static boolean uploadSimulatedCrashLogsToS3(String appId, String fileName) {
    DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
    AWSCredentials credentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    AmazonS3Client s3Client = new AmazonS3Client(credentials);
    PutObjectRequest putObjectRequest;/*  w  w w.j av  a  2 s .  c o m*/

    String s3FullFileName = config.getEnvironment() + "/crashlog/" + appId + "/" + fileName;

    try {

        ObjectMetadata metaData = new ObjectMetadata();

        metaData.setHeader(Headers.S3_CANNED_ACL, CannedAccessControlList.AuthenticatedRead);

        putObjectRequest = new PutObjectRequest(config.getS3LogBucket(), s3FullFileName,
                new ByteArrayInputStream(fileName.getBytes("UTF-8")), null);
        PutObjectResult result = s3Client.putObject(putObjectRequest);
        return true;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.apache.usergrid.apm.util.GenerateSimulatedMobileData.java

License:Apache License

public static boolean uploadSimulatedCrashLogsToS3(App app, String fileName) {
    DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
    AWSCredentials credentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    AmazonS3Client s3Client = new AmazonS3Client(credentials);
    PutObjectRequest putObjectRequest;/*from   ww w  . j  av a2  s .  c o m*/

    String s3FullFileName = config.getEnvironment() + "/" + config.getS3CrashLogFolder() + "/"
            + app.getFullAppName() + "/" + fileName;
    String sampleCrashFileName = null;

    if (fileName.endsWith(".crash")) //it's an iOS crash file
        sampleCrashFileName = "ios-crash-log-example.txt";
    else if (fileName.endsWith(".stacktrace"))
        sampleCrashFileName = "android-crash-log-example.txt";
    try {

        ObjectMetadata metaData = new ObjectMetadata();

        metaData.setHeader(Headers.S3_CANNED_ACL, CannedAccessControlList.AuthenticatedRead);

        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(sampleCrashFileName);

        putObjectRequest = new PutObjectRequest(config.getS3LogBucket(), s3FullFileName, is, null);
        //new ByteArrayInputStream(    //fileName.getBytes("UTF-8")),null);
        PutObjectResult result = s3Client.putObject(putObjectRequest);
        return true;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.eclipse.hawkbit.artifact.repository.S3Repository.java

License:Open Source License

private ObjectMetadata createObjectMetadata(final String mdMD5Hash16, final String contentType,
        final File file) {
    final ObjectMetadata objectMetadata = new ObjectMetadata();
    final String mdMD5Hash64 = BaseEncoding.base64()
            .encode(BaseEncoding.base16().lowerCase().decode(mdMD5Hash16));
    objectMetadata.setContentMD5(mdMD5Hash64);
    objectMetadata.setContentType(contentType);
    objectMetadata.setContentLength(file.length());
    objectMetadata.setHeader("x-amz-meta-md5chksum", mdMD5Hash64);
    if (s3Properties.isServerSideEncryption()) {
        objectMetadata.setHeader(Headers.SERVER_SIDE_ENCRYPTION,
                s3Properties.getServerSideEncryptionAlgorithm());
    }/*from   w  w w. j ava 2  s  .co  m*/
    return objectMetadata;
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * Prepares the object metadata for server side encryption and reduced redundancy storage.
 *
 * @param params the parameters./*from  w  w w .  j a v  a2s .  c o m*/
 * @param metadata the metadata to prepare.
 */
private void prepareMetadata(final S3FileTransferRequestParamsDto params, ObjectMetadata metadata) {
    // Set the server side encryption
    metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);

    // If specified, set the metadata to use RRS.
    if (Boolean.TRUE.equals(params.getUseRrs())) {
        // TODO: For upload File, we can set RRS on the putObjectRequest.  For uploadDirectory, this is the only
        // way to do it.  However, setHeader() is flagged as For Internal Use Only
        metadata.setHeader(Headers.STORAGE_CLASS, StorageClass.ReducedRedundancy.toString());
    }
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * Prepares the object metadata for server side encryption and reduced redundancy storage.
 *
 * @param params the parameters./*from www. ja  va2 s  . c o m*/
 * @param metadata the metadata to prepare.
 */
private void prepareMetadata(final S3FileTransferRequestParamsDto params, ObjectMetadata metadata) {
    // Set the server side encryption
    if (params.getKmsKeyId() != null) {
        /*
         * TODO Use proper way to set KMS once AWS provides a way.
         * We are modifying the raw headers directly since TransferManager's uploadFileList operation does not provide a way to set a KMS key ID.
         * This would normally cause some issues when uploading where an MD5 checksum validation exception will be thrown, even though the object is
         * correctly uploaded.
         * To get around this, a system property defined at
         * com.amazonaws.services.s3.internal.SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY must be set.
         */
        metadata.setSSEAlgorithm(SSEAlgorithm.KMS.getAlgorithm());
        metadata.setHeader(Headers.SERVER_SIDE_ENCRYPTION_AWS_KMS_KEYID, params.getKmsKeyId().trim());
    } else {
        metadata.setSSEAlgorithm(SSEAlgorithm.AES256.getAlgorithm());
    }

    // If specified, set the metadata to use RRS.
    if (Boolean.TRUE.equals(params.isUseRrs())) {
        // TODO: For upload File, we can set RRS on the putObjectRequest. For uploadDirectory, this is the only
        // way to do it. However, setHeader() is flagged as For Internal Use Only
        metadata.setHeader(Headers.STORAGE_CLASS, StorageClass.ReducedRedundancy.toString());
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

License:Apache License

public boolean save(InputStream is, long size, String path) {
    //        Assert.notNull(is, " ??   'is'?  .");
    Assert.notNull(is, "Please enter the input stream.");
    //        Assert.hasLength(path, "??  ? ??  'path'?  .");
    Assert.hasLength(path, "Please enter the path.");

    try {/* w w w  .j a  v  a  2s. c  o  m*/
        String bucket = S3Utils.getBucket(path);
        String key = StringUtils.remove(path, "/" + bucket + "/");
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(Headers.CONTENT_LENGTH, size);
        awsClient.putObject(new PutObjectRequest(bucket, key, is, metadata));
        return true;
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error " + "response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());

        //            throw new FileSystemException("??    . ? ? ? .", ase);
        throw new FileSystemException("Connot copy the file.", ase);
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to " + " communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());

        //            throw new FileSystemException("??   . ? ? ?.", ace);
        throw new FileSystemException("Connot copy the file.", ace);
    }
}