本文整理汇总了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());
}
}