创建Bucket
您可以使用OSSClient.createBucket创建Bucket。如下代码展示如何 新建一个Bucket:
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 创建bucket
String bucketName = "<your-oss-bucket-name>";
ossClient.createBucket(bucketName);
// 关闭client
ossClient.shutdown();
提示:
- Bucket的命名规范,参见基本概念中的命名规范。
- Bucket的名字是[backcolor=transparent]全局唯一的,所以您需要保证Bucket名称不与别人重复。
CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
// 设置bucket权限为公共读,默认是私有读写
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
// 设置bucket存储类型为低频访问类型,默认是标准类型
createBucketRequest.setStorageClass(StorageClass.IA);
ossClient.createBucket(createBucketRequest);
[backcolor=transparent]注意:
- 创建指定类型的Bucket,请使用Java SDK 2.6.0及以上版本。
[backcolor=transparent]注意:
- 如果Bucket有低频存储类型或归档存储类型,请使用Java SDK 2.6.0及以上版本。
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 列举bucket
List<Bucket> buckets = ossClient.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(" - " + bucket.getName());
}
// 关闭client
ossClient.shutdown();
参数 | 作用 |
prefix | 限定返回的Bucket name必须以prefix作为前缀,可以不设定,不设定时不过滤前缀信息 |
marker | 设定结果从marker之后按字母排序的第一个开始返回,可以不设定,不设定时从头开始返回 |
max keys | 限定此次返回Bucket的最大数,如果不设定,默认为100,max-keys取值不能大于1000 |
ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
listBucketsRequest.setPrefix("<yourBucketPrefix>");
for (Bucket bucket : ossClient.listBuckets()) {
System.out.println(" - " + bucket.getName());
}
ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
listBucketsRequest.setMaxKeys(500);
for (Bucket bucket : ossClient.listBuckets()) {
System.out.println(" - " + bucket.getName());
}
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 删除bucket
ossClient.deleteBucket("<bucketName>");
// 关闭client
ossClient.shutdown();
提示:
- 如果存储空间不为空(存储空间中有文件或者分片上传碎片),则存储空间无法删除;
- 必须先删除存储空间中的所有文件后,存储空间才能成功删除。
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
boolean exists = ossClient.doesBucketExist("<bucketName>");
// 关闭client
ossClient.shutdown();
权限 | Java SDK对应值 |
私有读写 | CannedAccessControlList.Private |
公共读私有写 | CannedAccessControlList.PublicRead |
公共读写 | CannedAccessControlList.PublicReadWrite |
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 设置bucket权限
ossClient.setBucketAcl("<bucketName>", CannedAccessControlList.Private);
// 关闭client
ossClient.shutdown();
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
AccessControlList acl = ossClient.getBucketAcl("<bucketName>");
// Bucket权限
System.out.println(acl.toString());
// 关闭client
ossClient.shutdown();
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String location = ossClient.getBucketLocation("<bucketName>");
System.out.println(location);
// 关闭client
ossClient.shutdown();
// endpoint以杭州为例,其它region请按实际情况填写
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
BucketInfo info = ossClient.getBucketInfo("<bucketName>");
// Location
info.getBucket().getLocation();
// 创建日期
info.getBucket().getCreationDate();
// owner
info.getBucket().getOwner();
// 权限
info.getGrants();
// 关闭client
ossClient.shutdown();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。