Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2014-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 import java.util.List;
An observer that gets notified of ciphertext file creation for the purpose of pipelined parallel multi-part uploads of encrypted data to S3. This observer is responsible for uploading the files to S3 via multi-part upload, including the multi-part upload initiation, individual part uploads, and multi-part upload completion.

This observer is designed for extension so that custom behavior can be provided. A customer observer can be configured via

 public class UploadObjectObserver {
     private final List<Future<UploadPartResult>> futures = new ArrayList<Future<UploadPartResult>>();
     private UploadObjectRequest req;
     private String uploadId;
     private S3DirectSpi s3direct;
     private AmazonS3 s3;
     private ExecutorService es;

Used to initialized this observer. This method is an SPI (service provider interface) that is called from AmazonS3EncryptionClient.

Implementation of this method should never block.

req the upload object request
s3direct used to perform non-encrypting s3 operation via the current instance of s3 (encryption) client
s3 the current instance of s3 (encryption) client
es the executor service to be used for concurrent uploads
this object
             S3DirectSpi s3directAmazonS3 s3ExecutorService es) {
         this. = req;
         this. = s3direct;
         this. = s3;
         this. = es;
         return this;
             UploadObjectRequest req) {
         return new EncryptedInitiateMultipartUploadRequest(
                 req.getBucketName(), req.getKey(), req.getMetadata())

Notified from AmazonS3EncryptionClient.uploadObject( to initiate a multi-part upload.

req the upload object request
the initiated multi-part uploadId
        InitiateMultipartUploadResult res =
        return this. = res.getUploadId();

Notified from when a part ready for upload has been successfully created on disk. By default, this method performs the following:
  1. calls newUploadPartRequest(, to create an upload-part request for the newly created ciphertext file
  2. call appendUserAgent(com.amazonaws.AmazonWebServiceRequest,java.lang.String) to append the necessary user agent string to the request
  3. and finally submit a concurrent task, which calls the method uploadPart(, to be performed

To enable parallel uploads, implementation of this method should never block.

event to represent the completion of a ciphertext file creation which is ready for multipart upload to S3.
    public void onPartCreate(PartCreationEvent event) {
        final File part = event.getPart();
        final UploadPartRequest reqUploadPart =
        final OnFileDelete fileDeleteObserver = event.getFileDeleteObserver();
        .add(.submit(new Callable<UploadPartResult>() {
            @Override public UploadPartResult call() {
                // Upload the ciphertext directly via the non-encrypting
                // s3 client
                try {
                    return uploadPart(reqUploadPart);
                } finally {
                    // clean up part already uploaded 
                    if (!part.delete()) {
                                "Ignoring failure to delete file " + part
                                        + " which has already been uploaded");
                    } else {
                        if (fileDeleteObserver != null)

Notified from AmazonS3EncryptionClient.uploadObject( when all parts have been successfully uploaded to S3. This method is responsible for finishing off the upload by making a complete multi-part upload request to S3 with the given list of etags.

partETags all the etags returned from S3 for the previous part uploads.
the completed multi-part upload result
        return .completeMultipartUpload(
            new CompleteMultipartUploadRequest(
                    .getBucketName(), .getKey(), partETags));

Notified from AmazonS3EncryptionClient.uploadObject( when failed to upload any part. This method is responsible for cancelling ongoing uploads and aborting the multi-part upload request.
    public void onAbort() {
        for (Future<?> future : getFutures()) {
        if ( != null) {
            try {
                .abortMultipartUpload(new AbortMultipartUploadRequest(
                    .getBucketName(), .getKey(), ));
            } catch (Exception e) {
                    .debug("Failed to abort multi-part upload: " + e);
Creates and returns an upload-part request corresponding to a ciphertext file upon a part-creation event.

event the part-creation event of the ciphertxt file.
part the created ciphertext file corresponding to the upload-part
            final File part) {
        final UploadPartRequest reqUploadPart = new UploadPartRequest()
        return reqUploadPart;

Uploads the ciphertext via the non-encrypting s3 client.

reqUploadPart part upload request
the result of the part upload when there is no exception
    protected UploadPartResult uploadPart(UploadPartRequest reqUploadPart) {
        // Upload the ciphertext directly via the non-encrypting
        // s3 client
        return .uploadPart(reqUploadPart);

Appends the given user agent to the given request.

the given request.
    protected <X extends AmazonWebServiceRequest> X appendUserAgent(
            X requestString userAgent) {
        return request;
    public List<Future<UploadPartResult>> getFutures() {
        return ;

    protected UploadObjectRequest getRequest() {
        return ;

Returns the upload id after the multi-part upload has been initiated via onUploadInitiation(
    protected String getUploadId() {
        return ;

    protected S3DirectSpi getS3DirectSpi() {
        return ;

    protected AmazonS3 getAmazonS3() {
        return ;

    protected ExecutorService getExecutorService() {
        return ;
New to GrepCode? Check out our FAQ X