Bucket权限控制
更新时间:2022-10-25
设置Bucket的访问权限
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient.setBucketAcl()方法。
- setBucketAcl()无返回值,设置失败时会抛出异常。
示例代码
// 设置Bucket访问权限
client.setBucketAcl(<BucketName>, CannedAccessControlList.Private);
说明:CannedAccessControlList是枚举类型,包含三个值:
Private
、PublicRead
、PublicReadWrite
,它们分别对应相关权限。具体内容可以参考《BOS API文档 使用CannedAcl方式的权限控制》。
完整示例
import org.json.JSONException;
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.CannedAccessControlList;
public class ExampleActivity extends Activity {
private String bucketName = <BucketName>;
@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访问权限
client.setBucketAcl(<BucketName>, CannedAccessControlList.Private); //指定Bucket名称,并设置Bucket权限为Private
} 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());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
设置指定用户对Bucket的访问权限
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient.setBucketAcl()方法,您需要创建一个SetBucketAclRequest的实例来提供上述信息。
- setBucketAcl()无返回值,设置失败时会抛出异常。
示例代码
List<Grant> grants = new ArrayList<Grant>();
List<Grantee> grantee = new ArrayList<Grantee>();
List<Permission> permission = new ArrayList<Permission>();
//授权给用户
grantee.add(new Grantee(<UserID>);
//设置权限
permission.add(Permission.READ);
grants.add(new Grant().withGrantee(grantee).withPermission(permission));
//封装成request
SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants);
//设置bucket的访问权限
client.setBucketAcl(request);
注意:Permission中的权限设置包含三个值:
READ
、WRITE
、FULL_CONTROL
,它们分别对应相关权限。具体内容可以参考《BOS API文档 上传ACL文件方式的权限控制》。
完整示例
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
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.Grant;
import com.baidubce.services.bos.model.Grantee;
import com.baidubce.services.bos.model.Permission;
import com.baidubce.services.bos.model.SetBucketAclRequest;
public class ExampleActivity extends Activity {
private String bucketName = <BucektName>;
List<Grant> grants = new ArrayList<Grant>();
List<Grantee> grantee = new ArrayList<Grantee>();
List<Permission> permission = new ArrayList<Permission>();
@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);
//授权给特定用户
grantee.add(new Grantee("UserId_1"));
grantee.add(new Grantee("UserId_2"));
//授权给Everyone
grantee.add(new Grantee("*"));
//设置权限
permission.add(Permission.READ);
permission.add(Permission.WRITE);
grants.add(new Grant().withGrantee(grantee).withPermission(permission));
//封装request
SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants);
//设置bucket的访问权限
client.setBucketAcl(request);
} 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());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}