Example usage for com.amazonaws.services.s3.model PutObjectRequest setCannedAcl

List of usage examples for com.amazonaws.services.s3.model PutObjectRequest setCannedAcl

Introduction

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

Prototype

public void setCannedAcl(CannedAccessControlList cannedAcl) 

Source Link

Document

Sets the optional pre-configured access control policy to use for the new object.

Usage

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

protected void addOrUpdateResourcesInternalStreamVersion(S3Configuration s3config, AmazonS3Client s3,
        InputStream inputStream, String fileName, long fileSizeInBytes) {
    final String bucketName = s3config.getDefaultBucketName();

    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(fileSizeInBytes);
    final String resourceName = buildResourceName(s3config, fileName);
    final PutObjectRequest objToUpload = new PutObjectRequest(bucketName, resourceName, inputStream, metadata);

    if ((s3config.getStaticAssetFileExtensionPattern() != null)
            && s3config.getStaticAssetFileExtensionPattern().matcher(getExtension(fileName)).matches()) {
        objToUpload.setCannedAcl(CannedAccessControlList.PublicRead);
    }/*  w  w w  . j  a va  2s .  co m*/

    s3.putObject(objToUpload);

    if (LOG.isTraceEnabled()) {
        final String s3Uri = String.format("s3://%s/%s", s3config.getDefaultBucketName(), resourceName);
        final String msg = String.format("%s copied/updated to %s", fileName, s3Uri);

        LOG.trace(msg);
    }
}

From source file:org.duracloud.s3storage.S3StorageProvider.java

License:Apache License

/**
 * Adds content to a hidden space./*  w w  w  .  ja  v a2s  .  com*/
 *
 * @param spaceId         hidden spaceId
 * @param contentId
 * @param contentMimeType
 * @param content
 * @return
 */
public String addHiddenContent(String spaceId, String contentId, String contentMimeType, InputStream content) {
    log.debug("addHiddenContent(" + spaceId + ", " + contentId + ", " + contentMimeType + ")");

    // Will throw if bucket does not exist
    String bucketName = getBucketName(spaceId);

    // Wrap the content in order to be able to retrieve a checksum

    if (contentMimeType == null || contentMimeType.equals("")) {
        contentMimeType = DEFAULT_MIMETYPE;
    }

    ObjectMetadata objMetadata = new ObjectMetadata();
    objMetadata.setContentType(contentMimeType);

    PutObjectRequest putRequest = new PutObjectRequest(bucketName, contentId, content, objMetadata);
    putRequest.setStorageClass(DEFAULT_STORAGE_CLASS);
    putRequest.setCannedAcl(CannedAccessControlList.Private);

    try {
        PutObjectResult putResult = s3Client.putObject(putRequest);
        return putResult.getETag();
    } catch (AmazonClientException e) {
        String err = "Could not add content " + contentId + " with type " + contentMimeType + " to S3 bucket "
                + bucketName + " due to error: " + e.getMessage();
        throw new StorageException(err, e, NO_RETRY);
    }

}

From source file:org.duracloud.s3storage.S3StorageProvider.java

License:Apache License

/**
 * {@inheritDoc}/*from   w  ww.  ja  v  a  2  s . co m*/
 */
