Search in sources :

Example 1 with UploadFileRequest

use of com.aliyun.oss.model.UploadFileRequest in project aliyun-oss-java-sdk by aliyun.

the class UploadSample method main.

public static void main(String[] args) throws IOException {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        // The local file to upload---it must exist.
        uploadFileRequest.setUploadFile(uploadFile);
        // Sets the concurrent upload task number to 5.
        uploadFileRequest.setTaskNum(5);
        // Sets the part size to 1MB.
        uploadFileRequest.setPartSize(1024 * 1024 * 1);
        // Enables the checkpoint file. By default it's off.
        uploadFileRequest.setEnableCheckpoint(true);
        UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
        CompleteMultipartUploadResult multipartUploadResult = uploadResult.getMultipartUploadResult();
        System.out.println(multipartUploadResult.getETag());
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) OSSException(com.aliyun.oss.OSSException) UploadFileResult(com.aliyun.oss.model.UploadFileResult) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 2 with UploadFileRequest

use of com.aliyun.oss.model.UploadFileRequest in project aliyun-oss-java-sdk by aliyun.

the class CRCSample method main.

public static void main(String[] args) throws IOException {
    String content = "Hello OSS, Hi OSS, OSS OK.";
    // CRC check is enabled by default for upload or download. To turn it off, use the commented code below.
    // ClientConfiguration config = new ClientConfiguration();
    // config.setCrcCheckEnabled(false);
    // OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        // putObject/uploadPart/uploadFile are automatically enabled with CRC.
        // However, appendObject needs to call AppendObjectRequest.setInitCRC to enable CRC.
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.deleteObject(bucketName, key);
        // First data appending
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
        // because it's first append, the previous data is empty and thus CRC is 0.
        appendObjectRequest.setInitCRC(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        // Second data appending
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        // the initCRC is the first's CRC.
        appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        ossClient.deleteObject(bucketName, key);
        // Upload with checkpoint, it supports CRC as well.
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        // Sets the upload file with a local file.
        uploadFileRequest.setUploadFile(uploadFile);
        // Sets the concurrent task number to 5 (default is 1).
        uploadFileRequest.setTaskNum(5);
        // Sets the part size to 1MB (default is 100K)
        uploadFileRequest.setPartSize(1024 * 1024 * 1);
        // Enable checkpoint (by default is off for checkpoint enabled upload)
        uploadFileRequest.setEnableCheckpoint(true);
        ossClient.uploadFile(uploadFileRequest);
        // Download with CRC. Note that range download does not support CRC.
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        Assert.assertNull(ossObject.getClientCRC());
        Assert.assertNotNull(ossObject.getServerCRC());
        InputStream stream = ossObject.getObjectContent();
        while (stream.read() != -1) {
        }
        stream.close();
        // Check if CRC is consistent.
        OSSUtils.checkChecksum(IOUtils.getCRCValue(stream), ossObject.getServerCRC(), ossObject.getRequestId());
        ossClient.deleteObject(bucketName, key);
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } catch (InconsistentException ie) {
        System.out.println("Caught an OSSException");
        System.out.println("Request ID:      " + ie.getRequestId());
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) InconsistentException(com.aliyun.oss.InconsistentException) OSS(com.aliyun.oss.OSS) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 3 with UploadFileRequest

use of com.aliyun.oss.model.UploadFileRequest in project aliyun-oss-java-sdk by aliyun.

the class RequestTimeoutTest method testObjectOperationsNormal.

/**
 * Testing normal request.
 */
@Test
public void testObjectOperationsNormal() throws Exception {
    String key = "test-object-operation-normal";
    try {
        // get
        ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
        // put
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        // delete
        ossClient.deleteObject(bucketName, key);
        // upload
        File file = createSampleFile(key, 1024 * 500);
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        uploadFileRequest.setUploadFile(file.getAbsolutePath());
        uploadFileRequest.setPartSize(1024 * 100);
        uploadFileRequest.setTaskNum(10);
        uploadFileRequest.setEnableCheckpoint(true);
        ossClient.uploadFile(uploadFileRequest);
        // download
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
        downloadFileRequest.setDownloadFile(file.getAbsolutePath());
        downloadFileRequest.setTaskNum(10);
        downloadFileRequest.setEnableCheckpoint(true);
        ossClient.downloadFile(downloadFileRequest);
        ossClient.deleteObject(bucketName, key);
        file.delete();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } catch (Throwable t) {
        Assert.fail(t.getMessage());
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) OSSObject(com.aliyun.oss.model.OSSObject) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) File(java.io.File) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 4 with UploadFileRequest

use of com.aliyun.oss.model.UploadFileRequest in project aliyun-oss-java-sdk by aliyun.

the class UploadFileTest method testUploadFileWithCheckpointFile.

@Test
public void testUploadFileWithCheckpointFile() {
    final String key = "obj-upload-file-cpf";
    try {
        File file = createSampleFile(key, 1024 * 500);
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        uploadFileRequest.setUploadFile(file.getAbsolutePath());
        uploadFileRequest.setTaskNum(10);
        uploadFileRequest.setEnableCheckpoint(true);
        uploadFileRequest.setCheckpointFile("BingWallpaper.ucp");
        UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
        ObjectListing objects = ossClient.listObjects(bucketName, key);
        Assert.assertEquals(objects.getObjectSummaries().size(), 1);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getKey(), key);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getSize(), file.length());
        Assert.assertEquals(objects.getRequestId().length(), REQUEST_ID_LEN);
        ObjectMetadata meta = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(meta.getContentLength(), file.length());
        Assert.assertEquals(meta.getContentType(), "text/plain");
        File fileNew = new File(key + "-new.txt");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ossClient.getObject(getObjectRequest, fileNew);
        Assert.assertEquals(file.length(), fileNew.length());
        ossClient.deleteObject(bucketName, key);
        fileNew.delete();
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
        ossClient.deleteBucket(bucketName);
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) ObjectListing(com.aliyun.oss.model.ObjectListing) UploadFileResult(com.aliyun.oss.model.UploadFileResult) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) Test(org.junit.Test)

