use of com.aliyun.oss.OSSClient 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());
}
}
use of com.aliyun.oss.OSSClient 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());
}
}
use of com.aliyun.oss.OSSClient in project aliyun-oss-java-sdk by aliyun.
the class BucketAclTest method testUnormalDoesBucketExist.
@Test
public void testUnormalDoesBucketExist() {
final String nonexistentBucket = "unormal-does-bucket-exist";
try {
Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
OSSClient ossClient = new OSSClient("http://oss-cn-taikang.aliyuncs.com", new DefaultCredentialProvider(credentials));
ossClient.doesBucketExist(nonexistentBucket);
Assert.fail("Does bucket exist should not be successful");
} catch (Exception e) {
Assert.assertEquals("UnknownHost", e.getMessage());
}
}
use of com.aliyun.oss.OSSClient in project FredaBlog by yangjinlong86.
the class OssUtils method putObject.
/**
* 上传文件
*
* @param filePath
* @param fileName
*/
public static void putObject(String filePath, String fileName) {
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, new File(filePath));
System.out.println("文件上传成功!");
}
use of com.aliyun.oss.OSSClient in project spring-boot-quick by vector4wang.
the class OSSUnit method main.
// init static datas
// static{
// ResourceBundle bundle = PropertyResourceBundle.getBundle("properties.oss");
// ENDPOINT = bundle.containsKey("endpoint") == false ? "" : bundle.getString("endpoint");
// ACCESS_KEY_ID = bundle.containsKey("accessKeyId") == false? "" : bundle.getString("accessKeyId");
// ACCESS_KEY_SECRET = bundle.containsKey("accessKeySecret") == false ? "" : bundle.getString("accessKeySecret");
// }
public static void main(String[] args) {
OSSClient client = OSSUnit.getOSSClient();
try {
BufferedInputStream bis = new BufferedInputStream(OSSUnit.getOSS2InputStream(client, "rcboxtest", "", "71fa2f1c2f33751076fc523bb089396b21cd1"));
String resfile = "D:\\data\\oss\\71fa2f1c2f33751076fc523bb089396b21cd1.doc";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(resfile)));
int itemp = 0;
while ((itemp = bis.read()) != -1) {
bos.write(itemp);
}
// console log :文件获取成功
LOG.info("文件获取成功");
bis.close();
bos.close();
} catch (Exception e) {
LOG.error("从OSS获取文件失败:" + e.getMessage(), e);
}
}
Aggregations