Class MultipartUtils


  • public class MultipartUtils
    extends Object
    Tool to simplify working with the multipart uploads feature offered by Amazon S3.
    Author:
    jmurty
    • Field Detail

      • MIN_PART_SIZE

        public static final long MIN_PART_SIZE
        Minimum multipart upload part size supported by S3: 5 MB. NOTE: This minimum size does not apply to the last part in a multipart upload, which may be 1 byte or larger.
        See Also:
        Constant Field Values
      • MAX_OBJECT_SIZE

        public static final long MAX_OBJECT_SIZE
        Maximum object size supported by S3: 5 GB
        See Also:
        Constant Field Values
    • Constructor Detail

      • MultipartUtils

        public MultipartUtils​(long maxPartSize)
        Parameters:
        maxPartSize - the maximum size of objects that will be generated or upload by this instance, must be between MIN_PART_SIZE and MAX_OBJECT_SIZE.
      • MultipartUtils

        public MultipartUtils()
        Use default value for maximum part size: MAX_OBJECT_SIZE.
    • Method Detail

      • getMaxPartSize

        public long getMaxPartSize()
        Returns:
        maximum part size as set in constructor.
      • isFileLargerThanMaxPartSize

        public boolean isFileLargerThanMaxPartSize​(File file)
        Parameters:
        file -
        Returns:
        true if the given file is larger than the maximum part size defined in this instances.
      • splitFileIntoObjectsByMaxPartSize

        public List<S3Object> splitFileIntoObjectsByMaxPartSize​(String objectKey,
                                                                File file)
                                                         throws IOException,
                                                                NoSuchAlgorithmException
        Split the given file into objects such that no object has a size greater than the defined maximum part size. Each object uses a SegmentedRepeatableFileInputStream input stream to manage its own byte range within the underlying file.
        Parameters:
        objectKey - the object key name to apply to all objects returned by this method.
        file - a file to split into multiple parts.
        Returns:
        an ordered list of objects that can be uploaded as multipart parts to S3 to re-constitute the given file in the service.
        Throws:
        IOException
        NoSuchAlgorithmException
      • uploadObjects

        public void uploadObjects​(String bucketName,
                                  S3Service s3Service,
                                  List<StorageObject> objectsForMultipartUpload,
                                  S3ServiceEventListener eventListener)
                           throws Exception
        Upload one or more file-based objects to S3 as multipart uploads, where each object's underlying file is split into parts based on the value of maxPartSize. Objects are uploaded in parallel using a ThreadedS3Service class that is created within this method, so uploads will take place using as many connections and threads as are configured in your service's Jets3tProperties. This method can upload small files that don't need to be split into parts, but because there is extra overhead in performing unnecessary multipart upload operations you should avoid doing so unless it's really necessary.
        Parameters:
        bucketName - the target bucket name
        s3Service - the S3 service that will perform the work
        objectsForMultipartUpload - a list of one or more objects that will be uploaded, potentially in multiple parts if the object's underlying file is larger than maxPartSize
        eventListener - an event listener to monitor progress event notifications, which should recognize and handle error events. May be null, in which case a standard S3ServiceEventAdaptor is used which won't report on events but will throw an exception if there is a failure.
        Throws:
        Exception