新建Bucket
更新时间:2022-10-21
基本流程
- 创建BOSClient 类的实例。
- 执行BOSClient.createBucket()方法,你需要提供Bucket的名字。
- 对返回的对象可以调用执行getLocation()/getName()获取Bucket所在区域和Bucket名字。
示例代码
BosClient client = new BosClient(config); //创建BOSClient实例
client.createBucket(<BucketName>); //新建一个Bucket,指定Bucket名称
完整示例
import android.app.Activity;
import android.os.Bundle;
import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.development.R;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.CreateBucketResponse;
public class ExampleActivity extends Activity {
private static String bucketName = <BucketName>;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override-9
public void run() {
try {
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); //您的原始AK/SK
config.setEndpoint(<EndPoint>); //Bucket所在区域
BosClient client = new BosClient(config);
CreateBucketResponse response = client.createBucket(<BucketName>); //新建一个Bucket并指定Bucket名称
System.out.println(response.getLocation());
System.out.println(response.getName());
} catch (BceServiceException e) {
System.out.println("Error ErrorCode: " + e.getErrorCode());
System.out.println("Error RequestId: " + e.getRequestId());
System.out.println("Error StatusCode: " + e.getStatusCode());
System.out.println("Error Message: " + e.getMessage());
System.out.println("Error ErrorType: " + e.getErrorType());
} catch (BceClientException e) {
System.out.println("Error Message: " + e.getMessage());
}
}
}).start();
}
}
注意:由于Bucket的名称在所有区域中是唯一的,所以需要保证BucketName不与其他所有区域上的BucketName相同。