Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 Amazon.com, 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
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.s3;
 
 import java.io.File;
 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 com.amazonaws.services.s3.model.UploadObjectRequest.withUploadObjectObserver(com.amazonaws.services.s3.UploadObjectObserver).

 
 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.

Parameters:
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
Returns:
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())
             .withMaterialsDescription(req.getMaterialsDescription())
             .withRedirectLocation(req.getRedirectLocation())
             .withSSECustomerKey(req.getSSECustomerKey())
             .withStorageClass(req.getStorageClass())
             .withAccessControlList(req.getAccessControlList())
             .withCannedACL(req.getCannedAcl())
            ;
    }

    
Notified from AmazonS3EncryptionClient.uploadObject(com.amazonaws.services.s3.model.UploadObjectRequest) to initiate a multi-part upload.

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

    
Notified from com.amazonaws.services.s3.internal.MultiFileOutputStream.fos() when a part ready for upload has been successfully created on disk. By default, this method performs the following:
  1. calls newUploadPartRequest(com.amazonaws.services.s3.internal.PartCreationEvent,java.io.File) 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(com.amazonaws.services.s3.model.UploadPartRequest), to be performed

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

Parameters:
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 =
            newUploadPartRequest(eventpart);
        final OnFileDelete fileDeleteObserver = event.getFileDeleteObserver();
        appendUserAgent(reqUploadPart.);
        .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()) {
                        LogFactory.getLog(getClass()).debug(
                                "Ignoring failure to delete file " + part
                                        + " which has already been uploaded");
                    } else {
                        if (fileDeleteObserver != null)
                            fileDeleteObserver.onFileDelete(null);
                    }
                }
            }
        }));
    }

    
Notified from AmazonS3EncryptionClient.uploadObject(com.amazonaws.services.s3.model.UploadObjectRequest) 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.

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

    
Notified from AmazonS3EncryptionClient.uploadObject(com.amazonaws.services.s3.model.UploadObjectRequest) 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()) {
            future.cancel(true);
        }
        if ( != null) {
            try {
                .abortMultipartUpload(new AbortMultipartUploadRequest(
                    .getBucketName(), .getKey(), ));
            } catch (Exception e) {
                LogFactory.getLog(getClass())
                    .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.

Parameters:
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()
            .withBucketName(.getBucketName())
            .withFile(part)
            .withKey(.getKey())
            .withPartNumber(event.getPartNumber())
            .withPartSize(part.length())
            .withLastPart(event.isLastPart())
            .withUploadId()
            .withObjectMetadata(.getUploadPartMetadata())
            ;
        return reqUploadPart;
    }

    
Uploads the ciphertext via the non-encrypting s3 client.

Parameters:
reqUploadPart part upload request
Returns:
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.

Returns:
the given request.
    protected <X extends AmazonWebServiceRequest> X appendUserAgent(
            X requestString userAgent) {
        request.getRequestClientOptions().appendUserAgent(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(com.amazonaws.services.s3.model.UploadObjectRequest)
    protected String getUploadId() {
        return ;
    }

    
    protected S3DirectSpi getS3DirectSpi() {
        return ;
    }

    
    protected AmazonS3 getAmazonS3() {
        return ;
    }

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