Example usage for com.amazonaws.services.s3.model CopyObjectRequest setCannedAccessControlList

List of usage examples for com.amazonaws.services.s3.model CopyObjectRequest setCannedAccessControlList

Introduction

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

Prototype

public void setCannedAccessControlList(CannedAccessControlList cannedACL) 

Source Link

Document

Sets the canned ACL to use for the newly copied object.

Usage

From source file:com.tango.BucketSyncer.KeyJobs.S32S3KeyCopyJob.java

License:Apache License

boolean keyCopied(ObjectMetadata sourceMetadata, AccessControlList objectAcl) {
    boolean copied = false;
    String key = summary.getKey();
    MirrorOptions options = context.getOptions();
    boolean verbose = options.isVerbose();
    int maxRetries = options.getMaxRetries();
    MirrorStats stats = context.getStats();
    for (int tries = 0; tries < maxRetries; tries++) {
        if (verbose) {
            log.info("copying (try # {}): {} to: {}", new Object[] { tries, key, keydest });
        }//from  w w w .  j av  a2  s . c  om
        final CopyObjectRequest request = new CopyObjectRequest(options.getSourceBucket(), key,
                options.getDestinationBucket(), keydest);
        request.setNewObjectMetadata(sourceMetadata);
        if (options.isCrossAccountCopy()) {
            request.setCannedAccessControlList(CannedAccessControlList.BucketOwnerFullControl);
        } else {
            request.setAccessControlList(objectAcl);
        }
        try {
            stats.copyCount.incrementAndGet();
            client.copyObject(request);
            stats.bytesCopied.addAndGet(sourceMetadata.getContentLength());
            if (verbose) {
                log.info("successfully copied (on try #{}): {} to: {}", new Object[] { tries, key, keydest });
            }
            copied = true;
            break;
        } catch (AmazonS3Exception s3e) {
            //if return with 404 error, problem with bucket name
            if (s3e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                log.error("Failed to access S3 bucket. Check bucket name: ", s3e);
                System.exit(1);
            }
            log.error("s3 exception copying (try #{}) {} to: {}: {}",
                    new Object[] { tries, key, keydest, s3e });
        } catch (Exception e) {
            log.error("unexpected exception copying (try #{}) {} to: {}: {}",
                    new Object[] { tries, key, keydest, e });
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            log.error("interrupted while waiting to retry key: {}: {}", key, e);
            return copied;
        }
    }
    return copied;
}

From source file:org.alanwilliamson.amazon.s3.Copy.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey(_session, argStruct);
    AmazonS3 s3Client = getAmazonS3(amazonKey);

    String srcbucket = getNamedStringParam(argStruct, "srcbucket", null);
    String srckey = getNamedStringParam(argStruct, "srckey", null);
    String srcaes256key = getNamedStringParam(argStruct, "srcaes256key", null);

    String destbucket = getNamedStringParam(argStruct, "destbucket", null);
    String deskey = getNamedStringParam(argStruct, "destkey", null);
    String destaes256key = getNamedStringParam(argStruct, "destaes256key", null);
    String deststorageclass = getNamedStringParam(argStruct, "deststorageclass", null);
    String destacl = getNamedStringParam(argStruct, "destacl", null);

    if (srckey != null && srckey.charAt(0) == '/')
        srckey = srckey.substring(1);/*from w  ww.ja v  a 2  s .  com*/

    if (deskey != null && deskey.charAt(0) == '/')
        deskey = deskey.substring(1);

    CopyObjectRequest cor = new CopyObjectRequest(srcbucket, srckey, destbucket, deskey);

    if (srcaes256key != null && !srcaes256key.isEmpty())
        cor.setSourceSSECustomerKey(new SSECustomerKey(srcaes256key));

    if (destaes256key != null && !destaes256key.isEmpty())
        cor.setDestinationSSECustomerKey(new SSECustomerKey(destaes256key));

    if (deststorageclass != null && !deststorageclass.isEmpty())
        cor.setStorageClass(amazonKey.getAmazonStorageClass(deststorageclass));

    if (destacl != null && !destacl.isEmpty())
        cor.setCannedAccessControlList(amazonKey.getAmazonCannedAcl(destacl));

    try {
        s3Client.copyObject(cor);
        return cfBooleanData.TRUE;
    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
        return cfBooleanData.FALSE;
    }
}

