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

通过 listObjects 接口,可以列举桶内指定前缀的对象信息,每次最大返回 1000 个对象,列举结果默认返回桶的 Owner 信息。

参数说明

参数

描述

prefix

本次查询结果的前缀。

delimiter

进行对象名称分组的字符,默认为 /

marker

此次列举对象的起点。

encodingType

返回 Key 编码类型。

maxKeys

最大返回数,最大值为 1000,即一次列举最多返回 1000 个对象。

列举所有对象

以下代码展示如何列举指定桶中的所有对象。

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.ListObjectsV2Input;
import com.volcengine.tos.model.object.ListObjectsV2Output;
import com.volcengine.tos.model.object.ListedObjectV2;

public class ListObjectLoopExample 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 marker = "";
                    int total = 0;
                    while (isTruncated) {
                        ListObjectsV2Input input = new ListObjectsV2Input()
                                // 必须设置 bucket name
                                .setBucket(bucketName).setMaxKeys(maxKeys).setMarker(marker);
                        ListObjectsV2Output output = tos.listObjects(input);
                        Log.i("listObjects", "listObjects succeed, is truncated? " + output.isTruncated() + "next marker is " + output.getNextMarker());
                        if (output.getContents() != null) {
                            for (int i = 0; i < output.getContents().size(); i++) {
                                ListedObjectV2 object = output.getContents().get(i);
                                Log.i("listObjects", "No." + (i+1+total) + " listed object is " + object);
                            }
                            total += output.getContents().size();
                        }
                        isTruncated = output.isTruncated();
                        marker = output.getNextMarker();
                    }
                } catch (TosException e) {
                    Log.e("TosException", "listObjects 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.ListObjectsV2Input;
import com.volcengine.tos.model.object.ListObjectsV2Output;
import com.volcengine.tos.model.object.ListedObjectV2;

public class ListObjectsByPrefixExample 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;
        String prefix = "the specific prefix";

        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 marker = "";
                    int total = 0;
                    while (isTruncated) {
                        ListObjectsV2Input input = new ListObjectsV2Input()
                                // 必须设置 bucket name
                                .setBucket(bucketName).setMaxKeys(maxKeys).setMarker(marker).setPrefix(prefix);
                        ListObjectsV2Output output = tos.listObjects(input);
                        Log.i("listObjects", "listObjects succeed, is truncated? " + output.isTruncated() + "next marker is " + output.getNextMarker());
                        if (output.getContents() != null) {
                            for (int i = 0; i < output.getContents().size(); i++) {
                                ListedObjectV2 object = output.getContents().get(i);
                                Log.i("listObjects", "No." + (i+1+total) + " listed object is " + object);
                            }
                            total += output.getContents().size();
                        }
                        isTruncated = output.isTruncated();
                        marker = output.getNextMarker();
                    }
                } catch (TosException e) {
                    Log.e("TosException", "listObjects failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

列举指定位置后的对象

设置参数 marker 可以指定列举的起始位置,TOS 服务将会返回字符串 marker 的字典序后的所有对象。
以下代码展示如何通过列举指定起始位置后的对象。

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.ListObjectsV2Input;
import com.volcengine.tos.model.object.ListObjectsV2Output;
import com.volcengine.tos.model.object.ListedObjectV2;

public class ListObjectsByMarkerExample 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;
        // 初始位置
        String initMarker = "the specific marker";

        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 marker = initMarker;
                    int total = 0;
                    while (isTruncated) {
                        ListObjectsV2Input input = new ListObjectsV2Input()
                                // 必须设置 bucket name
                                .setBucket(bucketName).setMaxKeys(maxKeys).setMarker(marker);
                        ListObjectsV2Output output = tos.listObjects(input);
                        Log.i("listObjects", "listObjects succeed, is truncated? " + output.isTruncated() + "next marker is " + output.getNextMarker());
                        if (output.getContents() != null) {
                            for (int i = 0; i < output.getContents().size(); i++) {
                                ListedObjectV2 object = output.getContents().get(i);
                                Log.i("listObjects", "No." + (i+1+total) + " listed object is " + object);
                            }
                            total += output.getContents().size();
                        }
                        isTruncated = output.isTruncated();
                        marker = output.getNextMarker();
                    }
                } catch (TosException e) {
                    Log.e("TosException", "listObjects failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

列举指定目录下的文件和子目录

TOS 只有文件语义,没有文件夹语义。您可通过创建一个大小为 0 并且以斜杠 / 结尾的对象, 模拟目录的功能。
通过 delimiterprefix 两个参数可以模拟目录的功能:

  • 首先设置 delimiter/ 同时设置 prefix 为空, 可返回根目录下的对象和子目录信息。
  • 在设置 delimiter/ 同时设置 prefix 为子目录, 可返回子目录的对象和次级目录。

以下代码用于列举指定目录下的对象和子目录。

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.ListObjectsV2Input;
import com.volcengine.tos.model.object.ListObjectsV2Output;
import com.volcengine.tos.model.object.ListedObjectV2;

public class ListObjectsByDelimiterExample 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;
        String prefix = "the specific prefix";
        String delimiter = "/";

        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 marker = "";
                    int total = 0;
                    while (isTruncated) {
                        ListObjectsV2Input input = new ListObjectsV2Input()
                                // 必须设置 bucket name
                                .setBucket(bucketName).setMaxKeys(maxKeys).setMarker(marker).setPrefix(prefix).setDelimiter(delimiter);
                        ListObjectsV2Output output = tos.listObjects(input);
                        Log.i("listObjects", "listObjects succeed, is truncated? " + output.isTruncated() + "next marker is " + output.getNextMarker());
                        if (output.getContents() != null) {
                            for (int i = 0; i < output.getContents().size(); i++) {
                                ListedObjectV2 object = output.getContents().get(i);
                                Log.i("listObjects", "No." + (i+1+total) + " listed object is " + object);
                            }
                            total += output.getContents().size();
                        }
                        isTruncated = output.isTruncated();
                        marker = output.getNextMarker();
                    }
                } catch (TosException e) {
                    Log.e("TosException", "listObjects failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于列举对象的 API 文档,请参见 ListObjects