Search in sources :

Example 1 with ProgressEvent

use of software.amazon.awssdk.services.s3.model.ProgressEvent in project aws-sdk-android by aws-amplify.

the class ProgressReportingInputStreamTest method testNotifiedOfAllBytesNoEventCompletedFired.

@Test
public void testNotifiedOfAllBytesNoEventCompletedFired() throws InterruptedException, IOException {
    // By default we should not recieve a event completed event
    final Map<Long, Integer> countMap = new HashMap<Long, Integer>();
    final ProgressListener listener = new ProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent) {
            synchronized (countMap) {
                if (countMap.size() >= 3) {
                    fail("Unexpected progress event found: " + progressEvent);
                }
                Integer curr = countMap.get(progressEvent.getBytesTransferred());
                if (curr == null) {
                    curr = 0;
                }
                countMap.put(progressEvent.getBytesTransferred(), ++curr);
                countMap.notifyAll();
            }
        }
    };
    ProgressReportingInputStream testStream = null;
    try {
        testStream = new ProgressReportingInputStream(is, listener);
        while (testStream.read() != -1) {
        }
    } finally {
        testStream.close();
    }
    assertTrue(countMap.size() == 2);
    assertEquals(countMap.get(new Long(1024)).intValue(), 1);
    assertEquals(countMap.get(new Long(8192)).intValue(), 2);
}
Also used : ProgressListener(com.amazonaws.services.s3.model.ProgressListener) HashMap(java.util.HashMap) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) Test(org.junit.Test)

Example 2 with ProgressEvent

use of software.amazon.awssdk.services.s3.model.ProgressEvent in project android-simpl3r by jgilfelt.

the class UploadService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    String filePath = intent.getStringExtra(ARG_FILE_PATH);
    File fileToUpload = new File(filePath);
    final String s3ObjectKey = md5(filePath);
    String s3BucketName = getString(R.string.s3_bucket);
    final String msg = "Uploading " + s3ObjectKey + "...";
    // create a new uploader for this file
    uploader = new Uploader(this, s3Client, s3BucketName, s3ObjectKey, fileToUpload);
    // listen for progress updates and broadcast/notify them appropriately
    uploader.setProgressListener(new UploadProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent, long bytesUploaded, int percentUploaded) {
            Notification notification = buildNotification(msg, percentUploaded);
            nm.notify(NOTIFY_ID_UPLOAD, notification);
            broadcastState(s3ObjectKey, percentUploaded, msg);
        }
    });
    // broadcast/notify that our upload is starting
    Notification notification = buildNotification(msg, 0);
    nm.notify(NOTIFY_ID_UPLOAD, notification);
    broadcastState(s3ObjectKey, 0, msg);
    try {
        // initiate the upload
        String s3Location = uploader.start();
        broadcastState(s3ObjectKey, -1, "File successfully uploaded to " + s3Location);
    } catch (UploadIterruptedException uie) {
        broadcastState(s3ObjectKey, -1, "User interrupted");
    } catch (Exception e) {
        e.printStackTrace();
        broadcastState(s3ObjectKey, -1, "Error: " + e.getMessage());
    }
}
Also used : UploadProgressListener(com.readystatesoftware.simpl3r.Uploader.UploadProgressListener) UploadIterruptedException(com.readystatesoftware.simpl3r.UploadIterruptedException) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) File(java.io.File) Uploader(com.readystatesoftware.simpl3r.Uploader) Notification(android.app.Notification) UploadIterruptedException(com.readystatesoftware.simpl3r.UploadIterruptedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with ProgressEvent

use of software.amazon.awssdk.services.s3.model.ProgressEvent 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)

Example 4 with ProgressEvent

use of software.amazon.awssdk.services.s3.model.ProgressEvent in project Synapse-Stack-Builder by Sage-Bionetworks.

the class ArtifactProcessing method createOrGetApplicationVersion.

/**
 * Create the application version if it does not already exist.
 * @param versionLabel
 * @param fileURl
 * @throws IOException
 */