From source file:org.apache.hadoop.fs.s3a.S3AFileSystem.java

License:Apache License

private void copyFile(String srcKey, String dstKey) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile " + srcKey + " -> " + dstKey);
    }/*from   w  ww  .  j av a2s.  c  o  m*/

    TransferManagerConfiguration transferConfiguration = new TransferManagerConfiguration();
    transferConfiguration.setMultipartCopyPartSize(partSize);

    TransferManager transfers = new TransferManager(s3);
    transfers.setConfiguration(transferConfiguration);

    ObjectMetadata srcom = s3.getObjectMetadata(bucket, srcKey);
    final ObjectMetadata dstom = srcom.clone();
    if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) {
        dstom.setServerSideEncryption(serverSideEncryptionAlgorithm);
    }

    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, srcKey, bucket, dstKey);
    copyObjectRequest.setCannedAccessControlList(cannedACL);
    copyObjectRequest.setNewObjectMetadata(dstom);

    ProgressListener progressListener = new ProgressListener() {
        public void progressChanged(ProgressEvent progressEvent) {
            switch (progressEvent.getEventCode()) {
            case ProgressEvent.PART_COMPLETED_EVENT_CODE:
                statistics.incrementWriteOps(1);
                break;
            }
        }
    };

    Copy copy = transfers.copy(copyObjectRequest);
    copy.addProgressListener(progressListener);
    try {
        copy.waitForCopyResult();
        statistics.incrementWriteOps(1);
    } catch (InterruptedException e) {
        throw new IOException("Got interrupted, cancelling");
    } finally {
        transfers.shutdownNow(false);
    }
}

From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java

License:Apache License

private void copyFile(String srcKey, String dstKey) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile " + srcKey + " -> " + dstKey);
    }/*from   w  w w .  j  a  va 2  s  .  c  o  m*/

    ObjectMetadata srcom = s3.getObjectMetadata(bucket, srcKey);
    final ObjectMetadata dstom = srcom.clone();
    if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) {
        dstom.setServerSideEncryption(serverSideEncryptionAlgorithm);
    }
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, srcKey, bucket, dstKey);
    copyObjectRequest.setCannedAccessControlList(cannedACL);
    copyObjectRequest.setNewObjectMetadata(dstom);

    ProgressListener progressListener = new ProgressListener() {
        public void progressChanged(ProgressEvent progressEvent) {
            switch (progressEvent.getEventCode()) {
            case ProgressEvent.PART_COMPLETED_EVENT_CODE:
                statistics.incrementWriteOps(1);
                break;
            default:
                break;
            }
        }
    };

    Copy copy = transfers.copy(copyObjectRequest);
    copy.addProgressListener(progressListener);
    try {
        copy.waitForCopyResult();
        statistics.incrementWriteOps(1);
    } catch (InterruptedException e) {
        throw new IOException("Got interrupted, cancelling");
    }
}

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

License:Apache License

