use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderInOss.
@Test
public void testStsKeyPairCredentialsProviderInOss() {
try {
PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH));
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()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderInOss.
@Test
public void testStsAssumeRoleCredentialsProviderInOss() {
try {
CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN).withExpiredDuration(900);
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()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesStsCredentialsProviderInOss.
@Test
public void testSystemPropertiesStsCredentialsProviderInOss() {
try {
CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, assumeRoleCred.getAccessKeyId());
System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, assumeRoleCred.getSecretAccessKey());
System.setProperty(AuthUtils.SESSION_TOKEN_SYSTEM_PROPERTY, assumeRoleCred.getSecurityToken());
SystemPropertiesCredentialsProvider credentialsProvider = CredentialsProviderFactory.newSystemPropertiesCredentialsProvider();
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()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class CallbackSample method main.
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
String content = "Hello OSS";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "key", new ByteArrayInputStream(content.getBytes()));
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"bucket\\\":${bucket},\\\"object\\\":${object}," + "\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}," + "\\\"my_var1\\\":${x:var1},\\\"my_var2\\\":${x:var2}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[1024];
putObjectResult.getResponse().getContent().read(buffer);
putObjectResult.getResponse().getContent().close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
ossClient.shutdown();
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class CreateFolderSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/*
* Create an empty folder without request body, note that the key must be
* suffixed with a slash
*/
final String keySuffixWithSlash = "MyObjectKey/";
client.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
System.out.println("Creating an empty folder " + keySuffixWithSlash + "\n");
/*
* Verify whether the size of the empty folder is zero
*/
OSSObject object = client.getObject(bucketName, keySuffixWithSlash);
System.out.println("Size of the empty folder '" + object.getKey() + "' is " + object.getObjectMetadata().getContentLength());
object.getObjectContent().close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
Aggregations