public String addContent(String spaceId, String contentId, String contentMimeType,
        Map<String, String> userProperties, long contentSize, String contentChecksum, InputStream content) {
    log.debug("addContent(" + spaceId + ", " + contentId + ", " + contentMimeType + ", " + contentSize + ", "
            + contentChecksum + ")");

    // Will throw if bucket does not exist
    String bucketName = getBucketName(spaceId);

    // Wrap the content in order to be able to retrieve a checksum
    ChecksumInputStream wrappedContent = new ChecksumInputStream(content, contentChecksum);

    String contentEncoding = removeContentEncoding(userProperties);

    userProperties = removeCalculatedProperties(userProperties);

    if (contentMimeType == null || contentMimeType.equals("")) {
        contentMimeType = DEFAULT_MIMETYPE;
    }

    ObjectMetadata objMetadata = new ObjectMetadata();
    objMetadata.setContentType(contentMimeType);
    if (contentSize > 0) {
        objMetadata.setContentLength(contentSize);
    }
    if (null != contentChecksum && !contentChecksum.isEmpty()) {
        String encodedChecksum = ChecksumUtil.convertToBase64Encoding(contentChecksum);
        objMetadata.setContentMD5(encodedChecksum);
    }

    if (contentEncoding != null) {
        objMetadata.setContentEncoding(contentEncoding);
    }

    if (userProperties != null) {
        for (String key : userProperties.keySet()) {
            String value = userProperties.get(key);

            if (log.isDebugEnabled()) {
                log.debug("[" + key + "|" + value + "]");
            }

            objMetadata.addUserMetadata(getSpaceFree(encodeHeaderKey(key)), encodeHeaderValue(value));
        }
    }

    PutObjectRequest putRequest = new PutObjectRequest(bucketName, contentId, wrappedContent, objMetadata);
    putRequest.setStorageClass(DEFAULT_STORAGE_CLASS);
    putRequest.setCannedAcl(CannedAccessControlList.Private);

    // Add the object
    String etag;
    try {
        PutObjectResult putResult = s3Client.putObject(putRequest);
        etag = putResult.getETag();
    } catch (AmazonClientException e) {
        if (e instanceof AmazonS3Exception) {
            AmazonS3Exception s3Ex = (AmazonS3Exception) e;
            String errorCode = s3Ex.getErrorCode();
            Integer statusCode = s3Ex.getStatusCode();
            String message = MessageFormat.format(
                    "exception putting object {0} into {1}: errorCode={2},"
                            + "  statusCode={3}, errorMessage={4}",
                    contentId, bucketName, errorCode, statusCode, e.getMessage());

            if (errorCode.equals("InvalidDigest") || errorCode.equals("BadDigest")) {
                log.error(message, e);

                String err = "Checksum mismatch detected attempting to add " + "content " + contentId
                        + " to S3 bucket " + bucketName + ". Content was not added.";
                throw new ChecksumMismatchException(err, e, NO_RETRY);
            } else if (errorCode.equals("IncompleteBody")) {
                log.error(message, e);
                throw new StorageException("The content body was incomplete for " + contentId + " to S3 bucket "
                        + bucketName + ". Content was not added.", e, NO_RETRY);
            } else if (!statusCode.equals(HttpStatus.SC_SERVICE_UNAVAILABLE)
                    && !statusCode.equals(HttpStatus.SC_NOT_FOUND)) {
                log.error(message, e);
            } else {
                log.warn(message, e);
            }
        } else {
            String err = MessageFormat.format("exception putting object {0} into {1}: {2}", contentId,
                    bucketName, e.getMessage());
            log.error(err, e);
        }

        // Check to see if file landed successfully in S3, despite the exception
        etag = doesContentExistWithExpectedChecksum(bucketName, contentId, contentChecksum);
        if (null == etag) {
            String err = "Could not add content " + contentId + " with type " + contentMimeType + " and size "
                    + contentSize + " to S3 bucket " + bucketName + " due to error: " + e.getMessage();
            throw new StorageException(err, e, NO_RETRY);
        }
    }

    // Compare checksum
    String providerChecksum = getETagValue(etag);
    String checksum = wrappedContent.getMD5();
    StorageProviderUtil.compareChecksum(providerChecksum, spaceId, contentId, checksum);
    return providerChecksum;
}

From source file:org.kuali.maven.wagon.S3Wagon.java

License:Educational Community License

/**
 * Create a PutObjectRequest based on the source file and destination passed in
 *///from   ww w .jav a  2 s. c  o m
protected PutObjectRequest getPutObjectRequest(File source, String destination, TransferProgress progress) {
    try {
        String key = getNormalizedKey(source, destination);
        String bucketName = bucket.getName();
        InputStream input = getInputStream(source, progress);
        ObjectMetadata metadata = getObjectMetadata(source, destination);
        PutObjectRequest request = new PutObjectRequest(bucketName, key, input, metadata);
        request.setCannedAcl(acl);
        return request;
    } catch (FileNotFoundException e) {
        throw new AmazonServiceException("File not found", e);
    }
}

From source file:org.mule.module.s3.simpleapi.SimpleAmazonS3AmazonDevKitImpl.java

License:Open Source License

@Override
public String createObject(@NotNull S3ObjectId objectId, @NotNull S3ObjectContent content, String contentType,
        String contentDisposition, CannedAccessControlList acl, StorageClass storageClass,
        Map<String, String> userMetadata, String encryption) {
    Validate.notNull(content);//from  ww w .ja v a 2 s.  c  om
    PutObjectRequest request = content.createPutObjectRequest();

    if (request.getMetadata() != null) {
        request.getMetadata().setContentType(contentType);
        if (StringUtils.isNotBlank(contentDisposition)) {
            request.getMetadata().setContentDisposition(contentDisposition);
        }
        if (encryption != null) {
            request.getMetadata().setServerSideEncryption(encryption);
        }
    }
    request.getMetadata().setUserMetadata(userMetadata);
    request.setBucketName(objectId.getBucketName());
    request.setKey(objectId.getKey());
    request.setCannedAcl(acl);
    if (storageClass != null) {
        request.setStorageClass(storageClass);
    }
    return s3.putObject(request).getVersionId();
}

From source file:squash.booking.lambdas.core.PageManager.java

