Search in sources :

Example 6 with ListBucketsRequest

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

the class BucketListTest method testListBucketRequest.

@Test
public void testListBucketRequest() {
    ListBucketsRequest request = new ListBucketsRequest("prefix", "marker", Integer.valueOf(1000));
    assertEquals("prefix", request.getPrefix());
    assertEquals("marker", request.getMarker());
    assertEquals(Integer.valueOf(1000), request.getMaxKeys());
    request.setPrefix("prefix2");
    request.setMarker("marker2");
    request.setMaxKeys(Integer.valueOf(1));
    assertEquals("prefix2", request.getPrefix());
    assertEquals("marker2", request.getMarker());
    assertEquals(Integer.valueOf(1), request.getMaxKeys());
    try {
        request.setMaxKeys(Integer.valueOf(1001));
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        request.setMaxKeys(Integer.valueOf(-1));
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
}
Also used : ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) Test(org.junit.Test)

Example 7 with ListBucketsRequest

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

the class GetStartedSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    System.out.println("Getting Started with OSS SDK for Java\n");
    try {
        /*
             * Determine whether the bucket exists
             */
        if (!ossClient.doesBucketExist(bucketName)) {
            /*
                 * Create a new OSS bucket
                 */
            System.out.println("Creating bucket " + bucketName + "\n");
            ossClient.createBucket(bucketName);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }
        /*
             * List the buckets in your account
             */
        System.out.println("Listing buckets");
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        listBucketsRequest.setMaxKeys(500);
        for (Bucket bucket : ossClient.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();
        /*
             * Upload an object to your bucket
             */
        System.out.println("Uploading a new object to OSS from a file\n");
        ossClient.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        /*
             * Determine whether an object residents in your bucket
             */
        boolean exists = ossClient.doesObjectExist(bucketName, key);
        System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
        ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, key);
        System.out.println("ACL:" + objectAcl.getPermission().toString());
        /*
             * Download an object from your bucket
             */
        System.out.println("Downloading an object");
        OSSObject object = ossClient.getObject(bucketName, key);
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());
        /*
             * List objects in your bucket by prefix
             */
        System.out.println("Listing objects");
        ObjectListing objectListing = ossClient.listObjects(bucketName, "My");
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();
        /*
             * Delete an object
             */
        System.out.println("Deleting an object\n");
        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());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        ossClient.shutdown();
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Aggregations

ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)7 BucketList (com.aliyun.oss.model.BucketList)5 Bucket (com.aliyun.oss.model.Bucket)4 Test (org.junit.Test)4 OSSException (com.aliyun.oss.OSSException)2 ArrayList (java.util.ArrayList)2 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)1 OSSObject (com.aliyun.oss.model.OSSObject)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)1