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();
}
}
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());
}
}
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());
}
}
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();
}
}
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...
}
Aggregations