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

删除桶(Android SDK)

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

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

您可以用 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