License:Apache License

private void copyJsonDataToS3(String keyName, String jsonToCopy) throws Exception {

    logger.log("About to copy cached json data to S3");

    try {/*from   www  . j  a v  a2  s  . c  om*/
        logger.log("Uploading json data to S3 bucket: " + websiteBucketName + " and key: " + keyName + ".json");
        byte[] jsonAsBytes = jsonToCopy.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream jsonAsStream = new ByteArrayInputStream(jsonAsBytes);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(jsonAsBytes.length);
        metadata.setContentType("application/json");
        // Direct caches not to satisfy future requests with this data without
        // revalidation.
        if (keyName.contains("famousplayers")) {
            // Famousplayers list is good for a year
            metadata.setCacheControl("max-age=31536000");
        } else {
            metadata.setCacheControl("no-cache, must-revalidate");
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(websiteBucketName, keyName + ".json",
                jsonAsStream, metadata);
        // Data must be public so it can be served from the website
        putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        IS3TransferManager transferManager = getS3TransferManager();
        TransferUtils.waitForS3Transfer(transferManager.upload(putObjectRequest), logger);
        logger.log("Uploaded cached json data to S3 bucket");
    } catch (AmazonServiceException ase) {
        ExceptionUtils.logAmazonServiceException(ase, logger);
        throw new Exception("Exception caught while copying json data to S3");
    } catch (AmazonClientException ace) {
        ExceptionUtils.logAmazonClientException(ace, logger);
        throw new Exception("Exception caught while copying json data to S3");
    } catch (InterruptedException e) {
        logger.log("Caught interrupted exception: ");
        logger.log("Error Message: " + e.getMessage());
        throw new Exception("Exception caught while copying json data to S3");
    }
}

From source file:squash.booking.lambdas.core.PageManager.java

License:Apache License

private void copyUpdatedBookingPageToS3(String pageBaseName, String page, String uidSuffix, boolean usePrefix)
        throws Exception {

    logger.log("About to copy booking page to S3");

    String pageBaseNameWithPrefix = usePrefix ? "NoScript/" + pageBaseName : pageBaseName;
    try {// w w  w. ja va  2s . c o m
        logger.log("Uploading booking page to S3 bucket: " + websiteBucketName + "s3websitebucketname"
                + " and key: " + pageBaseNameWithPrefix + uidSuffix + ".html");
        byte[] pageAsGzippedBytes = FileUtils.gzip(page.getBytes(StandardCharsets.UTF_8), logger);

        ByteArrayInputStream pageAsStream = new ByteArrayInputStream(pageAsGzippedBytes);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(pageAsGzippedBytes.length);
        metadata.setContentEncoding("gzip");
        metadata.setContentType("text/html");
        // Direct caches not to satisfy future requests with this data without
        // revalidation.
        metadata.setCacheControl("no-cache, must-revalidate");
        PutObjectRequest putObjectRequest = new PutObjectRequest(websiteBucketName,
                pageBaseNameWithPrefix + uidSuffix + ".html", pageAsStream, metadata);
        // Page must be public so it can be served from the website
        putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        IS3TransferManager transferManager = getS3TransferManager();
        TransferUtils.waitForS3Transfer(transferManager.upload(putObjectRequest), logger);
        logger.log("Uploaded booking page to S3 bucket");

        if (uidSuffix.equals("")) {
            // Nothing to copy - so return
            logger.log("UidSuffix is empty - so not creating duplicate page");
            return;
        }

        // N.B. We copy from hashed key to non-hashed (and not vice versa)
        // to ensure consistency
        logger.log("Copying booking page in S3 bucket: " + websiteBucketName + " and key: "
                + pageBaseNameWithPrefix + ".html");
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(websiteBucketName,
                pageBaseNameWithPrefix + uidSuffix + ".html", websiteBucketName,
                pageBaseNameWithPrefix + ".html");
        copyObjectRequest.setCannedAccessControlList(CannedAccessControlList.PublicRead);
        // N.B. Copied object will get same metadata as the source (e.g. the
        // cache-control header etc.)
        TransferUtils.waitForS3Transfer(transferManager.copy(copyObjectRequest), logger);
        logger.log("Copied booking page successfully in S3");
    } catch (AmazonServiceException ase) {
        ExceptionUtils.logAmazonServiceException(ase, logger);
        throw new Exception("Exception caught while copying booking page to S3");
    } catch (AmazonClientException ace) {
        ExceptionUtils.logAmazonClientException(ace, logger);
        throw new Exception("Exception caught while copying booking page to S3");
    } catch (InterruptedException e) {
        logger.log("Caught interrupted exception: ");
        logger.log("Error Message: " + e.getMessage());
        throw new Exception("Exception caught while copying booking page to S3");
    }
}