You need to enable JavaScript to run this app.
导航
管理对象的访问权限(Android SDK)
最近更新时间:2024.02.04 18:31:00首次发布时间:2022.12.01 16:31:40

本文介绍如何获取和设置对象的访问权限。对象的访问权限优先级高于桶的访问权限,如果对象未设置访问权限,则遵循桶的访问权限。

对象权限说明

访问权限值

描述

private

私有。对象的所有者拥有所有权限,其他用户没有权限操作该对象。

public-read

公共读。对象的所有者拥有所有权限,其他用户只有该对象的读权限。

public-read-write

公共读写。所有用户都有该对象文件的读写权限。

authenticated-read

对象的所有者拥有所有权限,认证用户拥有该对象的读权限。

bucket-owner-read

对象所有者拥有所有权限,桶所有者拥有该对象的读权限。

bucket-owner-full-control

桶所有者和对象所有者都拥有对象的所有操作权限。

设置对象的访问权限

以下代码展示如何通过 putObjectAcl 接口设置对象的访问权限。

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.comm.common.ACLType;
import com.volcengine.tos.model.object.PutObjectACLInput;
import com.volcengine.tos.model.object.PutObjectACLOutput;

public class PutObjectAclExample extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = "your access key";
        String secretKey = "your secret key";
        String securityToken = "your security token";

        String bucketName = "your bucket name";
        String objectKey = "your object key";

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);

        Thread tosThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    // 授予 objectKey 对象公共读权限
                    PutObjectACLInput input = new PutObjectACLInput().setAcl(ACLType.ACL_PUBLIC_READ)
                            .setBucket(bucketName).setKey(objectKey);
                    PutObjectACLOutput output = tos.putObjectAcl(input);
                    Log.i("putObjectAcl", "putObjectAcl succeed, request info is " + output);
                } catch (TosException e) {
                    Log.e("TosException", "putObjectAcl failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

获取对象的访问权限

以下代码展示如何通过 getObjectAcl 接口获取对象的访问权限。

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.model.object.GetObjectACLV2Input;
import com.volcengine.tos.model.object.GetObjectACLV2Output;

public class GetObjectAclExample extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = "your access key";
        String secretKey = "your secret key";
        String securityToken = "your security token";

        String bucketName = "your bucket name";
        String objectKey = "your object key";

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);

        Thread tosThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    GetObjectACLV2Input input = new GetObjectACLV2Input().setBucket(bucketName).setKey(objectKey);
                    GetObjectACLV2Output output = tos.getObjectAcl(input);
                    Log.i("getObjectAcl", "getObjectAcl succeed, full acl info is " + output);
                } catch (TosException e) {
                    Log.e("TosException", "getObjectAcl failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于获取对象的读写权限 API 文档,请参见 GetObjectAcl