存储桶是存储对象的容器。您可以通过 headBucket 接口可以判断桶是否存在,以及获取桶的元数据。
tos:HeadBucket
权限。具体操作,请参见权限配置指南。如下代码展示如何获取桶信息。
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.HeadBucketV2Input; import com.volcengine.tos.model.bucket.HeadBucketV2Output; public class HeadBucketExample 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{ HeadBucketV2Input input = new HeadBucketV2Input().setBucket(bucketName); HeadBucketV2Output output = tos.headBucket(input); Log.i("headBucket", "bucket's region is " + output.getRegion()); Log.i("headBucket", "bucket's storage class is " + output.getStorageClass()); } catch (TosException e) { if (e.getStatusCode() == 404) { Log.e("TosException", "headBucket failed, the bucket is not found."); } else { Log.e("TosException", "headBucket failed."); } e.printStackTrace(); } } }); tosThread.start(); } }
关于获取桶的元数据 API 文档,请参见 HeadBucket。