Java Code Examples for com.amazonaws.services.s3.AmazonS3#copyObject()

The following examples show how to use com.amazonaws.services.s3.AmazonS3#copyObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParallelCopier.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void copy(final AmazonS3 s3, final String bucket, final String srcKey, final String destKey) {
  long length = s3.getObjectMetadata(bucket, srcKey).getContentLength();

  try {
    if (length < chunkSize) {
      s3.copyObject(bucket, srcKey, bucket, destKey);
    }
    else {
      final AtomicInteger offset = new AtomicInteger(1);
      parallelRequests(s3, bucket, destKey,
          () -> (uploadId -> copyParts(s3, uploadId, bucket, srcKey, destKey, length, offset)));
    }
  }
  catch (SdkClientException e) {
    throw new BlobStoreException("Error copying blob", e, null);
  }
}
 
Example 2
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Copyto back up.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param from the from
 * @param to the to
 */
private void copytoBackUp(AmazonS3 s3client,String s3Bucket,String from, String to){
	String[] keys = listKeys(s3client,s3Bucket,from);
	String fileName ="";
	for(String key:keys){
		try{
			fileName = key.substring(key.lastIndexOf('/')+1);
			s3client.copyObject(s3Bucket,key,s3Bucket,to+"/"+fileName);
			log.debug("    Copy "+fileName + " to backup folder");
		}catch(Exception e){
			log.info("    Copy "+fileName + "failed",e);
			ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
		}
	}
}
 
Example 3
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Copyto back up.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param from the from
 * @param to the to
 */
private void copytoBackUp(AmazonS3 s3client,String s3Bucket,String from, String to){
	String[] keys = listKeys(s3client,s3Bucket,from);
	String fileName ="";
	for(String key:keys){
		try{
			fileName = key.substring(key.lastIndexOf('/')+1);
			s3client.copyObject(s3Bucket,key,s3Bucket,to+"/"+fileName);
			log.debug("    Copy "+fileName + " to backup folder");
		}catch(Exception e){
			log.info("    Copy "+fileName + "failed",e);
			ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
		}
	}
}
 
Example 4
Source File: TracingHandlerTest.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testS3CopyObjectSubsegmentContainsBucketName() {
    // Setup test
    final String copyResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" +
                                "<LastModified>2018-01-21T10:09:54.000Z</LastModified><ETag>" +
                                "&quot;31748afd7b576119d3c2471f39fc7a55&quot;</ETag>" +
                                "</CopyObjectResult>";
    AmazonS3 s3 = AmazonS3ClientBuilder
        .standard()
        .withRequestHandlers(new TracingHandler())
        .withRegion(Regions.US_EAST_1)
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake")))
        .build();
    mockHttpClient(s3, copyResponse);

    String bucket = "source-bucket";
    String key = "source-key";
    String dstBucket = "dest-bucket";
    String dstKey = "dest-key";
    // Test logic 
    Segment segment = AWSXRay.beginSegment("test");
    s3.copyObject(bucket, key, dstBucket, dstKey);
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("CopyObject", segment.getSubsegments().get(0).getAws().get("operation"));
    Assert.assertEquals(bucket, segment.getSubsegments().get(0).getAws().get("source_bucket_name"));
    Assert.assertEquals(key, segment.getSubsegments().get(0).getAws().get("source_key"));
    Assert.assertEquals(dstBucket, segment.getSubsegments().get(0).getAws().get("destination_bucket_name"));
    Assert.assertEquals(dstKey, segment.getSubsegments().get(0).getAws().get("destination_key"));
}
 
Example 5
Source File: S3ArchiveImageHandler.java    From smart-security-camera with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Parameters handleRequest(Parameters parameters, Context context) {

    context.getLogger().log("Input Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]");

    // The archive location of the snapshot will be decided by the alert
    // flag
    String newFilename;
    if (parameters.getSendAlert()) {
        newFilename =  parameters.getS3Key().replace("upload/", "archive/alerts/");
    } else {
        newFilename = parameters.getS3Key().replace("upload/", "archive/falsepositives/");
    }

    // Ensure that the first two hyphens are used to create sub-directories
    // in the file path
    newFilename = newFilename.replaceFirst("-", "/");
    newFilename = newFilename.replaceFirst("-", "/");

    // Using the S3 client, first copy the file to the archive, and then
    // delete the original
    AmazonS3 client = AmazonS3ClientBuilder.defaultClient();
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(parameters.getS3Bucket(), parameters.getS3Key(), parameters.getS3Bucket(), newFilename);
    client.copyObject(copyObjectRequest);
    DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(parameters.getS3Bucket(), parameters.getS3Key());
    client.deleteObject(deleteObjectRequest);

    // Place the new location in the parameters
    parameters.setS3ArchivedKey(newFilename);

    context.getLogger().log("Output Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]");

    return parameters;
}
 
