存储桶是存储对象的容器。您可以通过 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。