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

通过 listObjectVersions 接口,可以列举桶内的多版本对象信息,每次最大返回 1000 个多版本对象。

参数说明

参数

描述

prefix

本次查询结果的前缀。

delimiter

进行对象名称分组的字符。

keyMarker

此次列举对象的起点。

encodingType

返回 Key 编码类型。
默认值: None

maxKeys

最大返回数。

versionIdMarker

版本号分页标识。

列举所有多版本对象

以下代码展示如何分页列举所有多版本对象。

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.ListObjectVersionsV2Input;
import com.volcengine.tos.model.object.ListObjectVersionsV2Output;
import com.volcengine.tos.model.object.ListedDeleteMarkerEntry;

public class ListObjectVersionsLoopExample 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";
        int maxKeys = 1000;

        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{
                    boolean isTruncated = true;
                    String keyMarker = "";
                    String versionIDMarker = "";
                    int total = 0;
                    while (isTruncated) {
                        ListObjectVersionsV2Input input = new ListObjectVersionsV2Input()
                                // 必须设置 bucket name
                                .setBucket(bucketName).setMaxKeys(maxKeys)
                                .setKeyMarker(keyMarker).setVersionIDMarker(versionIDMarker);
                        ListObjectVersionsV2Output output = tos.listObjectVersions(input);
                        Log.i("listObjectVersions", "listObjectVersions succeed, is truncated? " + output.isTruncated() +
                                "next key marker is " + output.getNextKeyMarker() + "next version id marker is" +
                                output.getNextVersionIDMarker());
                        if (output.getDeleteMarkers() != null) {
                            for (int i = 0; i < output.getDeleteMarkers().size(); i++) {
                                ListedDeleteMarkerEntry entry = output.getDeleteMarkers().get(i);
                                Log.i("listObjectVersions", "No." + (i+1+total) + " listed entry is " + entry);
                            }
                            total += output.getDeleteMarkers().size();
                        }
                        isTruncated = output.isTruncated();
                        keyMarker = output.getNextKeyMarker();
                        versionIDMarker = output.getVersionIDMarker();
                    }

                } catch (TosException e) {
                    Log.e("TosException", "listObjectVersions failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于列举多版本对象的 API 文档,请参见 ListObjectsVersions