Example 5 with UploadFileRequest

use of com.aliyun.oss.model.UploadFileRequest in project aliyun-oss-java-sdk by aliyun.

the class UploadFileTest method testUploadFileWithCheckpoint.

@Test
public void testUploadFileWithCheckpoint() {
    final String key = "obj-upload-file-cp";
    try {
        File file = createSampleFile(key, 1024 * 500);
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        uploadFileRequest.setUploadFile(file.getAbsolutePath());
        uploadFileRequest.setTaskNum(10);
        uploadFileRequest.setEnableCheckpoint(true);
        UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
        ObjectListing objects = ossClient.listObjects(bucketName, key);
        Assert.assertEquals(objects.getObjectSummaries().size(), 1);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getKey(), key);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getSize(), file.length());
        Assert.assertEquals(objects.getRequestId().length(), REQUEST_ID_LEN);
        ObjectMetadata meta = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(meta.getContentLength(), file.length());
        Assert.assertEquals(meta.getContentType(), "text/plain");
        File fileNew = new File(key + "-new.txt");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ossClient.getObject(getObjectRequest, fileNew);
        Assert.assertEquals(file.length(), fileNew.length());
        ossClient.deleteObject(bucketName, key);
        fileNew.delete();
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
        ossClient.deleteBucket(bucketName);
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) ObjectListing(com.aliyun.oss.model.ObjectListing) UploadFileResult(com.aliyun.oss.model.UploadFileResult) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) Test(org.junit.Test)

Aggregations

UploadFileRequest (com.aliyun.oss.model.UploadFileRequest)11 Test (org.junit.Test)9 UploadFileResult (com.aliyun.oss.model.UploadFileResult)8 File (java.io.File)8 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)6 DownloadFileRequest (com.aliyun.oss.model.DownloadFileRequest)5 OSSException (com.aliyun.oss.OSSException)4 ClientException (com.aliyun.oss.ClientException)3 DownloadFileResult (com.aliyun.oss.model.DownloadFileResult)3 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSObject (com.aliyun.oss.model.OSSObject)2 InconsistentException (com.aliyun.oss.InconsistentException)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 BucketStat (com.aliyun.oss.model.BucketStat)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1