创建存储桶
更新时间: 2020-03-24
目录
存储桶(Bucket)是存储对象(Object)的容器,对象都隶属于存储桶。
本节介绍如何创建存储桶。
您可以通过ossClient.createBucket创建新的存储桶。createBucket有以下三类参数格式:
- OSSBucket createBucket(String bucketName):创建新的存储桶。最多创建100个。
- OSSBucket createBucket(String bucketName, CannedAccessControlList acl):创建指定访问权限的存储桶。最多创建100个。
- OSSBucket createBucket(CreateBucketRequest createBucketRequest):提供多种参数传递,实现灵活的创建功能。最多创建100个。
- createBucketRequest参数的属性及说明如下:
属性 | 描述 |
---|---|
String bucketName | 存储桶的名字 |
CannedAccessControlList cannedAccessControlList | 访问权限,分为Private、PublicRead、PublicReadWrite,详见管理存储桶访问权限 |
String locationConstraint | 存储类型,目前分为低频(STANDARD_IA)和标准(default-placement),格式"区域:类型",例如:cn-north-3:STANDARD_IA |
简单创建存储桶
以下代码用于简单创建存储桶:
// Endpoint以华北三为例,其它Region请按实际情况填写
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);
//创建bucket,不设置acl
OSSBucket bucket = ossClient.createBucket(bucketName);
System.out.println("Information about the bucket just created: " + bucket.toString());
创建指定访问权限的存储桶
以下代码用于创建设置访问权限存储桶:
// Endpoint以华北三为例,其它Region请按实际情况填写
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);
//创建bucket并设置acl, acl有三种类型:Private、PublicRead、PublicReadWrite
OSSBucket bucket = ossClient.createBucket(bucketName, CannedAccessControlList.PublicRead);
System.out.println("Information about the bucket just created: " + bucket.toString());
自定义请求对象创建存储桶
以下代码用于创建存储桶:
// Endpoint以华北三为例,其它Region请按实际情况填写
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);
//只设置桶名
//CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
//设置桶名和acl
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName, CannedAccessControlList.PublicRead);
OSSBucket bucket = ossClient.createBucket(createBucketRequest);
System.out.println("Information about the bucket just created: " + bucket.toString());