Search in sources :

Example 1 with ProgressListener

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;
}
Also used : ArrayList(java.util.ArrayList) ProgressListener(com.aliyun.oss.event.ProgressListener) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProgressListener

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;
}
Also used : DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) ProgressListener(com.aliyun.oss.event.ProgressListener) LogUtils.logException(com.aliyun.oss.common.utils.LogUtils.logException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ProgressListener

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;
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) CRC64(com.aliyun.oss.common.utils.CRC64) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) RepeatableFileInputStream(com.aliyun.oss.common.comm.io.RepeatableFileInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) InputStream(java.io.InputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ProgressListener(com.aliyun.oss.event.ProgressListener) GetObjectResponseParser(com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 4 with ProgressListener

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;
    }
}
Also used : ProgressListener(com.aliyun.oss.event.ProgressListener) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) InputStream(java.io.InputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) IOException(java.io.IOException) ClientException(com.aliyun.oss.ClientException) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ProgressListener

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) LinkedHashMap(java.util.LinkedHashMap) UploadPartResult(com.aliyun.oss.model.UploadPartResult) ProgressListener(com.aliyun.oss.event.ProgressListener) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException)

Aggregations

ProgressListener (com.aliyun.oss.event.ProgressListener)11 InputStream (java.io.InputStream)7 ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)6 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)5 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 ExecutionException (java.util.concurrent.ExecutionException)4 CheckedInputStream (java.util.zip.CheckedInputStream)4 ClientException (com.aliyun.oss.ClientException)3 UploadPartResult (com.aliyun.oss.model.UploadPartResult)3 ArrayList (java.util.ArrayList)3 RepeatableFileInputStream (com.aliyun.oss.common.comm.io.RepeatableFileInputStream)2 CRC64 (com.aliyun.oss.common.utils.CRC64)2 LogUtils.logException (com.aliyun.oss.common.utils.LogUtils.logException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)1