Search in sources :

Example 11 with OSSClientBuilder

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

the class ConcurrentGetObjectSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        /*
             * Upload an object to your bucket
             */
        System.out.println("Uploading a new object to OSS from a file\n");
        client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        /*
             * Get size of the object and pre-create a random access file to hold object data
             */
        ObjectMetadata metadata = client.getObjectMetadata(bucketName, key);
        long objectSize = metadata.getContentLength();
        RandomAccessFile raf = new RandomAccessFile(localFilePath, "rw");
        raf.setLength(objectSize);
        raf.close();
        /*
             * Calculate how many blocks to be divided
             */
        // 5MB
        final long blockSize = 5 * 1024 * 1024L;
        int blockCount = (int) (objectSize / blockSize);
        if (objectSize % blockSize != 0) {
            blockCount++;
        }
        System.out.println("Total blocks count " + blockCount + "\n");
        /*
             * Download the object concurrently
             */
        System.out.println("Start to download " + key + "\n");
        for (int i = 0; i < blockCount; i++) {
            long startPos = i * blockSize;
            long endPos = (i + 1 == blockCount) ? objectSize : (i + 1) * blockSize;
            executorService.execute(new BlockFetcher(startPos, endPos, i + 1));
        }
        /*
             * Waiting for all blocks finished
             */
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            try {
                executorService.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /*
             * Verify whether all blocks are finished
             */
        if (completedBlocks.intValue() != blockCount) {
            throw new IllegalStateException("Download fails due to some blocks are not finished yet");
        } else {
            System.out.println("Succeed to download object " + 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());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        if (client != null) {
            client.shutdown();
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 12 with OSSClientBuilder

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

the class GetProgressSample method main.

public static void main(String[] args) {
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    String accessKeyId = "<accessKeyId>";
    String accessKeySecret = "<accessKeySecret>";
    String bucketName = "<bucketName>";
    String key = "object-get-progress-sample";
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        File fh = createSampleFile();
        // 带进度条的上传
        client.putObject(new PutObjectRequest(bucketName, key, fh).<PutObjectRequest>withProgressListener(new PutObjectProgressListener()));
        // 带进度条的下载
        client.getObject(new GetObjectRequest(bucketName, key).<GetObjectRequest>withProgressListener(new GetObjectProgressListener()), fh);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : File(java.io.File) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) IOException(java.io.IOException)

Example 13 with OSSClientBuilder

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

the class DownloadSample method main.

public static void main(String[] args) throws IOException {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
        // Sets the local file to download to
        downloadFileRequest.setDownloadFile(downloadFile);
        // Sets the concurrent task thread count 5. By default it's 1.
        downloadFileRequest.setTaskNum(5);
        // Sets the part size, by default it's 100K.
        downloadFileRequest.setPartSize(1024 * 1024 * 1);
        // Enable checkpoint. By default it's false.
        downloadFileRequest.setEnableCheckpoint(true);
        DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);
        ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
        System.out.println(objectMetadata.getETag());
        System.out.println(objectMetadata.getLastModified());
        System.out.println(objectMetadata.getUserMetadata().get("meta"));
    } 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 : DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) OSSException(com.aliyun.oss.OSSException) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 14 with OSSClientBuilder

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

the class ListObjectsSample method main.

public static void main(String[] args) throws IOException {
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        final String content = "Hello OSS";
        final String keyPrefix = "MyObjectKey";
        if (!client.doesBucketExist(bucketName)) {
            client.createBucket(bucketName);
        }
        // Prepare the environment---inserting 100 test files.
        List<String> keys = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            String key = keyPrefix + i;
            InputStream instream = new ByteArrayInputStream(content.getBytes());
            client.putObject(bucketName, key, instream);
            keys.add(key);
        }
        System.out.println("Put " + keys.size() + " objects completed.");
        ObjectListing objectListing = null;
        String nextMarker = null;
        final int maxKeys = 30;
        List<OSSObjectSummary> sums = null;
        // use default parameter to list the files. By default up to 100 entries could be listed.
        System.out.println("Default paramters:");
        objectListing = client.listObjects(bucketName);
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Sets the max keys with 200 (the max value could be 1000).
        System.out.println("With max keys:");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(200));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets the object with specified prefix. By default it returns up to 100 entries.
        System.out.println("With prefix:");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets the object with specified marker. By default it returns up to 100 entries.
        System.out.println("With marker: ");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(keyPrefix + "11"));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets all object by paging. Each page will have up to 100 entries.
        System.out.println("List all objects:");
        nextMarker = null;
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Gets all object with specified prefix by paging. Each page will have up to 100 entries.
        System.out.println("List all objects after marker:");
        nextMarker = keyPrefix + "11";
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Gets all object with specified marker by paging. Each page will have up to 100 entries.
        System.out.println("List all objects with prefix:");
        nextMarker = null;
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix + "1").withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Clean up the environment----deleting the test files.
        System.out.println("Deleting all objects:");
        DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        for (String object : deletedObjects) {
            System.out.println("\t" + object);
        }
    } 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());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        client.shutdown();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) OSS(com.aliyun.oss.OSS) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) ListObjectsRequest(com.aliyun.oss.model.ListObjectsRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 15 with OSSClientBuilder

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

the class RequestTimeoutTest method testMultiOssClientIndependent.

/**
 * Multiple OSSClient instances test.
 * Each instance should work independently without being impacted by other instances.
 * So one timeout instance should not make other instances timeout.
 */
@Test
public void testMultiOssClientIndependent() throws Exception {
    String key = "test-multi-client-independent";
    try {
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setRequestTimeout(1);
        config.setRequestTimeoutEnabled(true);
        config.setMaxConnections(1);
        OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                threads[i] = new TimeoutOperationThread(client, key + i);
            } else {
                threads[i] = new OperationThread(key + i);
            }
        }
        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
        for (int i = 0; i < 10; i++) {
            threads[i].join();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)48 OSS (com.aliyun.oss.OSS)39 ByteArrayInputStream (java.io.ByteArrayInputStream)23 Test (org.junit.Test)22 ClientException (com.aliyun.oss.ClientException)19 OSSException (com.aliyun.oss.OSSException)19 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)13 OSSObject (com.aliyun.oss.model.OSSObject)10 InputStream (java.io.InputStream)10 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)9 Ignore (org.junit.Ignore)8 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)7 ArrayList (java.util.ArrayList)7 OSSClient (com.aliyun.oss.OSSClient)5 Credentials (com.aliyun.oss.common.auth.Credentials)5 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 BucketInfo (com.aliyun.oss.model.BucketInfo)5 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)5 File (java.io.File)5 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)4