List of usage examples for com.amazonaws.services.s3.model CopyObjectRequest CopyObjectRequest
public CopyObjectRequest(String sourceBucketName, String sourceKey, String destinationBucketName,
String destinationKey)
Constructs with basic options.
From source file:org.finra.dm.dao.impl.S3DaoImpl.java
License:Apache License
@Override public S3FileTransferResultsDto copyFile(final S3FileCopyRequestParamsDto params) throws InterruptedException { LOGGER.info(/*from w w w . j a v a 2 s.c om*/ String.format("Copying S3 object from s3://%s/%s to s3://%s/%s...", params.getSourceBucketName(), params.getS3KeyPrefix(), params.getTargetBucketName(), params.getS3KeyPrefix())); // Perform the copy. S3FileTransferResultsDto results = performTransfer(params, new Transferer() { @Override public Transfer performTransfer(TransferManager transferManager) { // Create a copy request. CopyObjectRequest copyObjectRequest = new CopyObjectRequest(params.getSourceBucketName(), params.getS3KeyPrefix(), params.getTargetBucketName(), params.getS3KeyPrefix()); copyObjectRequest .withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(params.getKmsKeyId())); return s3Operations.copyFile(copyObjectRequest, transferManager); } }); LOGGER.info("File \"" + params.getS3KeyPrefix() + "\" contains " + results.getTotalBytesTransferred() + " byte(s) which was successfully copied from source bucket:\"" + params.getSourceBucketName() + "\" to target bucket:\"" + params.getTargetBucketName() + "\" in " + DmDateUtils.formatDuration(results.getDurationMillis(), true)); LOGGER.info(String.format("Overall transfer rate: %.2f kBytes/s (%.2f Mbits/s)", getTransferRateInKilobytesPerSecond(results.getTotalBytesTransferred(), results.getDurationMillis()), getTransferRateInMegabitsPerSecond(results.getTotalBytesTransferred(), results.getDurationMillis()))); return results; }
From source file:org.finra.herd.dao.impl.S3DaoImpl.java
License:Apache License
@Override public S3FileTransferResultsDto copyFile(final S3FileCopyRequestParamsDto params) throws InterruptedException { LOGGER.info(/*from ww w .jav a 2 s .co m*/ "Copying S3 object... sourceS3Key=\"{}\" sourceS3BucketName=\"{}\" targetS3Key=\"{}\" targetS3BucketName=\"{}\"", params.getSourceObjectKey(), params.getSourceBucketName(), params.getTargetObjectKey(), params.getTargetBucketName()); // Perform the copy. S3FileTransferResultsDto results = performTransfer(params, new Transferer() { @Override public Transfer performTransfer(TransferManager transferManager) { // Create a copy request. CopyObjectRequest copyObjectRequest = new CopyObjectRequest(params.getSourceBucketName(), params.getSourceObjectKey(), params.getTargetBucketName(), params.getTargetObjectKey()); // If KMS Key ID is specified, set the AWS Key Management System parameters to be used to encrypt the object. if (StringUtils.isNotBlank(params.getKmsKeyId())) { copyObjectRequest .withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(params.getKmsKeyId())); } // Otherwise, specify the server-side encryption algorithm for encrypting the object using AWS-managed keys. else { ObjectMetadata metadata = new ObjectMetadata(); metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); copyObjectRequest.setNewObjectMetadata(metadata); } return s3Operations.copyFile(copyObjectRequest, transferManager); } }); LOGGER.info( "Copied S3 object. sourceS3Key=\"{}\" sourceS3BucketName=\"{}\" targetS3Key=\"{}\" targetS3BucketName=\"{}\" " + "totalBytesTransferred={} transferDuration=\"{}\"", params.getSourceObjectKey(), params.getSourceBucketName(), params.getTargetObjectKey(), params.getTargetBucketName(), results.getTotalBytesTransferred(), HerdDateUtils.formatDuration(results.getDurationMillis())); logOverallTransferRate(results); return results; }
From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java
License:Open Source License
@Override public void renameFile(String from, String to, String pp) throws IOException { this.s3clientLock.readLock().lock(); try {/*from w w w .j av a2 s.com*/ while (from.startsWith(File.separator)) from = from.substring(1); while (to.startsWith(File.separator)) to = to.substring(1); String fn = EncyptUtils.encString(from, Main.chunkStoreEncryptionEnabled); String tn = EncyptUtils.encString(to, Main.chunkStoreEncryptionEnabled); try { CopyObjectRequest req = new CopyObjectRequest(this.name, pp + "/" + fn, this.name, pp + "/" + tn); s3Service.copyObject(req); s3Service.deleteObject(this.name, pp + "/" + fn); } catch (Exception e1) { throw new IOException(e1); } } finally { this.s3clientLock.readLock().unlock(); } }
From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java
License:Open Source License
private void updateObject(String km, ObjectMetadata om) { try {/* ww w . j a v a2 s. c o m*/ CopyObjectRequest req = new CopyObjectRequest(name, km, name, km).withNewObjectMetadata(om); s3Service.copyObject(req); } catch (AmazonS3Exception e) { CopyObjectRequest req = new CopyObjectRequest(name, km, name, km + ".cpy").withNewObjectMetadata(om); s3Service.copyObject(req); s3Service.deleteObject(name, km); req = new CopyObjectRequest(name, km + ".cpy", name, km).withNewObjectMetadata(om); s3Service.copyObject(req); s3Service.deleteObject(name, km + ".cpy"); } }
From source file:org.openflamingo.fs.s3.S3ObjectProvider.java
License:Apache License
@Override public boolean copy(String from, String to) { // Assert.hasLength(from, " ?? ? 'from'? ."); Assert.hasLength(from, "Please enter the source path."); // Assert.hasLength(to, " ? 'to' ."); Assert.hasLength(to, "Please enter the destination path."); String fromBucket = S3Utils.getBucket(from); String toBucket = S3Utils.getBucket(to); String fromKey = StringUtils.remove(from, "/" + fromBucket + "/"); String toKey = S3Utils.getObjectKey(to); String fileName = getFileName(fromKey); try {/*from www . j a v a 2 s.co m*/ CopyObjectRequest copyObjectRequest = new CopyObjectRequest(fromBucket, fromKey, toBucket, toKey + fileName); awsClient.copyObject(copyObjectRequest); 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("Cannot 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("Cannot copy the file.", ace); } }
From source file:org.springframework.integration.aws.outbound.S3MessageHandler.java
License:Apache License
private Transfer copy(Message<?> requestMessage) { String sourceBucketName = obtainBucket(requestMessage); String sourceKey = null;//from w w w . ja v a 2 s.c o m if (this.keyExpression != null) { sourceKey = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class); } Assert.state(sourceKey != null, "The 'keyExpression' must not be null for 'copy' operation and can't evaluate to null. " + "Root object is: " + requestMessage); String destinationBucketName = null; if (this.destinationBucketExpression != null) { destinationBucketName = this.destinationBucketExpression.getValue(this.evaluationContext, requestMessage, String.class); } if (this.resourceIdResolver != null) { destinationBucketName = this.resourceIdResolver.resolveToPhysicalResourceId(destinationBucketName); } Assert.state(destinationBucketName != null, "The 'destinationBucketExpression' must not be null for 'copy' operation and can't evaluate to null. " + "Root object is: " + requestMessage); String destinationKey = null; if (this.destinationKeyExpression != null) { destinationKey = this.destinationKeyExpression.getValue(this.evaluationContext, requestMessage, String.class); } Assert.state(destinationKey != null, "The 'destinationKeyExpression' must not be null for 'copy' operation and can't evaluate to null. " + "Root object is: " + requestMessage); CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey); return this.transferManager.copy(copyObjectRequest); }
From source file:org.springframework.integration.aws.support.S3Session.java
License:Apache License
@Override public void rename(String pathFrom, String pathTo) throws IOException { String[] bucketKeyFrom = splitPathToBucketAndKey(pathFrom); String[] bucketKeyTo = splitPathToBucketAndKey(pathTo); CopyObjectRequest copyRequest = new CopyObjectRequest(bucketKeyFrom[0], bucketKeyFrom[1], bucketKeyTo[0], bucketKeyTo[1]);/*from ww w . j a v a2 s . c o m*/ this.amazonS3.copyObject(copyRequest); //Delete the source this.amazonS3.deleteObject(bucketKeyFrom[0], bucketKeyFrom[1]); }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public void moveObject(S3ObjectSummary objectSummary, String destBucket, String destKey) { try {/*from w w w .j a v a2s . c o m*/ // Copying object CopyObjectRequest copyObjRequest = new CopyObjectRequest(objectSummary.getBucketName(), objectSummary.getKey(), destBucket, destKey); s3Client.copyObject(copyObjRequest); DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(objectSummary.getBucketName(), objectSummary.getKey()); s3Client.deleteObject(deleteObjectRequest); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("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."); logger.error("Error Message: " + ace.getMessage()); } }
From source file:rg.ent.S3SampleB.java
License:Open Source License
public static void copyFileBtweenBuckets() { AWSCredentials credentials = null;//from w w w . j a v a 2 s . c o m try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/john/.aws/credentials), and is in valid format.", e); } String file_name = "640_262098533.JPG"; String source_bucket = "royaltygroupimages"; String dest_bucket = "royaltygroupupload"; AmazonS3 s3 = new AmazonS3Client(credentials); CopyObjectRequest cor = new CopyObjectRequest(source_bucket, file_name, dest_bucket, file_name); s3.copyObject(cor); s3.setObjectAcl(dest_bucket, file_name, CannedAccessControlList.PublicRead); //S3Object[] filteredObjects = s3Service.listObjects("sourceBucket", "appData/", null); //for(S3Object object: filteredObjects ){ // s3Service.copyObject("sourceBucket", "newAppData/" + object.getKey().substring(object.getKey().indexOf("/"), "destBucket", object, false); //} }
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 ww. ja v a2 s. 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"); } }