use of com.aliyun.oss.model.BucketInfo in project FredaBlog by yangjinlong86.
the class OssUtils method test.
public static void test() {
String firstKey = "jason_test_object";
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
try {
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
if (ossClient.doesBucketExist(bucketName)) {
System.out.println("您已经创建Bucket:" + bucketName + "。");
} else {
System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
// 创建Bucket。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
ossClient.createBucket(bucketName);
}
// 查看Bucket信息。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
BucketInfo info = ossClient.getBucketInfo(bucketName);
System.out.println("Bucket " + bucketName + "的信息如下:");
System.out.println("\t数据中心:" + info.getBucket().getLocation());
System.out.println("\t创建时间:" + info.getBucket().getCreationDate());
System.out.println("\t用户标志:" + info.getBucket().getOwner());
// 把字符串存入OSS,Object的名称为firstKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
InputStream is = new ByteArrayInputStream("Hello OSS".getBytes());
ossClient.putObject(bucketName, firstKey, is);
System.out.println("Object:" + firstKey + "存入OSS成功。");
// 下载文件。详细请参看“SDK手册 > Java-SDK > 下载文件”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/download_object.html?spm=5176.docoss/sdk/java-sdk/manage_object
OSSObject ossObject = ossClient.getObject(bucketName, firstKey);
InputStream inputStream = ossObject.getObjectContent();
StringBuilder objectContent = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
objectContent.append(line);
}
inputStream.close();
System.out.println("Object:" + firstKey + "的内容是:" + objectContent);
// 文件存储入OSS,Object的名称为fileKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
String fileKey = "README.md";
ossClient.putObject(bucketName, fileKey, new File("README.md"));
System.out.println("Object:" + fileKey + "存入OSS成功。");
// 查看Bucket中的Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
ObjectListing objectListing = ossClient.listObjects(bucketName);
List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries();
System.out.println("您有以下Object:");
for (OSSObjectSummary object : objectSummary) {
System.out.println("\t" + object.getKey());
}
// 删除Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
// 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
} catch (OSSException oe) {
oe.printStackTrace();
} catch (ClientException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
logger.info("Completed");
}
use of com.aliyun.oss.model.BucketInfo in project aliyun-oss-java-sdk by aliyun.
the class ResponseParsers method parseGetBucketInfo.
/**
* Unmarshall get bucket info response body to bucket info.
*/
public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
Element bucketElem = root.getChild("Bucket");
BucketInfo bucketInfo = new BucketInfo();
// owner
Bucket bucket = new Bucket();
String id = bucketElem.getChild("Owner").getChildText("ID");
String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
Owner owner = new Owner(id, displayName);
bucket.setOwner(owner);
// bucket
bucket.setName(bucketElem.getChildText("Name"));
bucket.setLocation(bucketElem.getChildText("Location"));
bucket.setExtranetEndpoint(bucketElem.getChildText("ExtranetEndpoint"));
bucket.setIntranetEndpoint(bucketElem.getChildText("IntranetEndpoint"));
bucket.setCreationDate(DateUtil.parseIso8601Date(bucketElem.getChildText("CreationDate")));
if (bucketElem.getChild("StorageClass") != null) {
bucket.setStorageClass(StorageClass.parse(bucketElem.getChildText("StorageClass")));
}
bucketInfo.setBucket(bucket);
// acl
String aclString = bucketElem.getChild("AccessControlList").getChildText("Grant");
CannedAccessControlList acl = CannedAccessControlList.parse(aclString);
bucketInfo.setCannedACL(acl);
switch(acl) {
case PublicRead:
bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.Read);
break;
case PublicReadWrite:
bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
break;
default:
break;
}
return bucketInfo;
} catch (JDOMParseException e) {
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}
use of com.aliyun.oss.model.BucketInfo in project aliyun-oss-java-sdk by aliyun.
the class ClientBuilderTest method testClientBuilderWithCredentialProvider.
@Test
public void testClientBuilderWithCredentialProvider() {
try {
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), null);
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.model.BucketInfo in project aliyun-oss-java-sdk by aliyun.
the class ClientBuilderTest method testClientBuilderWithBuilderConfiguration.
@Test
public void testClientBuilderWithBuilderConfiguration() {
try {
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), new ClientBuilderConfiguration());
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.model.BucketInfo in project aliyun-oss-java-sdk by aliyun.
the class ProxyTest method testProxy.
@Ignore
public void testProxy() {
String key = "test/test.txt";
String content = "Hello OSS.";
try {
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setProxyHost(TestConfig.PROXY_HOST);
conf.setProxyPort(TestConfig.PROXY_PORT);
conf.setProxyUsername(TestConfig.PROXY_USER);
conf.setProxyPassword(TestConfig.PROXY_PASSWORD);
OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET, conf);
BucketInfo info = ossClient.getBucketInfo(bucketName);
Assert.assertEquals(info.getBucket().getName(), bucketName);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.getBytes().length);
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()), metadata);
OSSObject ossObject = ossClient.getObject(bucketName, key);
InputStream inputStream = ossObject.getObjectContent();
inputStream.close();
ossClient.deleteObject(bucketName, key);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations