Search in sources :

Example 1 with BucketInfo

use of com.aliyun.oss.model.BucketInfo in project FredaBlog by yangjinlong86.

the class OssUtils method test.

public static void test() {
    String firstKey = "jason_test_object";
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    try {
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("您已经创建Bucket:" + bucketName + "。");
        } else {
            System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
            // 创建Bucket。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            ossClient.createBucket(bucketName);
        }
        // 查看Bucket信息。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        System.out.println("Bucket " + bucketName + "的信息如下:");
        System.out.println("\t数据中心:" + info.getBucket().getLocation());
        System.out.println("\t创建时间:" + info.getBucket().getCreationDate());
        System.out.println("\t用户标志:" + info.getBucket().getOwner());
        // 把字符串存入OSS,Object的名称为firstKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        InputStream is = new ByteArrayInputStream("Hello OSS".getBytes());
        ossClient.putObject(bucketName, firstKey, is);
        System.out.println("Object:" + firstKey + "存入OSS成功。");
        // 下载文件。详细请参看“SDK手册 > Java-SDK > 下载文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/download_object.html?spm=5176.docoss/sdk/java-sdk/manage_object
        OSSObject ossObject = ossClient.getObject(bucketName, firstKey);
        InputStream inputStream = ossObject.getObjectContent();
        StringBuilder objectContent = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            objectContent.append(line);
        }
        inputStream.close();
        System.out.println("Object:" + firstKey + "的内容是:" + objectContent);
        // 文件存储入OSS,Object的名称为fileKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        String fileKey = "README.md";
        ossClient.putObject(bucketName, fileKey, new File("README.md"));
        System.out.println("Object:" + fileKey + "存入OSS成功。");
        // 查看Bucket中的Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries();
        System.out.println("您有以下Object:");
        for (OSSObjectSummary object : objectSummary) {
            System.out.println("\t" + object.getKey());
        }
    // 删除Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
    // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
    } catch (OSSException oe) {
        oe.printStackTrace();
    } catch (ClientException ce) {
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
    logger.info("Completed");
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) BucketInfo(com.aliyun.oss.model.BucketInfo) ClientException(com.aliyun.oss.ClientException)

Example 2 with BucketInfo

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

the class ResponseParsers method parseGetBucketInfo.

/**
 * Unmarshall get bucket info response body to bucket info.
 */
public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        Element bucketElem = root.getChild("Bucket");
        BucketInfo bucketInfo = new BucketInfo();
        // owner
        Bucket bucket = new Bucket();
        String id = bucketElem.getChild("Owner").getChildText("ID");
        String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        bucket.setOwner(owner);
        // bucket
        bucket.setName(bucketElem.getChildText("Name"));
        bucket.setLocation(bucketElem.getChildText("Location"));
        bucket.setExtranetEndpoint(bucketElem.getChildText("ExtranetEndpoint"));
        bucket.setIntranetEndpoint(bucketElem.getChildText("IntranetEndpoint"));
        bucket.setCreationDate(DateUtil.parseIso8601Date(bucketElem.getChildText("CreationDate")));
        if (bucketElem.getChild("StorageClass") != null) {
            bucket.setStorageClass(StorageClass.parse(bucketElem.getChildText("StorageClass")));
        }
        bucketInfo.setBucket(bucket);
        // acl
        String aclString = bucketElem.getChild("AccessControlList").getChildText("Grant");
        CannedAccessControlList acl = CannedAccessControlList.parse(aclString);
        bucketInfo.setCannedACL(acl);
        switch(acl) {
            case PublicRead:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.Read);
                break;
            case PublicReadWrite:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
                break;
            default:
                break;
        }
        return bucketInfo;
    } 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) BucketInfo(com.aliyun.oss.model.BucketInfo) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 3 with BucketInfo

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

the class ClientBuilderTest method testClientBuilderWithCredentialProvider.

@Test
public void testClientBuilderWithCredentialProvider() {
    try {
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), null);
        Assert.assertFalse(ossClient.getClientConfiguration().isSupportCname());
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        Assert.assertEquals(info.getBucket().getName(), bucketName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(TEST_CONTENT.getBytes().length);
        ossClient.putObject(bucketName, TEST_KEY, new ByteArrayInputStream(TEST_CONTENT.getBytes()), metadata);
        OSSObject ossObject = ossClient.getObject(bucketName, TEST_KEY);
        InputStream inputStream = ossObject.getObjectContent();
        inputStream.close();
        ossClient.deleteObject(bucketName, TEST_KEY);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) OSSClient(com.aliyun.oss.OSSClient) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 4 with BucketInfo

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

the class ClientBuilderTest method testClientBuilderWithBuilderConfiguration.

@Test
public void testClientBuilderWithBuilderConfiguration() {
    try {
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), new ClientBuilderConfiguration());
        Assert.assertFalse(ossClient.getClientConfiguration().isSupportCname());
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        Assert.assertEquals(info.getBucket().getName(), bucketName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(TEST_CONTENT.getBytes().length);
        ossClient.putObject(bucketName, TEST_KEY, new ByteArrayInputStream(TEST_CONTENT.getBytes()), metadata);
        OSSObject ossObject = ossClient.getObject(bucketName, TEST_KEY);
        InputStream inputStream = ossObject.getObjectContent();
        inputStream.close();
        ossClient.deleteObject(bucketName, TEST_KEY);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) OSSClient(com.aliyun.oss.OSSClient) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 5 with BucketInfo

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

the class ProxyTest method testProxy.

@Ignore
public void testProxy() {
    String key = "test/test.txt";
    String content = "Hello OSS.";
    try {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setProxyHost(TestConfig.PROXY_HOST);
        conf.setProxyPort(TestConfig.PROXY_PORT);
        conf.setProxyUsername(TestConfig.PROXY_USER);
        conf.setProxyPassword(TestConfig.PROXY_PASSWORD);
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET, conf);
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        Assert.assertEquals(info.getBucket().getName(), bucketName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(content.getBytes().length);
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()), metadata);
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        InputStream inputStream = ossObject.getObjectContent();
        inputStream.close();
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Aggregations

BucketInfo (com.aliyun.oss.model.BucketInfo)8 OSSObject (com.aliyun.oss.model.OSSObject)6 OSSClient (com.aliyun.oss.OSSClient)5 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)3 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)3 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSException (com.aliyun.oss.OSSException)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 Bucket (com.aliyun.oss.model.Bucket)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 Grant (com.aliyun.oss.model.Grant)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 Owner (com.aliyun.oss.model.Owner)1