Example usage for com.amazonaws.services.s3 AmazonS3Client copyObject

List of usage examples for com.amazonaws.services.s3 AmazonS3Client copyObject

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client copyObject.

Prototype

@Override
    public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest)
            throws SdkClientException, AmazonServiceException 

Source Link

Usage

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

@Override
public CopyObjectResponseType copyObject(CopyObjectType request) throws S3Exception {
    CopyObjectResponseType reply = request.getReply();
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;

    String sourceBucket = request.getSourceBucket();
    String sourceKey = request.getSourceObject();
    String sourceVersionId = request.getSourceVersionId();
    String destinationBucket = request.getDestinationBucket();
    String destinationKey = request.getDestinationObject();
    String copyIfMatch = request.getCopySourceIfMatch();
    String copyIfNoneMatch = request.getCopySourceIfNoneMatch();
    Date copyIfUnmodifiedSince = request.getCopySourceIfUnmodifiedSince();
    Date copyIfModifiedSince = request.getCopySourceIfModifiedSince();
    try {//from   w ww. j  a v a2  s  . co m
        CopyObjectRequest copyRequest = new CopyObjectRequest(sourceBucket, sourceKey, sourceVersionId,
                destinationBucket, destinationKey);
        copyRequest.setModifiedSinceConstraint(copyIfModifiedSince);
        copyRequest.setUnmodifiedSinceConstraint(copyIfUnmodifiedSince);
        if (copyIfMatch != null) {
            List<String> copyIfMatchConstraint = new ArrayList<String>();
            copyIfMatchConstraint.add(copyIfMatch);
            copyRequest.setMatchingETagConstraints(copyIfMatchConstraint);
        }
        if (copyIfNoneMatch != null) {
            List<String> copyIfNoneMatchConstraint = new ArrayList<String>();
            copyIfNoneMatchConstraint.add(copyIfNoneMatch);
            copyRequest.setNonmatchingETagConstraints(copyIfNoneMatchConstraint);
        }
        //TODO: Need to set canned ACL if specified
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();
        CopyObjectResult result = s3Client.copyObject(copyRequest);
        reply.setEtag(result.getETag());
        reply.setLastModified(DateFormatter.dateToListingFormattedString(result.getLastModifiedDate()));
        String destinationVersionId = result.getVersionId();
        if (destinationVersionId != null) {
            reply.setCopySourceVersionId(sourceVersionId);
            reply.setVersionId(destinationVersionId);
        }
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
    return reply;

}

From source file:com.streamsets.pipeline.stage.origin.s3.AmazonS3Util.java

License:Apache License

static void move(AmazonS3Client s3Client, String srcBucket, String sourceKey, String destBucket, String destKey)
        throws AmazonClientException {
    CopyObjectRequest cp = new CopyObjectRequest(srcBucket, sourceKey, destBucket, destKey);
    s3Client.copyObject(cp);
    s3Client.deleteObject(new DeleteObjectRequest(srcBucket, sourceKey));
}

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

License:Open Source License

@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
    Preconditions.checkArgument(source instanceof S3Path, "source must be an instance of %s",
            S3Path.class.getName());
    Preconditions.checkArgument(target instanceof S3Path, "target must be an instance of %s",
            S3Path.class.getName());

    if (isSameFile(source, target)) {
        return;//  w  w  w .  jav a2 s  . c o m
    }

    S3Path s3Source = (S3Path) source;
    S3Path s3Target = (S3Path) target;
    /*
     * Preconditions.checkArgument(!s3Source.isDirectory(),
     * "copying directories is not yet supported: %s", source); // TODO
     * Preconditions.checkArgument(!s3Target.isDirectory(),
     * "copying directories is not yet supported: %s", target); // TODO
     */
    ImmutableSet<CopyOption> actualOptions = ImmutableSet.copyOf(options);
    verifySupportedOptions(EnumSet.of(StandardCopyOption.REPLACE_EXISTING), actualOptions);

    if (!actualOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
        if (exists(s3Target)) {
            throw new FileAlreadyExistsException(format("target already exists: %s", target));
        }
    }

    AmazonS3Client client = s3Source.getFileSystem().getClient();

    final ObjectMetadata sourceObjMetadata = s3Source.getFileSystem().getClient()
            .getObjectMetadata(s3Source.getBucket(), s3Source.getKey());
    final S3MultipartOptions opts = props != null ? new S3MultipartOptions<>(props) : new S3MultipartOptions();
    final int chunkSize = opts.getChunkSize();
    final long length = sourceObjMetadata.getContentLength();

    if (length <= chunkSize) {

        CopyObjectRequest copyObjRequest = new CopyObjectRequest(s3Source.getBucket(), s3Source.getKey(),
                s3Target.getBucket(), s3Target.getKey());
        if (sourceObjMetadata.getSSEAlgorithm() != null) {
            ObjectMetadata targetObjectMetadata = new ObjectMetadata();
            targetObjectMetadata.setSSEAlgorithm(sourceObjMetadata.getSSEAlgorithm());
            copyObjRequest.setNewObjectMetadata(targetObjectMetadata);
        }

        client.copyObject(copyObjRequest);
    } else {
        client.multipartCopyObject(s3Source, s3Target, length, opts);
    }
}

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/*from   w  w w .  ja v a2  s.co  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);
        }
    }
}