You need to enable JavaScript to run this app.
导航

列举桶(Android SDK)

最近更新时间2024.02.04 18:31:00

首次发布时间2022.12.01 16:31:39

存储桶是存储对象的容器。您可以通过 listBuckets 获取存储桶(Bucket)列表,该操作会返回所有桶列表。

注意事项

  • 列举桶之前,您必须具有 tos:ListBuckets 权限。具体操作,请参见权限配置指南
  • 此接口会返回当前账号所有地域的桶。

示例代码

如下代码展示如何列举所有的桶。

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.bucket.ListBucketsV2Input;
import com.volcengine.tos.model.bucket.ListBucketsV2Output;
import com.volcengine.tos.model.bucket.ListedBucket;

public class ListBucketExample 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";
        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{
                    ListBucketsV2Input input = new ListBucketsV2Input();
                    ListBucketsV2Output output = tos.listBuckets(input);
                    Log.i("listBuckets", "bucket owner is " + output.getOwner());
                    if (output.getBuckets() != null) {
                        Log.i("listBuckets", "you have listed " + output.getBuckets().size() + " buckets");
                        for (int i = 0; i < output.getBuckets().size(); i++){
                            ListedBucket bucket = output.getBuckets().get(i);
                            Log.i("listBuckets", "the No." + " bucket is " + bucket.toString());
                        }
                    } else {
                        Log.i("listBuckets", "there are no buckets in your account.");
                    }
                } catch (TosException e) {
                    Log.e("TosException", "listBuckets failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于列举桶的 API 文档,请参见 ListBuckets