You need to enable JavaScript to run this app.
导航
删除对象(Android SDK)
最近更新时间:2024.02.04 18:31:01首次发布时间:2022.12.01 16:31:40

通过 deleteObject 接口,可以删除指定的对象。通过 deleteMultiObjects 接口,可以删除指定列表内的一批对象。

参数说明

参数

描述

versionID

版本号,如果桶开启多版本功能,可以携带此参数删除指定版本对象。

删除对象

以下代码展示如何删除对象。

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.object.DeleteObjectInput;
import com.volcengine.tos.model.object.DeleteObjectOutput;

public class DeleteObjectExample 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";
        String objectKey = "your object key";
        // 如果桶开启了多版本功能,可以指定版本号删除对象
        String versionID = "the specific version id";
        
        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{
                    DeleteObjectInput input = new DeleteObjectInput().setBucket(bucketName).setKey(objectKey);
                    DeleteObjectOutput output = tos.deleteObject(input);
                    // 如果桶开启了多版本功能,可以指定版本号删除对象
                    input.setVersionID(versionID);
                    Log.i("deleteObject", "succeed, " + output);
                } catch (TosException e) {
                    Log.e("TosException", "deleteObject failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

批量删除对象

以下代码展示如何批量删除对象。

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.object.DeleteError;
import com.volcengine.tos.model.object.DeleteMultiObjectsV2Input;
import com.volcengine.tos.model.object.DeleteMultiObjectsV2Output;
import com.volcengine.tos.model.object.Deleted;
import com.volcengine.tos.model.object.ObjectTobeDeleted;

import java.util.ArrayList;
import java.util.List;

public class DeleteMultiObjectsExample 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";
        String objectKey = "your object key";
        // 可选,如果桶开启了多版本功能,可以指定版本号删除对象
        String versionID = "the specific version id";

        String objectKey1 = "your object key";
        // 可选,如果桶开启了多版本功能,可以指定版本号删除对象
        String versionID1 = "the specific version id";

        String objectKey2 = "your object key";
        // 可选,如果桶开启了多版本功能,可以指定版本号删除对象
        String versionID2 = "the specific version id";

        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{
                    List<ObjectTobeDeleted> objs = new ArrayList<>();
                    objs.add(new ObjectTobeDeleted().setKey(objectKey).setVersionID(versionID));
                    objs.add(new ObjectTobeDeleted().setKey(objectKey1).setVersionID(versionID1));
                    objs.add(new ObjectTobeDeleted().setKey(objectKey2).setVersionID(versionID2));
                    DeleteMultiObjectsV2Input input = new DeleteMultiObjectsV2Input().setBucket(bucketName).setObjects(objs);
                    DeleteMultiObjectsV2Output output = tos.deleteMultiObjects(input);
                    Log.i("deleteMultiObjects", "deleteMultiObjects succeed, request info is " + output.getRequestInfo());
                    if (output.getDeleteds() != null) {
                        for (int i = 0; i < output.getDeleteds().size(); i++){
                            Deleted deleted = output.getDeleteds().get(i);
                            Log.i("deleteMultiObjects", "No." + i + " deleted object is " + deleted);
                        }
                    }
                    if (output.getErrors() != null) {
                        for (int i = 0; i < output.getErrors().size(); i++){
                            DeleteError error = output.getErrors().get(i);
                            Log.i("deleteMultiObjects", "No." + i + " delete error is " + error);
                        }
                    }
                } catch (TosException e) {
                    Log.e("TosException", "deleteMultiObjects failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于删除对象的 API 文档,请参见 DeleteObject