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

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

Introduction

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

Prototype

@Override
    public void abortMultipartUpload(AbortMultipartUploadRequest abortMultipartUploadRequest)
            throws SdkClientException, AmazonServiceException 

Source Link

Usage

From source file:baldrickv.s3streamingtool.S3CleanupMultipart.java

License:Open Source License

public static void cleanup(S3StreamConfig config) throws Exception {

    AmazonS3Client s3 = config.getS3Client();
    String bucket = config.getS3Bucket();

    ListMultipartUploadsRequest list_req = new ListMultipartUploadsRequest(bucket);

    List<MultipartUpload> list = s3.listMultipartUploads(list_req).getMultipartUploads();

    Scanner scan = new Scanner(System.in);

    for (MultipartUpload mu : list) {
        System.out.println("-----------------------");
        System.out.println("  bucket: " + bucket);
        System.out.println("  key: " + mu.getKey());
        System.out.println("  uploadId: " + mu.getUploadId());
        System.out.println("  initiated at: " + mu.getInitiated());
        System.out.println("  initiated by: " + mu.getInitiator());
        System.out.println("-----------------------");

        System.out.print("Abort this upload [y|N]? ");
        String result = scan.nextLine().trim().toLowerCase();
        if (result.equals("y")) {
            AbortMultipartUploadRequest abort = new AbortMultipartUploadRequest(bucket, mu.getKey(),
                    mu.getUploadId());/*ww w . ja v  a  2s  . c om*/

            s3.abortMultipartUpload(abort);
            System.out.println("Aborted upload");
        } else {
            System.out.println("Leaving this one alone");

        }

    }

}

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

License:Open Source License

@Override
public AbortMultipartUploadResponseType abortMultipartUpload(AbortMultipartUploadType request)
        throws S3Exception {
    AbortMultipartUploadResponseType reply = request.getReply();
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;

    String bucketName = request.getBucket();
    String key = request.getKey();
    String uploadId = request.getUploadId();
    AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key,
            uploadId);//  w  ww .ja  v a2s .c  o m
    try {
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();

        s3Client.abortMultipartUpload(abortMultipartUploadRequest);
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
    return reply;
}

From source file:com.netflix.dynomitemanager.sidecore.backup.S3Backup.java

License:Apache License

/**
 * Uses the Amazon S3 API to upload the AOF/RDB to S3
 * Filename: Backup location + DC + Rack + App + Token
 *//* w ww . j a  v  a 2  s.  c  om*/
@Override
public boolean upload(File file, DateTime todayStart) {
    logger.info("Snapshot backup: sending " + file.length() + " bytes to S3");

    /* Key name is comprised of the 
    * backupDir + DC + Rack + token + Date
    */
    String keyName = config.getBackupLocation() + "/" + iid.getInstance().getDatacenter() + "/"
            + iid.getInstance().getRack() + "/" + iid.getInstance().getToken() + "/" + todayStart.getMillis();

    // Get bucket location.
    logger.info("Key in Bucket: " + keyName);
    logger.info("S3 Bucket Name:" + config.getBucketName());

    AmazonS3Client s3Client = new AmazonS3Client(cred.getAwsCredentialProvider());

    try {
        // Checking if the S3 bucket exists, and if does not, then we create it

        if (!(s3Client.doesBucketExist(config.getBucketName()))) {
            logger.error("Bucket with name: " + config.getBucketName() + " does not exist");
            return false;
        } else {
            logger.info("Uploading data to S3\n");
            // Create a list of UploadPartResponse objects. You get one of these for
            // each part upload.
            List<PartETag> partETags = new ArrayList<PartETag>();

            InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
                    config.getBucketName(), keyName);

            InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);

            long contentLength = file.length();
            long filePosition = 0;
            long partSize = this.initPartSize;

            try {
                for (int i = 1; filePosition < contentLength; i++) {
                    // Last part can be less than initPartSize (500MB). Adjust part size.
                    partSize = Math.min(partSize, (contentLength - filePosition));

                    // Create request to upload a part.
                    UploadPartRequest uploadRequest = new UploadPartRequest()
                            .withBucketName(config.getBucketName()).withKey(keyName)
                            .withUploadId(initResponse.getUploadId()).withPartNumber(i)
                            .withFileOffset(filePosition).withFile(file).withPartSize(partSize);

                    // Upload part and add response to our list.
                    partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());

                    filePosition += partSize;
                }

                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
                        config.getBucketName(), keyName, initResponse.getUploadId(), partETags);

                s3Client.completeMultipartUpload(compRequest);

            } catch (Exception e) {
                logger.error("Abosting multipart upload due to error");
                s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(config.getBucketName(), keyName,
                        initResponse.getUploadId()));
            }

            return true;
        }
    } catch (AmazonServiceException ase) {

        logger.error(
                "AmazonServiceException;" + " request made it to Amazon S3, but was rejected with an error ");
        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());
        return false;

    } catch (AmazonClientException ace) {
        logger.error("AmazonClientException;" + " the client encountered "
                + "an internal error while trying to " + "communicate with S3, ");
        logger.error("Error Message: " + ace.getMessage());
        return false;
    }
}

