com.aliyun.android.oss.task.UploadPartTask.java Source code

Java tutorial

Introduction

Here is the source code for com.aliyun.android.oss.task.UploadPartTask.java

Source

/**
 * Copyright (c) 2012 The Wiseserc. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package com.aliyun.android.oss.task;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.InputStreamEntity;

import com.aliyun.android.oss.OSSErrorCode;
import com.aliyun.android.oss.OSSException;
import com.aliyun.android.oss.http.HttpMethod;
import com.aliyun.android.oss.http.OSSHttpTool;
import com.aliyun.android.oss.model.Part;
import com.aliyun.android.util.Helper;

/**
 * ??Multipart Upload
 * Task??Multipart Upload Init Task???UploadId
 * ?UploadId???Object??UploadId??(Part)??
 * Part??(part number, 1~10000)?part??
 * ?Part???Partpart?5MB??
 * Part??
 * 
 * @author Michael
 */
public class UploadPartTask extends Task {
    /**
     * Object??
     */
    private String objectKey;

    /**
     * ??
     */
    private Part part;

    /**
     * uploadId
     */
    private String uploadId;

    /**
     * 
     */
    public UploadPartTask(String bucketName, String objectKey, String uploadId, Part part) {
        super(HttpMethod.PUT, bucketName);
        this.objectKey = objectKey;
        this.uploadId = uploadId;
        this.part = part;
        httpTool.setPartNumber(part.getPartNumber());
        httpTool.setUploadId(uploadId);
    }

    /**
     * ???
     */
    @Override
    protected void checkArguments() {
        if (Helper.isEmptyString(bucketName) || Helper.isEmptyString(objectKey)) {
            throw new IllegalArgumentException("bucketName or objectKey not set");
        }
        if (Helper.isEmptyString(uploadId)) {
            throw new IllegalArgumentException("uploadId not set");
        }
        if (part.getPartNumber() == null || part.getPartNumber() < 1 || part.getPartNumber() > 10000) {
            throw new IllegalArgumentException("partNumber should be 1~10000");
        }
        if (null == part.getStream()) {
            throw new IllegalArgumentException("this part doesn't contain data");
        }
    }

    /**
     * HttpPut
     */
    protected HttpUriRequest generateHttpRequest() {
        String requestUri = this.getOSSEndPoint()
                + httpTool.generateCanonicalizedResource("/" + OSSHttpTool.encodeUri(objectKey));
        HttpPut httpPut = new HttpPut(requestUri);

        String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey);
        String dateStr = Helper.getGMTDate();
        String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "", "",
                dateStr, "", resource);

        httpPut.setHeader(AUTHORIZATION, authorization);
        httpPut.setHeader(DATE, dateStr);

        httpPut.setHeader(UPLOAD_ID, httpTool.getUploadId());
        httpPut.setHeader(PART_NUMBER, Integer.toString(httpTool.getPartNumber()));

        InputStreamEntity entity = new InputStreamEntity(part.getStream(), part.getSize());
        httpPut.setEntity(entity);

        return httpPut;
    }

    /**
     * Part?MD5???MD5?
     * @return
     */
    public String getResult() {
        HttpResponse r = null;
        try {
            r = this.execute();
        } catch (OSSException osse) {
            throw osse;
        }
        Header etagHeader = r.getFirstHeader("ETag");
        if (etagHeader == null) {
            OSSException ossException = new OSSException("no ETag header returned from oss.");
            ossException.setErrorCode(OSSErrorCode.ETAG_HEADER_EMPTY);
            throw ossException;
        }
        String value = etagHeader.getValue();

        //"
        while (value.startsWith("\"")) {
            value = value.substring(1);
        }
        while (value.endsWith("\"")) {
            value = value.substring(0, value.length() - 1);
        }
        return value;
    }
}