Example 6
Source File: CopyObject.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "To run this example, supply the name (key) of an S3 object, the bucket name\n" +
            "that it's contained within, and the bucket to copy it to.\n" +
            "\n" +
            "Ex: CopyObject <objectname> <frombucket> <tobucket>\n";

    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String object_key = args[0];
    String from_bucket = args[1];
    String to_bucket = args[2];

    System.out.format("Copying object %s from bucket %s to %s\n",
            object_key, from_bucket, to_bucket);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        s3.copyObject(from_bucket, object_key, to_bucket, object_key);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
 
Example 7
Source File: SpecifyServerSideEncryption.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void changeSSEEncryptionStatusByCopying(AmazonS3 s3Client,
                                                       String bucketName,
                                                       String sourceKey,
                                                       String destKey) {
    // Upload a new, unencrypted object.
    PutObjectResult putResult = s3Client.putObject(bucketName, sourceKey, "Object example to encrypt by copying");
    System.out.println("Unencrypted object \"" + sourceKey + "\" uploaded.");
    printEncryptionStatus(putResult);

    // Make a copy of the object and use server-side encryption when storing the copy.
    CopyObjectRequest request = new CopyObjectRequest(bucketName,
            sourceKey,
            bucketName,
            destKey);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    request.setNewObjectMetadata(objectMetadata);

    // Perform the copy operation and display the copy's encryption status.
    CopyObjectResult response = s3Client.copyObject(request);
    System.out.println("Object \"" + destKey + "\" uploaded with SSE.");
    printEncryptionStatus(response);

    // Delete the original, unencrypted object, leaving only the encrypted copy in Amazon S3.
    s3Client.deleteObject(bucketName, sourceKey);
    System.out.println("Unencrypted object \"" + sourceKey + "\" deleted.");
}
 
Example 8
Source File: AmazonS3Util.java    From datacollector with Apache License 2.0 5 votes vote down vote up
static void copy(
    AmazonS3 s3Client,
    String srcBucket,
    String sourceKey,
    String destBucket,
    String destKey,
    boolean isMove
) {
  CopyObjectRequest cp = new CopyObjectRequest(srcBucket, sourceKey, destBucket, destKey);
  s3Client.copyObject(cp);
  if (isMove) {
    s3Client.deleteObject(new DeleteObjectRequest(srcBucket, sourceKey));
  }
}
 
Example 9
Source File: Rename.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{
AmazonKey amazonKey	= getAmazonKey(_session, argStruct);
AmazonS3 s3Client		= getAmazonS3(amazonKey);

	String bucket			= getNamedStringParam(argStruct, "bucket", null );
	String srckey			= getNamedStringParam(argStruct, "srckey", null );
	String deskey			= getNamedStringParam(argStruct, "destkey", null );
	String aes256key	= getNamedStringParam(argStruct, "aes256key", null );

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

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

	
	CopyObjectRequest cor = new CopyObjectRequest(bucket, srckey, bucket, deskey);
	
	if ( aes256key != null && !aes256key.isEmpty() ){
		cor.setSourceSSECustomerKey( new SSECustomerKey(aes256key) );
		cor.setDestinationSSECustomerKey( new SSECustomerKey(aes256key) );
	}
	

try {
	s3Client.copyObject(cor);
	s3Client.deleteObject(new DeleteObjectRequest(bucket, srckey));
	return cfBooleanData.TRUE;
} catch (Exception e) {
	throwException(_session, "AmazonS3: " + e.getMessage() );
	return cfBooleanData.FALSE;
}
}
 
Example 10
Source File: MultipartCopier.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private void copySinglePart(final AmazonS3 s3,
                            final String bucket,
                            final String sourcePath,
                            final String destinationPath) {
  s3.copyObject(bucket, sourcePath, bucket, destinationPath);
}
 
Example 11
Source File: Copy.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
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);

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;
}
}
 
Example 12
Source File: PrimitiveS3OperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Performs server-side copy of a regular file.
 */
@Override
public URI copyFile(URI source, URI target) throws IOException {
	source = source.normalize();
	target = target.normalize();
	PooledS3Connection connection = null;
	try {
		connection = connect(target);
		
		URI parentUri = URIUtils.getParentURI(target);
		if (parentUri != null) {
			Info parentInfo = info(parentUri, connection);
			if (parentInfo == null) {
				throw new IOException("Parent directory does not exist");
			}
		}
		
		AmazonS3 service = connection.getService();
		String[] sourcePath = getPath(source);
		if (sourcePath.length == 1) {
			throw new IOException("Cannot read from " + source);
		}
		String[] targetPath = getPath(target);
		if (targetPath.length == 1) {
			throw new IOException("Cannot write to " + target);
		}
		String sourceBucket = sourcePath[0];
		String sourceKey = sourcePath[1];
		String targetBucket = targetPath[0];
		String targetKey = targetPath[1];
		try {
			// server-side copy!
			service.copyObject(sourceBucket, sourceKey, targetBucket, targetKey);
			return target;
		} catch (AmazonClientException e) {
			throw S3Utils.getIOException(e);
		}
	} finally {
		disconnect(connection);
	}
}