判断Bucket是否存在
更新时间:2022-10-21
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient.doesBucketExist()方法。
- doesBucketExist()方法会返回布尔类型,判断bucket是否存在。
示例代码
// 获取Bucket的存在信息
boolean exists = client.doesBucketExist(<BucketName>); //指定Bucket名称
// 输出结果
if (exists) {
System.out.println("Bucket exists");
} else {
System.out.println("Bucket not exists");
}
完整示例
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;
public class ExampleActivity extends Activity {
private String bucketName = <BucketName>; //您的Bucket名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
try {
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
config.setEndpoint(<EndPoint>);
BosClient client = new BosClient(config);
// 获取Bucket的存在信息
boolean exists = client.doesBucketExist(<BucketName>); //指定Bucket名称
// 输出结果
if (exists) {
System.out.println("Bucket exists");
} else {
System.out.println("Bucket not exists");
}
} 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();
}
}