public ApplicationVersionDescription createOrGetApplicationVersion(String appPrfix) throws IOException {
    String s3Path = config.getVersionPath(appPrfix);
    final String versionLabel = config.getVersionLabel(appPrfix);
    String fileURL = config.getArtifactoryUrl(appPrfix);
    // First determine if this version already exists
    log.debug(String.format("Creating version: %1$s using: %2$s ", versionLabel, fileURL));
    DescribeApplicationVersionsResult results = beanstalkClient.describeApplicationVersions(new DescribeApplicationVersionsRequest().withApplicationName(config.getElasticBeanstalkApplicationName()).withVersionLabels(versionLabel));
    if (results.getApplicationVersions().size() < 1) {
        log.debug(String.format("Version: %1$s does not already existing so it will be created...", versionLabel));
        // first download the file
        // Download the artifacts
        File temp = null;
        String key = s3Path + versionLabel;
        try {
            // First download the file from Artifactory
            temp = downloadFile(fileURL);
            // Now upload it to s3.
            final long start = System.currentTimeMillis();
            ;
            log.debug("Starting to upload file " + fileURL + " to S3...");
            PutObjectResult putResult = s3Client.putObject(new PutObjectRequest(config.getStackConfigS3BucketName(), key, temp).withProgressListener(new ProgressListener() {

                private volatile long lastUpdate = start;

                public void progressChanged(ProgressEvent progressEvent) {
                    // The progress data they give use never seems to change so we just show the elase time.
                    long now = System.currentTimeMillis();
                    long lastUpdateElapase = now - lastUpdate;
                    long totalElapse = now - start;
                    if (lastUpdateElapase > 2 * 1000) {
                        // Log the event
                        log.debug(String.format("Uploading %1$s to S3. Elapse time: %2$tM:%2$tS:%2$tL ", versionLabel, totalElapse));
                        lastUpdate = now;
                    }
                }
            }));
        } finally {
            if (temp != null) {
                temp.delete();
            }
        // Clean up the file
        }
        // The S3 Location for this file.
        S3Location location = new S3Location(config.getStackConfigS3BucketName(), key);
        // we need to create this version
        beanstalkClient.createApplicationVersion(new CreateApplicationVersionRequest().withApplicationName(config.getElasticBeanstalkApplicationName()).withAutoCreateApplication(false).withSourceBundle(location).withVersionLabel(versionLabel));
        // Describe the version
        results = beanstalkClient.describeApplicationVersions(new DescribeApplicationVersionsRequest().withApplicationName(config.getElasticBeanstalkApplicationName()).withVersionLabels(versionLabel));
    } else {
        log.debug(String.format("Version: %1$s already exists.", versionLabel));
    }
    return results.getApplicationVersions().get(0);
}
Also used : DescribeApplicationVersionsRequest(com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest) ProgressListener(com.amazonaws.services.s3.model.ProgressListener) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) DescribeApplicationVersionsResult(com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult) CreateApplicationVersionRequest(com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) File(java.io.File) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) S3Location(com.amazonaws.services.elasticbeanstalk.model.S3Location)

Example 5 with ProgressEvent

use of software.amazon.awssdk.services.s3.model.ProgressEvent in project aws-sdk-android by aws-amplify.

the class ProgressReportingInputStreamTest method testNotifiedOfAllByteWithEventCompletedFired.

@Test
public void testNotifiedOfAllByteWithEventCompletedFired() throws IOException {
    final Map<Long, Integer> countMap = new HashMap<Long, Integer>();
    final ProgressListener listener = new ProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent) {
            synchronized (countMap) {
                if (countMap.size() >= 3) {
                    fail("Unexpected progress event found: " + progressEvent);
                }
                if (progressEvent.getBytesTransferred() == 1024) {
                    assertTrue(progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE);
                }
                Integer curr = countMap.get(progressEvent.getBytesTransferred());
                if (curr == null) {
                    curr = 0;
                }
                countMap.put(progressEvent.getBytesTransferred(), ++curr);
                countMap.notifyAll();
            }
        }
    };
    ProgressReportingInputStream testStream = null;
    try {
        testStream = new ProgressReportingInputStream(is, listener);
        testStream.setFireCompletedEvent(true);
        while (testStream.read() != -1) {
        }
    } finally {
        testStream.close();
    }
    assertTrue(countMap.size() == 2);
    assertEquals(countMap.get(new Long(1024)).intValue(), 1);
    assertEquals(countMap.get(new Long(8192)).intValue(), 2);
}
Also used : ProgressListener(com.amazonaws.services.s3.model.ProgressListener) HashMap(java.util.HashMap) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) Test(org.junit.Test)

Aggregations

ProgressEvent (com.amazonaws.services.s3.model.ProgressEvent)10 ProgressListener (com.amazonaws.services.s3.model.ProgressListener)5 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 File (java.io.File)2 Notification (android.app.Notification)1 CreateApplicationVersionRequest (com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest)1 DescribeApplicationVersionsRequest (com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest)1 DescribeApplicationVersionsResult (com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult)1 S3Location (com.amazonaws.services.elasticbeanstalk.model.S3Location)1 AbortMultipartUploadRequest (com.amazonaws.services.s3.model.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.amazonaws.services.s3.model.CompleteMultipartUploadResult)1 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)1 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)1 PartETag (com.amazonaws.services.s3.model.PartETag)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)1 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)1 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)1