Search in sources :

Example 1 with PartETag

use of com.amazonaws.services.s3.model.PartETag in project elasticsearch by elastic.

the class DefaultS3OutputStream method doUploadMultipart.

protected PartETag doUploadMultipart(S3BlobStore blobStore, String bucketName, String blobName, String uploadId, InputStream is, int length, boolean lastPart) throws AmazonS3Exception {
    UploadPartRequest request = new UploadPartRequest().withBucketName(bucketName).withKey(blobName).withUploadId(uploadId).withPartNumber(multipartChunks).withInputStream(is).withPartSize(length).withLastPart(lastPart);
    UploadPartResult response = blobStore.client().uploadPart(request);
    return response.getPartETag();
}
Also used : UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest)

Example 2 with PartETag

use of com.amazonaws.services.s3.model.PartETag in project elasticsearch by elastic.

the class DefaultS3OutputStream method uploadMultipart.

private void uploadMultipart(byte[] bytes, int off, int len, boolean lastPart) throws IOException {
    try (ByteArrayInputStream is = new ByteArrayInputStream(bytes, off, len)) {
        int retry = 0;
        while (retry <= getNumberOfRetries()) {
            try {
                PartETag partETag = doUploadMultipart(getBlobStore(), getBucketName(), getBlobName(), multipartId, is, len, lastPart);
                multiparts.add(partETag);
                multipartChunks++;
                return;
            } catch (AmazonClientException e) {
                if (getBlobStore().shouldRetry(e) && retry < getNumberOfRetries()) {
                    is.reset();
                    retry++;
                } else {
                    abortMultipart();
                    throw e;
                }
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonClientException(com.amazonaws.AmazonClientException) PartETag(com.amazonaws.services.s3.model.PartETag)

Example 3 with PartETag

use of com.amazonaws.services.s3.model.PartETag in project android-simpl3r by jgilfelt.

the class Uploader method getCachedPartEtags.

private List<PartETag> getCachedPartEtags() {
    List<PartETag> result = new ArrayList<PartETag>();
    // get the cached etags
    ArrayList<String> etags = SharedPreferencesUtils.getStringArrayPref(prefs, s3key + PREFS_ETAGS);
    for (String etagString : etags) {
        String partNum = etagString.substring(0, etagString.indexOf(PREFS_ETAG_SEP));
        String partTag = etagString.substring(etagString.indexOf(PREFS_ETAG_SEP) + 2, etagString.length());
        PartETag etag = new PartETag(Integer.parseInt(partNum), partTag);
        result.add(etag);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) PartETag(com.amazonaws.services.s3.model.PartETag)

Example 4 with PartETag

use of com.amazonaws.services.s3.model.PartETag in project elasticsearch by elastic.

the class DefaultS3OutputStream method doCompleteMultipart.

protected void doCompleteMultipart(S3BlobStore blobStore, String bucketName, String blobName, String uploadId, List<PartETag> parts) throws AmazonS3Exception {
    CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucketName, blobName, uploadId, parts);
    blobStore.client().completeMultipartUpload(request);
}
Also used : CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 5 with PartETag

use of com.amazonaws.services.s3.model.PartETag in project android-simpl3r by jgilfelt.

the class Uploader method start.

/**
	 * Initiate a multipart file upload to Amazon S3
	 * 
	 * @return the URL of a successfully uploaded file
	 */
public String start() {
    // initialize
    List<PartETag> partETags = new ArrayList<PartETag>();
    final long contentLength = file.length();
    long filePosition = 0;
    int startPartNumber = 1;
    userInterrupted = false;
    userAborted = false;
    bytesUploaded = 0;
    // check if we can resume an incomplete download
    String uploadId = getCachedUploadId();
    if (uploadId != null) {
        // we can resume the download
        Log.i(TAG, "resuming upload for " + uploadId);
        // get the cached etags
        List<PartETag> cachedEtags = getCachedPartEtags();
        partETags.addAll(cachedEtags);
        // calculate the start position for resume
        startPartNumber = cachedEtags.size() + 1;
        filePosition = (startPartNumber - 1) * partSize;
        bytesUploaded = filePosition;
        Log.i(TAG, "resuming at part " + startPartNumber + " position " + filePosition);
    } else {
        // initiate a new multi part upload
        Log.i(TAG, "initiating new upload");
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(s3bucketName, s3key);
        configureInitiateRequest(initRequest);
        InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
        uploadId = initResponse.getUploadId();
    }
    final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(s3bucketName, s3key, uploadId);
    for (int k = startPartNumber; filePosition < contentLength; k++) {
        long thisPartSize = Math.min(partSize, (contentLength - filePosition));
        Log.i(TAG, "starting file part " + k + " with size " + thisPartSize);
        UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(s3bucketName).withKey(s3key).withUploadId(uploadId).withPartNumber(k).withFileOffset(filePosition).withFile(file).withPartSize(thisPartSize);
        ProgressListener s3progressListener = new ProgressListener() {

            public void progressChanged(ProgressEvent progressEvent) {
                // TODO calling shutdown too brute force?
                if (userInterrupted) {
                    s3Client.shutdown();
                    throw new UploadIterruptedException("User interrupted");
                } else if (userAborted) {
                    // aborted requests cannot be resumed, so clear any cached etags
                    clearProgressCache();
                    s3Client.abortMultipartUpload(abortRequest);
                    s3Client.shutdown();
                }
                bytesUploaded += progressEvent.getBytesTransfered();
                //Log.d(TAG, "bytesUploaded=" + bytesUploaded);
                // broadcast progress
                float fpercent = ((bytesUploaded * 100) / contentLength);
                int percent = Math.round(fpercent);
                if (progressListener != null) {
                    progressListener.progressChanged(progressEvent, bytesUploaded, percent);
                }
            }
        };
        uploadRequest.setProgressListener(s3progressListener);
        UploadPartResult result = s3Client.uploadPart(uploadRequest);
        partETags.add(result.getPartETag());
        // cache the part progress for this upload
        if (k == 1) {
            initProgressCache(uploadId);
        }
        // store part etag
        cachePartEtag(result);
        filePosition += thisPartSize;
    }
    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(s3bucketName, s3key, uploadId, partETags);
    CompleteMultipartUploadResult result = s3Client.completeMultipartUpload(compRequest);
    bytesUploaded = 0;
    Log.i(TAG, "upload complete for " + uploadId);
    clearProgressCache();
    return result.getLocation();
}
Also used : InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) PartETag(com.amazonaws.services.s3.model.PartETag) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) ProgressListener(com.amazonaws.services.s3.model.ProgressListener) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Aggregations

PartETag (com.amazonaws.services.s3.model.PartETag)4 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)3 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)3 ArrayList (java.util.ArrayList)3 AbortMultipartUploadRequest (com.amazonaws.services.s3.model.AbortMultipartUploadRequest)2 CompleteMultipartUploadResult (com.amazonaws.services.s3.model.CompleteMultipartUploadResult)2 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)2 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)2 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 InvalidArgumentException (com.amazonaws.services.cloudfront.model.InvalidArgumentException)1 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)1 CannedAccessControlList (com.amazonaws.services.s3.model.CannedAccessControlList)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 ProgressEvent (com.amazonaws.services.s3.model.ProgressEvent)1 ProgressListener (com.amazonaws.services.s3.model.ProgressListener)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Endpoint (org.apache.camel.Endpoint)1 Message (org.apache.camel.Message)1