您可以用 deleteBucket 接口删除一个不包含任何对象数据的空桶。对于非空的桶,您需要先把桶中的对象及未完成的分片上传任务清空之后,才能够调用此接口来进行删桶操作。
tos:DeleteBucket
权限。具体操作,请参见权限配置指南。删除桶之前,请确保您已经删除桶中所有数据。
如下代码展示如何删除空桶。
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.DeleteBucketInput; import com.volcengine.tos.model.bucket.DeleteBucketOutput; public class DeleteBucketExample 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{ DeleteBucketInput input = new DeleteBucketInput().setBucket(bucketName); DeleteBucketOutput output = tos.deleteBucket(input); Log.i("deleteBucket", "deleteBucket succeed, " + output); } catch (TosException e) { if (e.getStatusCode() == 404) { Log.e("TosException", "deleteBucket failed, the bucket is not found."); } else { Log.e("TosException", "deleteBucket failed."); } e.printStackTrace(); } } }); tosThread.start(); } }
关于删除桶的 API 文档,请参见 DeleteBucket。