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);
}
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());
}
}
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();
}
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);
}
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);
}
Aggregations