use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class LifecycleConfigTest method testLifecycleConfig.
@Ignore
public void testLifecycleConfig() {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
try {
SetBucketLifecycleRequest req = new SetBucketLifecycleRequest(bucketName);
req.AddLifecycleRule(new LifecycleRule("delete obsoleted files", "obsoleted/", RuleStatus.Enabled, 3));
req.AddLifecycleRule(new LifecycleRule("delete temporary files", "temporary/", RuleStatus.Enabled, DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z")));
// set bucket lifecycle
client.setBucketLifecycle(req);
// get bucket lifecycle
List<LifecycleRule> rules = client.getBucketLifecycle(bucketName);
Assert.assertEquals(rules.size(), 2);
LifecycleRule r0 = rules.get(0);
Assert.assertEquals(r0.getId(), "delete obsoleted files");
Assert.assertEquals(r0.getPrefix(), "obsoleted/");
Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(r0.getExpirationDays(), 3);
LifecycleRule r1 = rules.get(1);
Assert.assertEquals(r1.getId(), "delete temporary files");
Assert.assertEquals(r1.getPrefix(), "temporary/");
Assert.assertEquals(r1.getStatus(), RuleStatus.Enabled);
Assert.assertEquals(DateUtil.formatIso8601Date(r1.getExpirationTime()), "2022-10-12T00:00:00.000Z");
// delete bucket lifecycle
client.deleteBucketLifecycle(bucketName);
// try to get bucket lifecycle again
try {
client.getBucketLifecycle(bucketName);
} catch (OSSException ex) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_LIFECYCLE, ex.getErrorCode());
}
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class PostPolicyTest method testGenPostPolicy.
@Ignore
public void testGenPostPolicy() {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
try {
Date expiration = DateUtil.parseIso8601Date("2015-03-19T03:44:06.476Z");
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem("bucket", bucketName);
// $ must be escaped with backslash.
policyConds.addConditionItem(MatchMode.Exact, PolicyConditions.COND_KEY, "user/eric/\\${filename}");
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user/eric");
policyConds.addConditionItem(MatchMode.StartWith, "x-oss-meta-tag", "dummy_etag");
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1024);
String actualPostPolicy = client.generatePostPolicy(expiration, policyConds);
String expectedPostPolicy = "{\"expiration\":\"2015-03-19T03:44:06.476Z\",\"conditions\":[{\"bucket\":\"oss-test2\"},[\"eq\",\"$key\",\"user/eric/\\${filename}\"],[\"starts-with\",\"$key\",\"user/eric\"],[\"starts-with\",\"$x-oss-meta-tag\",\"dummy_etag\"],[\"content-length-range\",1,1024]]}";
Assert.assertEquals(expectedPostPolicy, actualPostPolicy);
byte[] binaryData = actualPostPolicy.getBytes("utf-8");
String actualEncodedPolicy = BinaryUtil.toBase64String(binaryData);
String expectedEncodedPolicy = "eyJleHBpcmF0aW9uIjoiMjAxNS0wMy0xOVQwMzo0NDowNi40NzZaIiwiY29uZGl0aW9ucyI6W3siYnVja2V0Ijoib3NzLXRlc3QyIn0sWyJlcSIsIiRrZXkiLCJ1c2VyL2VyaWMvXCR7ZmlsZW5hbWV9Il0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyL2VyaWMiXSxbInN0YXJ0cy13aXRoIiwiJHgtb3NzLW1ldGEtdGFnIiwiZHVtbXlfZXRhZyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDEsMTAyNF1dfQ==";
Assert.assertEquals(expectedEncodedPolicy, actualEncodedPolicy);
String actualPostSignature = client.calculatePostSignature(actualPostPolicy);
String expectedPostSignature = "+fOC13qQyIUqF+T/mSA/So2qEBw=";
Assert.assertEquals(expectedPostSignature, actualPostSignature);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class OSSClientTest method testProxyHost.
@Test
public void testProxyHost() {
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
String accessKeyId = "accessKeyId";
String accessKeySecret = "accessKeySecret";
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setProxyHost(endpoint);
conf.setProxyPort(80);
conf.setProxyUsername("user");
conf.setProxyPassword("passwd");
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
ossClient.shutdown();
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class CustomSessionCredentialsProviderTest method testGetCredentialsFromAuthInOss.
@Test
public void testGetCredentialsFromAuthInOss() {
try {
CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
Credentials credentials = credentialsProvider.getCredentials();
Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
Assert.assertEquals(credentials.getSecurityToken().length(), 516);
Assert.assertTrue(credentials.useSecurityToken());
String key = "test.txt";
String content = "HelloOSS";
OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSecurityToken());
ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class CustomSessionCredentialsProviderTest method testGetCredentialsUseInOss.
@Test
public void testGetCredentialsUseInOss() {
try {
CustomSessionCredentialsProvider credentialsProvider = CredentialsProviderFactory.newCustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
String key = "test.txt";
String content = "HelloOSS";
OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentialsProvider);
ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
Thread.sleep(1000 * 10);
ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations