本文整理匯總了Java中com.amazonaws.services.s3.model.PutObjectRequest.setGeneralProgressListener方法的典型用法代碼示例。如果您正苦於以下問題:Java PutObjectRequest.setGeneralProgressListener方法的具體用法?Java PutObjectRequest.setGeneralProgressListener怎麽用?Java PutObjectRequest.setGeneralProgressListener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.amazonaws.services.s3.model.PutObjectRequest
的用法示例。
在下文中一共展示了PutObjectRequest.setGeneralProgressListener方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: putObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入方法依賴的package包/類
private void putObject() throws IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing regular upload for bucket '{}' key '{}'", bucket,
key);
}
final ObjectMetadata om = createDefaultMetadata();
om.setContentLength(buffer.size());
final PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key,
new ByteArrayInputStream(buffer.toByteArray()), om);
putObjectRequest.setCannedAcl(cannedACL);
putObjectRequest.setGeneralProgressListener(progressListener);
ListenableFuture<PutObjectResult> putObjectResult =
executorService.submit(new Callable<PutObjectResult>() {
@Override
public PutObjectResult call() throws Exception {
return client.putObject(putObjectRequest);
}
});
//wait for completion
try {
putObjectResult.get();
} catch (InterruptedException ie) {
LOG.warn("Interrupted object upload:" + ie, ie);
Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
throw new IOException("Regular upload failed", ee.getCause());
}
}
示例2: uploadTextToS3Bucket
import com.amazonaws.services.s3.model.PutObjectRequest; //導入方法依賴的package包/類
public void uploadTextToS3Bucket(String bucketName, String key, String content, String storageClass) throws TranscodeException{
ObjectMetadata objectMetaData = new ObjectMetadata();
byte[] bytes = content.getBytes();
objectMetaData.setContentLength(bytes.length);
objectMetaData.setContentType("text/html; charset=utf-8");
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetaData);
if (storageClass.equalsIgnoreCase(StorageClass.ReducedRedundancy.name())) {
putObjectRequest.setStorageClass(StorageClass.ReducedRedundancy);
}
final long fileSizeInBytes = bytes.length;
putObjectRequest.setGeneralProgressListener(new ProgressListener() {
private long bytesTransferred = 0;
private int currentPercentage = 0;
@Override
public void progressChanged(ProgressEvent progressEvent) {
bytesTransferred += progressEvent.getBytesTransferred();
int percentTransferred = (int) (bytesTransferred * 100 / fileSizeInBytes);
if (percentTransferred%10 == 0 && percentTransferred != currentPercentage) {
logger.info("Transferred {}% of {} bytes.", percentTransferred, fileSizeInBytes);
currentPercentage = percentTransferred;
}
}
});
Upload upload = tm.upload(putObjectRequest);
if (upload!=null) {
upload.waitForCompletion();
} else {
logger.error("Did not get upload detail from S3 for asset with key " + key);
throw new TranscodeException("Did not get upload detail from S3 for asset with key " + key);
}
} catch (AmazonClientException | InterruptedException e) {
throw new TranscodeException(e.getMessage());
}
}