private void copyOrMoveObjectImpl(String srcKey, String destKey, boolean move,
        boolean checkAndSucceedIfAlreadyMoved) {
    final S3Configuration s3config = s3ConfigurationService.lookupS3Configuration();
    final AmazonS3Client s3Client = getAmazonS3Client(s3config);
    final String bucketName = s3config.getDefaultBucketName();
    // copy//w  ww .j a  va 2s  . c  o  m
    final CopyObjectRequest objToCopy = new CopyObjectRequest(bucketName, srcKey, bucketName, destKey);

    if ((s3config.getStaticAssetFileExtensionPattern() != null)
            && s3config.getStaticAssetFileExtensionPattern().matcher(getExtension(destKey)).matches()) {
        objToCopy.setCannedAccessControlList(CannedAccessControlList.PublicRead);
    }
    try {
        s3Client.copyObject(objToCopy);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 404 && checkAndSucceedIfAlreadyMoved) {
            // it's not in the srcKey. Check if something is at the destKey
            if (s3Client.doesObjectExist(bucketName, destKey)) {
                final String msg = String.format("src(%s) doesn't exist but dest(%s) does, so assuming success",
                        srcKey, destKey);
                LOG.warn(msg);
                return;
            } else {
                final String msg = String.format("neither src(%s) or dest(%s) exist", srcKey, destKey);
                throw new RuntimeException(msg);
            }
        }
    } catch (AmazonClientException e) {
        throw new RuntimeException("Unable to copy object from: " + srcKey + " to: " + destKey, e);
    }

    if (move) {
        // delete the old ones in sandbox folder (those with srcKey)
        DeleteObjectRequest objToDelete = new DeleteObjectRequest(bucketName, srcKey);
        try {
            s3Client.deleteObject(objToDelete);
        } catch (AmazonClientException e) {
            //throw new RuntimeException("Moving objects to production folder but unable to delete old object: " + srcKey, e);
            LOG.error("Moving objects to production folder but unable to delete old object: " + srcKey, e);
        }
    }
}

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

License:Apache License

@Override
public String copyContent(String sourceSpaceId, String sourceContentId, String destSpaceId,
        String destContentId) {/*from   w  w w  . j  a va2  s .  c  o m*/
    log.debug("copyContent({}, {}, {}, {})", sourceSpaceId, sourceContentId, destSpaceId, destContentId);

    // Will throw if source bucket does not exist
    String sourceBucketName = getBucketName(sourceSpaceId);
    // Will throw if destination bucket does not exist
    String destBucketName = getBucketName(destSpaceId);

    throwIfContentNotExist(sourceBucketName, sourceContentId);

    CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceContentId, destBucketName,
            destContentId);
    request.setStorageClass(DEFAULT_STORAGE_CLASS);
    request.setCannedAccessControlList(CannedAccessControlList.Private);

    CopyObjectResult result = doCopyObject(request);
    return StorageProviderUtil.compareChecksum(this, sourceSpaceId, sourceContentId, result.getETag());
}

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

License:Open Source License

public String copyObject(@NotNull S3ObjectId source, @NotNull S3ObjectId destination,
        @NotNull ConditionalConstraints conditionalConstraints, CannedAccessControlList acl,
        StorageClass storageClass, Map<String, String> userMetadata, String encryption) {
    Validate.notNull(source);//from   w  ww .  j  a v a2 s.  c  o  m
    Validate.notNull(destination);
    Validate.notNull(conditionalConstraints);
    CopyObjectRequest request = new CopyObjectRequest(source.getBucketName(), source.getKey(),
            source.getVersionId(), destination.getBucketName(), destination.getKey());
    request.setCannedAccessControlList(acl);
    if (storageClass != null) {
        request.setStorageClass(storageClass);
    }

    if (encryption != null) {
        request.setNewObjectMetadata(new ObjectMetadata());
        request.getNewObjectMetadata().setServerSideEncryption(encryption);
        if (userMetadata != null) {
            request.getNewObjectMetadata().setUserMetadata(userMetadata);
        }
    } else if (userMetadata != null) {
        request.setNewObjectMetadata(new ObjectMetadata());
        request.getNewObjectMetadata().setUserMetadata(userMetadata);
    }

    conditionalConstraints.populate(request);
    return s3.copyObject(request).getVersionId();
}

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 {/*ww w. j  a v  a  2s .  co  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");
    }
}