Search in sources :

Example 11 with Bucket

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

the class OSSBucketOperation method listBuckets.

/**
 * List all my buckets.
 */
public List<Bucket> listBuckets() throws OSSException, ClientException {
    BucketList bucketList = listBuckets(new ListBucketsRequest(null, null, null));
    List<Bucket> buckets = bucketList.getBucketList();
    while (bucketList.isTruncated()) {
        bucketList = listBuckets(new ListBucketsRequest(null, bucketList.getNextMarker(), null));
        buckets.addAll(bucketList.getBucketList());
    }
    return buckets;
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) BucketList(com.aliyun.oss.model.BucketList)

Example 12 with Bucket

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

the class OSSResponseParserTest method testParseListBucket.

@Test
public void testParseListBucket() throws Exception {
    String filename = "listBucket.xml";
    InputStream in = getInputStream(filename);
    BucketList bucketList = ResponseParsers.parseListBucket(in);
    assertEquals(null, bucketList.getPrefix());
    assertEquals(null, bucketList.getMarker());
    assertEquals(null, bucketList.getMaxKeys());
    assertEquals(false, bucketList.isTruncated());
    assertEquals(null, bucketList.getNextMarker());
    List<Bucket> buckets = bucketList.getBucketList();
    Bucket bucket1 = buckets.get(0);
    Assert.assertEquals("51744", bucket1.getOwner().getId());
    Assert.assertEquals("51744", bucket1.getOwner().getDisplayName());
    Assert.assertEquals("pacjux7y1b86pmtu7g8d6b7z-test-bucket", bucket1.getName());
    Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-09T01:49:38.000Z"), bucket1.getCreationDate());
    Bucket bucket2 = buckets.get(1);
    Assert.assertEquals("51744", bucket2.getOwner().getId());
    Assert.assertEquals("51744", bucket2.getOwner().getDisplayName());
    Assert.assertEquals("ganshumantest", bucket2.getName());
    Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-09T06:38:47.000Z"), bucket2.getCreationDate());
    in.close();
    filename = "listBucketTruncated.xml";
    in = getInputStream(filename);
    bucketList = ResponseParsers.parseListBucket(in);
    assertEquals("asdasdasdasd", bucketList.getPrefix());
    assertEquals("asdasdasd", bucketList.getMarker());
    assertEquals(Integer.valueOf(1), bucketList.getMaxKeys());
    assertEquals(true, bucketList.isTruncated());
    assertEquals("asdasdasdasdasd", bucketList.getNextMarker());
    buckets = bucketList.getBucketList();
    bucket1 = buckets.get(0);
    Assert.assertEquals("51744", bucket1.getOwner().getId());
    Assert.assertEquals("51744", bucket1.getOwner().getDisplayName());
    Assert.assertEquals("asdasdasdasd", bucket1.getName());
    Assert.assertEquals("osslocation", bucket1.getLocation());
    Assert.assertEquals(DateUtil.parseIso8601Date("2014-05-15T11:18:32.000Z"), bucket1.getCreationDate());
    in.close();
}
Also used : Bucket(com.aliyun.oss.model.Bucket) InputStream(java.io.InputStream) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 13 with Bucket

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

the class BucketListTest method testBucketList.

@Test
public void testBucketList() {
    BucketList bucketList = new BucketList();
    bucketList.setPrefix("prefix");
    bucketList.setMarker("marker");
    bucketList.setMaxKeys(Integer.valueOf(6));
    bucketList.setTruncated(true);
    bucketList.setNextMarker("nextMarker");
    List<Bucket> buckets = new ArrayList<Bucket>();
    Bucket bucket = new Bucket();
    bucket.setName("name");
    bucket.setLocation("osslocation");
    buckets.add(bucket);
    bucketList.setBucketList(buckets);
    assertEquals("prefix", bucketList.getPrefix());
    assertEquals("marker", bucketList.getMarker());
    assertEquals(6, bucketList.getMaxKeys().intValue());
    assertEquals("nextMarker", bucketList.getNextMarker());
    assertEquals(true, bucketList.isTruncated());
    assertEquals(1, bucketList.getBucketList().size());
    buckets = bucketList.getBucketList();
    bucket = buckets.get(0);
    assertEquals("name", bucket.getName());
    assertEquals("osslocation", bucket.getLocation());
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 14 with Bucket

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

the class ResponseParsers method parseListBucket.

/**
 * Unmarshall list bucket response body to bucket list.
 */
@SuppressWarnings("unchecked")
public static BucketList parseListBucket(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        BucketList bucketList = new BucketList();
        if (root.getChild("Prefix") != null) {
            bucketList.setPrefix(root.getChildText("Prefix"));
        }
        if (root.getChild("Marker") != null) {
            bucketList.setMarker(root.getChildText("Marker"));
        }
        if (root.getChild("MaxKeys") != null) {
            String value = root.getChildText("MaxKeys");
            bucketList.setMaxKeys(isNullOrEmpty(value) ? null : Integer.valueOf(value));
        }
        if (root.getChild("IsTruncated") != null) {
            String value = root.getChildText("IsTruncated");
            bucketList.setTruncated(isNullOrEmpty(value) ? false : Boolean.valueOf(value));
        }
        if (root.getChild("NextMarker") != null) {
            bucketList.setNextMarker(root.getChildText("NextMarker"));
        }
        Element ownerElem = root.getChild("Owner");
        String id = ownerElem.getChildText("ID");
        String displayName = ownerElem.getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        List<Bucket> buckets = new ArrayList<Bucket>();
        if (root.getChild("Buckets") != null) {
            List<Element> bucketElems = root.getChild("Buckets").getChildren("Bucket");
            for (Element e : bucketElems) {
                Bucket bucket = new Bucket();
                bucket.setOwner(owner);
                bucket.setName(e.getChildText("Name"));
                bucket.setLocation(e.getChildText("Location"));
                bucket.setCreationDate(DateUtil.parseIso8601Date(e.getChildText("CreationDate")));
                if (e.getChild("StorageClass") != null) {
                    bucket.setStorageClass(StorageClass.parse(e.getChildText("StorageClass")));
                }
                bucket.setExtranetEndpoint(e.getChildText("ExtranetEndpoint"));
                bucket.setIntranetEndpoint(e.getChildText("IntranetEndpoint"));
                buckets.add(bucket);
            }
        }
        bucketList.setBucketList(buckets);
        return bucketList;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Owner(com.aliyun.oss.model.Owner) Bucket(com.aliyun.oss.model.Bucket) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 15 with Bucket

use of com.aliyun.oss.model.Bucket 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

Bucket (com.aliyun.oss.model.Bucket)15 BucketList (com.aliyun.oss.model.BucketList)6 OSSException (com.aliyun.oss.OSSException)5 ArrayList (java.util.ArrayList)5 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)4 Test (org.junit.Test)4 OSSClient (com.aliyun.oss.OSSClient)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)2 Owner (com.aliyun.oss.model.Owner)2 ParseException (java.text.ParseException)2 Element (org.jdom.Element)2 JDOMParseException (org.jdom.input.JDOMParseException)2 Ignore (org.junit.Ignore)2 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 BucketInfo (com.aliyun.oss.model.BucketInfo)1