Search in sources :

Example 26 with OSS

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

the class RequestTimeoutTest method testClientConfigIndependent.

/**
 * Testing connection timeout.
 */
@Test
public void testClientConfigIndependent() throws Exception {
    String key = "test-client-config-independent";
    ClientBuilderConfiguration config = new ClientBuilderConfiguration();
    config.setRequestTimeout(requestTimeout);
    config.setRequestTimeoutEnabled(true);
    config.setConnectionTimeout(1);
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
    try {
        client.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(1024));
        Assert.fail("Put object should not be successful");
    } catch (ClientException e) {
        Assert.assertEquals(ClientErrorCode.CONNECTION_TIMEOUT, e.getErrorCode());
    } finally {
        client.shutdown();
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 27 with OSS

use of com.aliyun.oss.OSS 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 28 with OSS

use of com.aliyun.oss.OSS 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 29 with OSS

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

the class GetSimplifiedObjectMetaTest method testUnormalGetSimplifiedObjectMeta.

@Test
public void testUnormalGetSimplifiedObjectMeta() throws Exception {
    // Try to get simplified object meta under nonexistent bucket
    final String key = "unormal-get-simplified-object-meta";
    final String nonexistentBucket = "nonexistent-bukcet";
    try {
        ossClient.getSimplifiedObjectMeta(nonexistentBucket, key);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
    }
    // Try to get nonexistent object
    final String nonexistentKey = "nonexistent-object";
    try {
        ossClient.getSimplifiedObjectMeta(bucketName, nonexistentKey);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_KEY_ERR));
    }
    // SignatureDoesNotMatch
    OSS client = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET + " ");
    try {
        client.getSimplifiedObjectMeta(bucketName, nonexistentKey);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.SIGNATURE_DOES_NOT_MATCH, ex.getErrorCode());
    } finally {
        client.shutdown();
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 30 with OSS

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

the class CnameTest method testCnameExcludeList.

@Ignore
@SuppressWarnings("unused")
public void testCnameExcludeList() {
    ClientBuilderConfiguration cc = new ClientBuilderConfiguration();
    // Defalut CNAME Exclude List: [.aliyuncs.com, .aliyun-inc.com, localhost]
    List<String> currentExcludeList = cc.getCnameExcludeList();
    Assert.assertEquals(currentExcludeList.size(), 3);
    Assert.assertTrue(currentExcludeList.contains(".aliyuncs.com"));
    Assert.assertTrue(currentExcludeList.contains(".aliyun-inc.com"));
    Assert.assertTrue(currentExcludeList.contains("localhost"));
    List<String> cnameExcludeList = new ArrayList<String>();
    String excludeItem = "http://oss-cn-hangzhou.aliyuncs.gd";
    // Add your customized host name here
    cnameExcludeList.add(excludeItem);
    cc.setCnameExcludeList(cnameExcludeList);
    currentExcludeList = cc.getCnameExcludeList();
    Assert.assertEquals(currentExcludeList.size(), 4);
    Assert.assertTrue(currentExcludeList.contains(excludeItem));
    Assert.assertTrue(currentExcludeList.contains(".aliyuncs.com"));
    Assert.assertTrue(currentExcludeList.contains(".aliyun-inc.com"));
    Assert.assertTrue(currentExcludeList.contains("localhost"));
    OSS client = new OSSClientBuilder().build("<input your customized host name>", "<input your access id>", "<input your access key>", cc);
// Do some operations with client here...
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ArrayList(java.util.ArrayList) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Aggregations

OSS (com.aliyun.oss.OSS)50 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)40 Test (org.junit.Test)24 OSSException (com.aliyun.oss.OSSException)19 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ClientException (com.aliyun.oss.ClientException)17 ObjectListing (com.aliyun.oss.model.ObjectListing)10 Date (java.util.Date)10 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)9 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)9 OSSObject (com.aliyun.oss.model.OSSObject)8 Ignore (org.junit.Ignore)8 File (java.io.File)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 Credentials (com.aliyun.oss.common.auth.Credentials)4 URI (java.net.URI)4 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)3