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:com.universal.storage.UniversalS3Storage.java
License:Open Source License
/** * This method uploads a file with a length greater than PART_SIZE (5Mb). * /*w ww. ja v a2 s . c o m*/ * @param file to be stored within the storage. * @param path is the path for this new file within the root. * @throws UniversalIOException when a specific IO error occurs. */ private void uploadFile(File file, String path) throws UniversalIOException { // Create a list of UploadPartResponse objects. You get one of these // for each part upload. List<PartETag> partETags = new ArrayList<PartETag>(); // Step 1: Initialize. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(this.settings.getRoot(), file.getName()); InitiateMultipartUploadResult initResponse = this.s3client.initiateMultipartUpload(initRequest); long contentLength = file.length(); long partSize = PART_SIZE; // Set part size to 5 MB. ObjectMetadata objectMetadata = new ObjectMetadata(); if (this.settings.getEncryption()) { objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } List<Tag> tags = new ArrayList<Tag>(); for (String key : this.settings.getTags().keySet()) { tags.add(new Tag(key, this.settings.getTags().get(key))); } try { this.triggerOnStoreFileListeners(); // Step 2: Upload parts. long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // Last part can be less than 5 MB. Adjust part size. partSize = Math.min(partSize, (contentLength - filePosition)); // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))) .withKey(file.getName()).withUploadId(initResponse.getUploadId()).withPartNumber(i) .withFileOffset(filePosition).withFile(file).withObjectMetadata(objectMetadata) .withPartSize(partSize); // Upload part and add response to our list. partETags.add(this.s3client.uploadPart(uploadRequest).getPartETag()); filePosition += partSize; } // Step 3: Complete. CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest( this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path)), file.getName(), initResponse.getUploadId(), partETags); CompleteMultipartUploadResult result = this.s3client.completeMultipartUpload(compRequest); StorageClass storageClass = getStorageClass(); if (storageClass != StorageClass.Standard) { CopyObjectRequest copyObjectRequest = new CopyObjectRequest(this.settings.getRoot(), file.getName(), this.settings.getRoot(), file.getName()).withStorageClass(storageClass); this.s3client.copyObject(copyObjectRequest); } if (!tags.isEmpty()) { this.s3client.setObjectTagging(new SetObjectTaggingRequest(this.settings.getRoot(), file.getName(), new ObjectTagging(tags))); } this.triggerOnFileStoredListeners(new UniversalStorageData(file.getName(), PREFIX_S3_URL + (this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))) + "/" + file.getName(), result.getVersionId(), this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path)))); } catch (Exception e) { this.s3client.abortMultipartUpload(new AbortMultipartUploadRequest(this.settings.getRoot(), file.getName(), initResponse.getUploadId())); UniversalIOException error = new UniversalIOException(e.getMessage()); this.triggerOnErrorListeners(error); throw error; } }
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;//from w w w . ja v a2s. 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:io.druid.storage.s3.S3DataSegmentMover.java
License:Apache License
/** * Copies an object and after that checks that the object is present at the target location, via a separate API call. * If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check * is added after it was observed that S3 may report a successful move, and the object is not found at the target * location./* w ww. ja v a 2s. com*/ */ private void selfCheckingMove(String s3Bucket, String targetS3Bucket, String s3Path, String targetS3Path, String copyMsg) throws IOException, SegmentLoadingException { if (s3Bucket.equals(targetS3Bucket) && s3Path.equals(targetS3Path)) { log.info("No need to move file[s3://%s/%s] onto itself", s3Bucket, s3Path); return; } if (s3Client.doesObjectExist(s3Bucket, s3Path)) { final ListObjectsV2Result listResult = s3Client.listObjectsV2( new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Path).withMaxKeys(1)); if (listResult.getKeyCount() == 0) { // should never happen throw new ISE("Unable to list object [s3://%s/%s]", s3Bucket, s3Path); } final S3ObjectSummary objectSummary = listResult.getObjectSummaries().get(0); if (objectSummary.getStorageClass() != null && StorageClass.fromValue(StringUtils.toUpperCase(objectSummary.getStorageClass())) .equals(StorageClass.Glacier)) { throw new AmazonServiceException(StringUtils.format( "Cannot move file[s3://%s/%s] of storage class glacier, skipping.", s3Bucket, s3Path)); } else { log.info("Moving file %s", copyMsg); final CopyObjectRequest copyRequest = new CopyObjectRequest(s3Bucket, s3Path, targetS3Bucket, targetS3Path); if (!config.getDisableAcl()) { copyRequest .setAccessControlList(S3Utils.grantFullControlToBucketOwner(s3Client, targetS3Bucket)); } s3Client.copyObject(copyRequest); if (!s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) { throw new IOE( "After copy was reported as successful the file doesn't exist in the target location [%s]", copyMsg); } deleteWithRetriesSilent(s3Bucket, s3Path); log.debug("Finished moving file %s", copyMsg); } } else { // ensure object exists in target location if (s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) { log.info("Not moving file [s3://%s/%s], already present in target location [s3://%s/%s]", s3Bucket, s3Path, targetS3Bucket, targetS3Path); } else { throw new SegmentLoadingException( "Unable to move file %s, not present in either source or target location", copyMsg); } } }
From source file:io.konig.camel.aws.s3.DeleteObjectProducer.java
License:Apache License
private void copyObject(AmazonS3 s3Client, Exchange exchange) { String bucketNameDestination; String destinationKey;/*from w w w. j a va 2s . c o m*/ String sourceKey; String bucketName; String versionId; bucketName = exchange.getIn().getHeader(S3Constants.BUCKET_NAME, String.class); if (ObjectHelper.isEmpty(bucketName)) { bucketName = getConfiguration().getBucketName(); } sourceKey = exchange.getIn().getHeader(S3Constants.KEY, String.class); destinationKey = exchange.getIn().getHeader(S3Constants.DESTINATION_KEY, String.class); bucketNameDestination = exchange.getIn().getHeader(S3Constants.BUCKET_DESTINATION_NAME, String.class); versionId = exchange.getIn().getHeader(S3Constants.VERSION_ID, String.class); if (ObjectHelper.isEmpty(bucketName)) { throw new IllegalArgumentException("Bucket Name must be specified for copyObject Operation"); } if (ObjectHelper.isEmpty(bucketNameDestination)) { throw new IllegalArgumentException( "Bucket Name Destination must be specified for copyObject Operation"); } if (ObjectHelper.isEmpty(sourceKey)) { throw new IllegalArgumentException("Source Key must be specified for copyObject Operation"); } if (ObjectHelper.isEmpty(destinationKey)) { throw new IllegalArgumentException("Destination Key must be specified for copyObject Operation"); } CopyObjectRequest copyObjectRequest; if (ObjectHelper.isEmpty(versionId)) { copyObjectRequest = new CopyObjectRequest(bucketName, sourceKey, bucketNameDestination, destinationKey); } else { copyObjectRequest = new CopyObjectRequest(bucketName, sourceKey, versionId, bucketNameDestination, destinationKey); } if (getConfiguration().isUseAwsKMS()) { SSEAwsKeyManagementParams keyManagementParams; if (ObjectHelper.isNotEmpty(getConfiguration().getAwsKMSKeyId())) { keyManagementParams = new SSEAwsKeyManagementParams(getConfiguration().getAwsKMSKeyId()); } else { keyManagementParams = new SSEAwsKeyManagementParams(); } copyObjectRequest.setSSEAwsKeyManagementParams(keyManagementParams); } CopyObjectResult copyObjectResult = s3Client.copyObject(copyObjectRequest); Message message = getMessageForResponse(exchange); message.setHeader(S3Constants.E_TAG, copyObjectResult.getETag()); if (copyObjectResult.getVersionId() != null) { message.setHeader(S3Constants.VERSION_ID, copyObjectResult.getVersionId()); } }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public boolean copyEntity(String sourceBucketName, String sourceKeyName, String destinationBucketName, String destinationKeyName) { // If target bucket name is null or empty, that mean copy inside current // bucket.// w w w .java 2 s . c om if (StringUtils.isEmpty(destinationBucketName)) { destinationBucketName = sourceBucketName; } LOG.info("Copies a source object " + sourceKeyName + " from a source bucket " + sourceBucketName + " to a new destination bucket " + destinationBucketName + " with specified key " + destinationKeyName + " in Amazon S3"); try { CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKeyName, destinationBucketName, destinationKeyName); CopyObjectResult copyObjectResult = amazonS3Client.copyObject(copyObjectRequest); if (copyObjectResult != null) { LOG.info( "A CopyObjectResult object containing the information returned by Amazon S3 about the newly created object: " + copyObjectResult); return true; } } catch (AmazonServiceException ase) { LOG.warn(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.warn(ace.getMessage(), ace); } return false; }
From source file:io.minio.awssdk.tests.S3TestUtils.java
License:Apache License
void copyObject(String bucketName, String keyName, SSECustomerKey sseKey, String targetBucketName, String targetKeyName, SSECustomerKey newSseKey, boolean replace) { CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, keyName, targetBucketName, targetKeyName); if (sseKey != null) { copyRequest.withSourceSSECustomerKey(sseKey); }/*from w w w. j a v a2 s. c o m*/ if (newSseKey != null) { copyRequest.withDestinationSSECustomerKey(newSseKey); } if (replace) { copyRequest.withMetadataDirective(MetadataDirective.COPY); } s3Client.copyObject(copyRequest); }
From source file:it.openutils.mgnlaws.magnolia.datastore.S3DataStore.java
License:Open Source License
private void touch(String key) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setLastModified(new Date()); CopyObjectRequest req = new CopyObjectRequest(bucket, key, bucket, key).withNewObjectMetadata(metadata); amazonS3.copyObject(req);/*www . ja va 2 s . c om*/ }
From source file:nl.nn.adapterframework.filesystem.AmazonS3FileSystem.java
License:Apache License
/** * Copies a file from one Amazon S3 bucket to another one. * * @param fileName// w ww . j a v a 2 s.c om * This is the name of the file that is desired to be copied. * * @param destinationFileName * The name of the destination file */ public String copyObject(String fileName, String destinationFileName) throws SenderException { try { bucketDoesNotExist(bucketName); //if bucket does not exists this method throws an exception fileDoesNotExist(bucketName, fileName); //if object does not exists this method throws an exception if (!s3Client.doesBucketExistV2(destinationBucketName)) bucketCreationWithObjectAction(destinationBucketName); if (!s3Client.doesObjectExist(destinationBucketName, destinationFileName)) { CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, fileName, destinationBucketName, destinationFileName); if (isStorageClassEnabled()) copyObjectRequest.setStorageClass(getStorageClass()); s3Client.copyObject(copyObjectRequest); log.debug("Object with fileName [" + fileName + "] copied from bucket with bucketName [" + bucketName + "] into bucket with bucketName [" + destinationBucketName + "] and new fileName [" + destinationFileName + "]"); } else throw new SenderException(" file with given name already exists, please specify a new name"); } catch (AmazonServiceException e) { log.error("Failed to perform [copy] action on object with fileName [" + fileName + "]"); throw new SenderException("Failed to perform [copy] action on object with fileName [" + fileName + "]"); } return destinationFileName; }
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 www . j a v a2 s . co m*/ 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.alanwilliamson.amazon.s3.Rename.java
License:Open Source License
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);/*from w ww .j a v a 2s.c o m*/ 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; } }