Search in sources :

Example 16 with OSSClient

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

the class ClientBuilderTest method testClientBuilderWithAll.

@Test
public void testClientBuilderWithAll() {
    try {
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setSupportCname(true);
        config.setConnectionTimeout(10000);
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), config);
        Assert.assertTrue(ossClient.getClientConfiguration().isSupportCname());
        Assert.assertEquals(ossClient.getClientConfiguration().getConnectionTimeout(), 10000);
        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 17 with OSSClient

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

the class ClientBuilderTest method testClientBuilderDefault.

@Test
public void testClientBuilderDefault() {
    try {
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        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) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 18 with OSSClient

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

the class BucketAclTest method testUnormalDoesBucketExist.

@Test
public void testUnormalDoesBucketExist() {
    final String nonexistentBucket = "unormal-does-bucket-exist";
    try {
        Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        OSSClient ossClient = new OSSClient("http://oss-cn-taikang.aliyuncs.com", new DefaultCredentialProvider(credentials));
        ossClient.doesBucketExist(nonexistentBucket);
        Assert.fail("Does bucket exist should not be successful");
    } catch (Exception e) {
        Assert.assertEquals("UnknownHost", e.getMessage());
    }
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 19 with OSSClient

use of com.aliyun.oss.OSSClient in project FredaBlog by yangjinlong86.

the class OssUtils method putObject.

/**
 * 上传文件
 *
 * @param filePath
 * @param fileName
 */
public static void putObject(String filePath, String fileName) {
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    ossClient.putObject(bucketName, fileName, new File(filePath));
    System.out.println("文件上传成功!");
}
Also used : OSSClient(com.aliyun.oss.OSSClient)

Example 20 with OSSClient

use of com.aliyun.oss.OSSClient in project spring-boot-quick by vector4wang.

the class OSSUnit method main.

// init static datas
// static{
// ResourceBundle bundle = PropertyResourceBundle.getBundle("properties.oss");
// ENDPOINT = bundle.containsKey("endpoint") == false ? "" : bundle.getString("endpoint");
// ACCESS_KEY_ID = bundle.containsKey("accessKeyId") == false? "" : bundle.getString("accessKeyId");
// ACCESS_KEY_SECRET = bundle.containsKey("accessKeySecret") == false ? "" : bundle.getString("accessKeySecret");
// }
public static void main(String[] args) {
    OSSClient client = OSSUnit.getOSSClient();
    try {
        BufferedInputStream bis = new BufferedInputStream(OSSUnit.getOSS2InputStream(client, "rcboxtest", "", "71fa2f1c2f33751076fc523bb089396b21cd1"));
        String resfile = "D:\\data\\oss\\71fa2f1c2f33751076fc523bb089396b21cd1.doc";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(resfile)));
        int itemp = 0;
        while ((itemp = bis.read()) != -1) {
            bos.write(itemp);
        }
        // console log :文件获取成功
        LOG.info("文件获取成功");
        bis.close();
        bos.close();
    } catch (Exception e) {
        LOG.error("从OSS获取文件失败:" + e.getMessage(), e);
    }
}
Also used : OSSClient(com.aliyun.oss.OSSClient)

Aggregations

OSSClient (com.aliyun.oss.OSSClient)23 Test (org.junit.Test)8 OSSObject (com.aliyun.oss.model.OSSObject)7 ClientConfiguration (com.aliyun.oss.ClientConfiguration)6 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 BucketInfo (com.aliyun.oss.model.BucketInfo)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 OSSException (com.aliyun.oss.OSSException)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)3 Credentials (com.aliyun.oss.common.auth.Credentials)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)2 Bucket (com.aliyun.oss.model.Bucket)2 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)2 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)2 IOException (java.io.IOException)2