From source file:com.netflix.exhibitor.core.s3.S3ClientFactoryImpl.java

License:Apache License

@Override
public S3Client makeNewClient(final S3Credential credentials) throws Exception {
    return new S3Client() {
        private final AtomicReference<RefCountedClient> client = new AtomicReference<RefCountedClient>(null);

        {/*from w  ww. j  ava2 s.c  om*/
            changeCredentials(credentials);
        }

        @Override
        public void changeCredentials(S3Credential credential) throws Exception {
            RefCountedClient newRefCountedClient = (credential != null) ? new RefCountedClient(
                    new AmazonS3Client(new BasicAWSCredentials(credentials.getAccessKeyId(),
                            credentials.getSecretAccessKey())))
                    : null;
            RefCountedClient oldRefCountedClient = client.getAndSet(newRefCountedClient);
            if (oldRefCountedClient != null) {
                oldRefCountedClient.markForDelete();
            }
        }

        @Override
        public void close() throws IOException {
            try {
                changeCredentials(null);
            } catch (Exception e) {
                throw new IOException(e);
            }
        }

        @Override
        public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request)
                throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.initiateMultipartUpload(request);
            } finally {
                holder.release();
            }
        }

        @Override
        public PutObjectResult putObject(PutObjectRequest request) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.putObject(request);
            } finally {
                holder.release();
            }
        }

        @Override
        public S3Object getObject(String bucket, String key) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.getObject(bucket, key);
            } finally {
                holder.release();
            }
        }

        @Override
        public ObjectListing listObjects(ListObjectsRequest request) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.listObjects(request);
            } finally {
                holder.release();
            }
        }

        @Override
        public ObjectListing listNextBatchOfObjects(ObjectListing previousObjectListing) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.listNextBatchOfObjects(previousObjectListing);
            } finally {
                holder.release();
            }
        }

        @Override
        public void deleteObject(String bucket, String key) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                amazonS3Client.deleteObject(bucket, key);
            } finally {
                holder.release();
            }
        }

        @Override
        public UploadPartResult uploadPart(UploadPartRequest request) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                return amazonS3Client.uploadPart(request);
            } finally {
                holder.release();
            }
        }

        @Override
        public void completeMultipartUpload(CompleteMultipartUploadRequest request) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                amazonS3Client.completeMultipartUpload(request);
            } finally {
                holder.release();
            }
        }

        @Override
        public void abortMultipartUpload(AbortMultipartUploadRequest request) throws Exception {
            RefCountedClient holder = client.get();
            AmazonS3Client amazonS3Client = holder.useClient();
            try {
                amazonS3Client.abortMultipartUpload(request);
            } finally {
                holder.release();
            }
        }
    };
}

From source file:com.netflix.exhibitor.core.s3.S3ClientImpl.java

License:Apache License

@Override
public void abortMultipartUpload(AbortMultipartUploadRequest request) throws Exception {
    RefCountedClient holder = client.get();
    AmazonS3Client amazonS3Client = holder.useClient();
    try {//from  w w  w . ja  va  2s  .  co  m
        amazonS3Client.abortMultipartUpload(request);
    } finally {
        holder.release();
    }
}

From source file:com.streamsets.datacollector.bundles.SupportBundleManager.java

License:Apache License

/**
 * Instead of providing support bundle directly to user, upload it to StreamSets backend services.
 *//*from ww w  .j a  v  a 2s  .c  o m*/
