桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。
tos:CreateBucket
权限。您可以通过 IAM 策略授予相应权限,具体步骤,请参见 IAM 策略。如下代码展示如何创建一个新桶。
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.comm.common.StorageClassType; import com.volcengine.tos.model.bucket.CreateBucketV2Input; import com.volcengine.tos.model.bucket.CreateBucketV2Output; public class CreateBucketFullExample 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"; // 设置桶的访问权限 ACLType aclType = ACLType.ACL_PRIVATE; // 设置桶的存储类型 StorageClassType storageClassType = StorageClassType.STORAGE_CLASS_STANDARD; 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 { CreateBucketV2Input input = new CreateBucketV2Input().setBucket(bucketName) .setAcl(aclType).setStorageClass(storageClassType); CreateBucketV2Output output = tos.createBucket(input); Log.i("createBucket", output.getLocation()); } catch (TosException e) { Log.e("TosException", "createBucket failed"); e.printStackTrace(); } } }); tosThread.start(); } }
关于创建桶的 API 文档,请参见 CreateBucket。