Search in sources :

Example 26 with OSSClientBuilder

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

the class ProxySignTest method testProxyAuth.

@Ignore
public void testProxyAuth() {
    String bucketName = "sdk-test-md-1";
    String key = "mingdi/test.txt";
    String content = "Hello OSS.";
    String proxyHost = "";
    String endpoint = "";
    String accessKeyId = "";
    String secretAccessKey = "";
    try {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setProxyHost(proxyHost);
        conf.setProxyPort(8080);
        Credentials credentials = new ProxyCredentials("mingditest");
        ProxyRequestSigner proxySigner = new ProxyRequestSigner(credentials);
        List<RequestSigner> signerHandlers = new LinkedList<RequestSigner>();
        signerHandlers.add(proxySigner);
        conf.setSignerHandlers(signerHandlers);
        Map<String, String> proxyHeaders = new LinkedHashMap<String, String>();
        proxyHeaders.put(HEADER_PROXY_TYPE, "default");
        proxyHeaders.put(HEADER_PROXY_USER, "diff_bucket_sync_test_user_1");
        proxyHeaders.put(HEADER_PROXY_DEST, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_DEST_REGION, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_REAL_HOST, endpoint);
        conf.setDefaultHeaders(proxyHeaders);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey, conf);
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.getObject(bucketName, key);
        ossClient.deleteObject(bucketName, key);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : LinkedList(java.util.LinkedList) OSS(com.aliyun.oss.OSS) LinkedHashMap(java.util.LinkedHashMap) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Credentials(com.aliyun.oss.common.auth.Credentials) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) RequestSigner(com.aliyun.oss.common.auth.RequestSigner) Ignore(org.junit.Ignore)

Example 27 with OSSClientBuilder

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

the class UrlSignatureTest method testPutObject.

@Ignore
public void testPutObject() {
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
    try {
        Date expiration = DateUtil.parseRfc822Date("Thu, 19 Mar 2015 18:00:00 GMT");
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
        request.setExpiration(expiration);
        request.setContentType("application/octet-stream");
        request.addUserMetadata("author", "aliy");
        URL signedUrl = client.generatePresignedUrl(request);
        System.out.println("signed url for putObject: " + signedUrl);
        File f = new File(filePath);
        FileInputStream fin = new FileInputStream(f);
        Map<String, String> customHeaders = new HashMap<String, String>();
        customHeaders.put("Content-Type", "application/octet-stream");
        customHeaders.put("x-oss-meta-author", "aliy");
        PutObjectResult result = client.putObject(signedUrl, fin, f.length(), customHeaders);
        fin = new FileInputStream(f);
        byte[] binaryData = IOUtils.readStreamAsByteArray(fin);
        String expectedETag = BinaryUtil.encodeMD5(binaryData);
        String actualETag = result.getETag();
        Assert.assertEquals(expectedETag, actualETag);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) PutObjectResult(com.aliyun.oss.model.PutObjectResult) OSS(com.aliyun.oss.OSS) Date(java.util.Date) URL(java.net.URL) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) File(java.io.File) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Example 28 with OSSClientBuilder

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

the class BucketReplicationTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    if (replicationClient == null) {
        replicationClient = new OSSClientBuilder().build(OSS_TEST_REPLICATION_ENDPOINT, OSS_TEST_REPLICATION_ACCESS_KEY_ID, OSS_TEST_REPLICATION_ACCESS_KEY_SECRET);
        replicationClient.createBucket(targetBucketName);
        BucketList buckets = replicationClient.listBuckets(targetBucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
    }
}
Also used : BucketList(com.aliyun.oss.model.BucketList) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) BeforeClass(org.junit.BeforeClass)

Example 29 with OSSClientBuilder

use of com.aliyun.oss.OSSClientBuilder 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 30 with OSSClientBuilder

use of com.aliyun.oss.OSSClientBuilder 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)

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