public void uploadNewBundleFromInstances(List<BundleContentGenerator> generators, BundleType bundleType)
        throws IOException {
    // Generate bundle
    SupportBundle bundle = generateNewBundleFromInstances(generators, bundleType);

    boolean enabled = configuration.get(Constants.UPLOAD_ENABLED, Constants.DEFAULT_UPLOAD_ENABLED);
    String accessKey = configuration.get(Constants.UPLOAD_ACCESS, Constants.DEFAULT_UPLOAD_ACCESS);
    String secretKey = configuration.get(Constants.UPLOAD_SECRET, Constants.DEFAULT_UPLOAD_SECRET);
    String bucket = configuration.get(Constants.UPLOAD_BUCKET, Constants.DEFAULT_UPLOAD_BUCKET);
    int bufferSize = configuration.get(Constants.UPLOAD_BUFFER_SIZE, Constants.DEFAULT_UPLOAD_BUFFER_SIZE);

    if (!enabled) {
        throw new IOException("Uploading support bundles was disabled by administrator.");
    }

    AWSCredentialsProvider credentialsProvider = new StaticCredentialsProvider(
            new BasicAWSCredentials(accessKey, secretKey));
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider, new ClientConfiguration());
    s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
    s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));

    // Object Metadata
    ObjectMetadata s3Metadata = new ObjectMetadata();
    for (Map.Entry<Object, Object> entry : getMetadata(bundleType).entrySet()) {
        s3Metadata.addUserMetadata((String) entry.getKey(), (String) entry.getValue());
    }

    List<PartETag> partETags;
    InitiateMultipartUploadResult initResponse = null;
    try {
        // Uploading part by part
        LOG.info("Initiating multi-part support bundle upload");
        partETags = new ArrayList<>();
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket,
                bundle.getBundleKey());
        initRequest.setObjectMetadata(s3Metadata);
        initResponse = s3Client.initiateMultipartUpload(initRequest);
    } catch (AmazonClientException e) {
        LOG.error("Support bundle upload failed: ", e);
        throw new IOException("Support bundle upload failed", e);
    }

    try {
        byte[] buffer = new byte[bufferSize];
        int partId = 1;
        int size = -1;
        while ((size = readFully(bundle.getInputStream(), buffer)) != -1) {
            LOG.debug("Uploading part {} of size {}", partId, size);
            UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucket)
                    .withKey(bundle.getBundleKey()).withUploadId(initResponse.getUploadId())
                    .withPartNumber(partId++).withInputStream(new ByteArrayInputStream(buffer))
                    .withPartSize(size);

            partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
        }

        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucket,
                bundle.getBundleKey(), initResponse.getUploadId(), partETags);

        s3Client.completeMultipartUpload(compRequest);
        LOG.info("Support bundle upload finished");
    } catch (Exception e) {
        LOG.error("Support bundle upload failed", e);
        s3Client.abortMultipartUpload(
                new AbortMultipartUploadRequest(bucket, bundle.getBundleKey(), initResponse.getUploadId()));

        throw new IOException("Can't upload support bundle", e);
    } finally {
        // Close the client
        s3Client.shutdown();
    }
}

From source file:org.apache.nifi.processors.aws.s3.PutS3Object.java

License:Apache License

protected void abortS3MultipartUpload(final AmazonS3Client s3, final String bucket,
        final MultipartUpload upload) {
    final String uploadKey = upload.getKey();
    final String uploadId = upload.getUploadId();
    final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(bucket, uploadKey,
            uploadId);/*from w  ww . j  a  v  a 2 s.  co m*/
    try {
        s3.abortMultipartUpload(abortRequest);
        getLogger().info("Aborting out of date multipart upload, bucket {} key {} ID {}, initiated {}",
                new Object[] { bucket, uploadKey, uploadId, logFormat.format(upload.getInitiated()) });
    } catch (AmazonClientException ace) {
        getLogger().info("Error trying to abort multipart upload from bucket {} with key {} and ID {}: {}",
                new Object[] { bucket, uploadKey, uploadId, ace.getMessage() });
    }
}

From source file:org.finra.dm.dao.impl.S3OperationsImpl.java

License:Apache License

/**
 * {@inheritDoc}//from w  w  w  .  j a  va 2 s  .  c o  m
 */
@Override
public void abortMultipartUpload(AbortMultipartUploadRequest abortMultipartUploadRequest,
        AmazonS3Client s3Client) {
    s3Client.abortMultipartUpload(abortMultipartUploadRequest);
}