use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSDownloadOperation method download.
private DownloadResult download(DownloadCheckPoint downloadCheckPoint, DownloadFileRequest downloadFileRequest) throws Throwable {
DownloadResult downloadResult = new DownloadResult();
ArrayList<PartResult> taskResults = new ArrayList<PartResult>();
ExecutorService service = Executors.newFixedThreadPool(downloadFileRequest.getTaskNum());
ArrayList<Future<PartResult>> futures = new ArrayList<Future<PartResult>>();
List<Task> tasks = new ArrayList<Task>();
ProgressListener listener = downloadFileRequest.getProgressListener();
// Compute the size of data pending download.
long contentLength = 0;
for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) {
if (!downloadCheckPoint.downloadParts.get(i).isCompleted) {
long partSize = downloadCheckPoint.downloadParts.get(i).end - downloadCheckPoint.downloadParts.get(i).start + 1;
contentLength += partSize;
}
}
ProgressPublisher.publishResponseContentLength(listener, contentLength);
downloadFileRequest.setProgressListener(null);
// Concurrently download parts.
for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) {
if (!downloadCheckPoint.downloadParts.get(i).isCompleted) {
Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, objectOperation, listener);
futures.add(service.submit(task));
tasks.add(task);
} else {
taskResults.add(new PartResult(i + 1, downloadCheckPoint.downloadParts.get(i).start, downloadCheckPoint.downloadParts.get(i).end));
}
}
service.shutdown();
// Waiting for all parts download,
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
for (Future<PartResult> future : futures) {
try {
PartResult tr = future.get();
taskResults.add(tr);
} catch (ExecutionException e) {
downloadFileRequest.setProgressListener(listener);
throw e.getCause();
}
}
// Sorts the download result by the part number.
Collections.sort(taskResults, new Comparator<PartResult>() {
@Override
public int compare(PartResult p1, PartResult p2) {
return p1.getNumber() - p2.getNumber();
}
});
// sets the return value.
downloadResult.setPartResults(taskResults);
if (tasks.size() > 0) {
downloadResult.setObjectMetadata(tasks.get(0).GetobjectMetadata());
}
downloadFileRequest.setProgressListener(listener);
return downloadResult;
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSDownloadOperation method downloadFileWithCheckpoint.
private DownloadFileResult downloadFileWithCheckpoint(DownloadFileRequest downloadFileRequest) throws Throwable {
DownloadFileResult downloadFileResult = new DownloadFileResult();
DownloadCheckPoint downloadCheckPoint = new DownloadCheckPoint();
// checkpoint file.
if (downloadFileRequest.isEnableCheckpoint()) {
// re-download again.
try {
downloadCheckPoint.load(downloadFileRequest.getCheckpointFile());
} catch (Exception e) {
remove(downloadFileRequest.getCheckpointFile());
}
// The download checkpoint is corrupted, download again.
if (!downloadCheckPoint.isValid(objectOperation)) {
prepare(downloadCheckPoint, downloadFileRequest);
remove(downloadFileRequest.getCheckpointFile());
}
} else {
// The checkpoint is not enabled, download the file again.
prepare(downloadCheckPoint, downloadFileRequest);
}
// Progress listen starts tracking the progress.
ProgressListener listener = downloadFileRequest.getProgressListener();
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
// Concurrently download parts.
DownloadResult downloadResult = download(downloadCheckPoint, downloadFileRequest);
for (PartResult partResult : downloadResult.getPartResults()) {
if (partResult.isFailed()) {
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
throw partResult.getException();
}
}
// Publish the complete status.
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
// rename the temp file.
renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile());
// successful download.
if (downloadFileRequest.isEnableCheckpoint()) {
remove(downloadFileRequest.getCheckpointFile());
}
downloadFileResult.setObjectMetadata(downloadResult.getObjectMetadata());
return downloadFileResult;
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperation method getObject.
/**
* Pull an object from oss.
*/
public OSSObject getObject(GetObjectRequest getObjectRequest) throws OSSException, ClientException {
assertParameterNotNull(getObjectRequest, "getObjectRequest");
String bucketName = null;
String key = null;
RequestMessage request = null;
if (!getObjectRequest.isUseUrlSignature()) {
assertParameterNotNull(getObjectRequest, "getObjectRequest");
bucketName = getObjectRequest.getBucketName();
key = getObjectRequest.getKey();
assertParameterNotNull(bucketName, "bucketName");
assertParameterNotNull(key, "key");
ensureBucketNameValid(bucketName);
ensureObjectKeyValid(key);
Map<String, String> headers = new HashMap<String, String>();
populateGetObjectRequestHeaders(getObjectRequest, headers);
Map<String, String> params = new HashMap<String, String>();
populateResponseHeaderParameters(params, getObjectRequest.getResponseHeaders());
String process = getObjectRequest.getProcess();
if (process != null) {
params.put(RequestParameters.SUBRESOURCE_PROCESS, process);
}
request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setOriginalRequest(getObjectRequest).build();
} else {
request = new RequestMessage(getObjectRequest, bucketName, key);
request.setMethod(HttpMethod.GET);
request.setAbsoluteUrl(getObjectRequest.getAbsoluteUri());
request.setUseUrlSignature(true);
request.setHeaders(getObjectRequest.getHeaders());
}
final ProgressListener listener = getObjectRequest.getProgressListener();
OSSObject ossObject = null;
try {
publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
ossObject = doOperation(request, new GetObjectResponseParser(bucketName, key), bucketName, key, true);
InputStream instream = ossObject.getObjectContent();
ProgressInputStream progressInputStream = new ProgressInputStream(instream, listener) {
@Override
protected void onEOF() {
publishProgress(getListener(), ProgressEventType.TRANSFER_COMPLETED_EVENT);
}
};
CRC64 crc = new CRC64();
CheckedInputStream checkedInputstream = new CheckedInputStream(progressInputStream, crc);
ossObject.setObjectContent(checkedInputstream);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
throw e;
}
return ossObject;
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSUdfOperation method uploadUdfImage.
/**
* UDF Image
*/
public void uploadUdfImage(UploadUdfImageRequest uploadUdfImageRequest) throws OSSException, ClientException {
assertParameterNotNull(uploadUdfImageRequest, "uploadUdfImageRequest");
assertParameterNotNull(uploadUdfImageRequest.getUdfImage(), "udfImage");
assertParameterNotNull(uploadUdfImageRequest.getName(), "udfImage");
ensureBucketNameValid(uploadUdfImageRequest.getName());
InputStream repeatableInputStream = null;
try {
repeatableInputStream = newRepeatableInputStream(uploadUdfImageRequest.getUdfImage());
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
Map<String, String> headers = new HashMap<String, String>();
Map<String, String> params = new LinkedHashMap<String, String>();
params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE, null);
params.put(RequestParameters.SUBRESOURCE_UDF_NAME, uploadUdfImageRequest.getName());
if (uploadUdfImageRequest.getUdfImageDesc() != null) {
params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE_DESC, uploadUdfImageRequest.getUdfImageDesc());
}
RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.POST).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, 0)).setOriginalRequest(uploadUdfImageRequest).build();
ProgressListener listener = uploadUdfImageRequest.getProgressListener();
try {
publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
doOperation(httpRequest, emptyResponseParser, null, null, true);
publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
throw e;
}
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSMultipartOperation method uploadPart.
/**
* Upload part.
*/
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws OSSException, ClientException {
assertParameterNotNull(uploadPartRequest, "uploadPartRequest");
String key = uploadPartRequest.getKey();
String bucketName = uploadPartRequest.getBucketName();
String uploadId = uploadPartRequest.getUploadId();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
assertParameterNotNull(key, "key");
ensureObjectKeyValid(key);
assertStringNotNullOrEmpty(uploadId, "uploadId");
if (uploadPartRequest.getInputStream() == null) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetContentStream"));
}
InputStream repeatableInputStream = null;
try {
repeatableInputStream = newRepeatableInputStream(uploadPartRequest.buildPartialStream());
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
int partNumber = uploadPartRequest.getPartNumber();
if (!checkParamRange(partNumber, 0, false, MAX_PART_NUMBER, true)) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("PartNumberOutOfRange"));
}
Map<String, String> headers = new HashMap<String, String>();
populateUploadPartOptionalHeaders(uploadPartRequest, headers);
// Use a LinkedHashMap to preserve the insertion order.
Map<String, String> params = new LinkedHashMap<String, String>();
params.put(PART_NUMBER, Integer.toString(partNumber));
params.put(UPLOAD_ID, uploadId);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(key).setParameters(params).setHeaders(headers).setInputStream(repeatableInputStream).setInputSize(uploadPartRequest.getPartSize()).setUseChunkEncoding(uploadPartRequest.isUseChunkEncoding()).setOriginalRequest(uploadPartRequest).build();
final ProgressListener listener = uploadPartRequest.getProgressListener();
ResponseMessage response = null;
try {
publishProgress(listener, ProgressEventType.TRANSFER_PART_STARTED_EVENT);
response = doOperation(request, emptyResponseParser, bucketName, key);
publishProgress(listener, ProgressEventType.TRANSFER_PART_COMPLETED_EVENT);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
throw e;
}
UploadPartResult result = new UploadPartResult();
result.setPartNumber(partNumber);
result.setETag(trimQuotes(response.getHeaders().get(OSSHeaders.ETAG)));
result.setRequestId(response.getRequestId());
result.setPartSize(uploadPartRequest.getPartSize());
ResponseParsers.setCRC(result, response);
if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
}
return result;
}
Aggregations