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

对象元信息是对象的属性描述,包括 HTTP 标准属性(HTTP Header)和用户自定义元数据(User Meta)两种。

设置对象元数据

以下代码展示如何通过 setObjectMeta 接口设置对象的 Content-Type,以及设置对象的自定义元数据,用于标识对象的用途或属性等。

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.comm.common.ACLType;
import com.volcengine.tos.comm.common.StorageClassType;
import com.volcengine.tos.model.object.ObjectMetaRequestOptions;
import com.volcengine.tos.model.object.SetObjectMetaInput;
import com.volcengine.tos.model.object.SetObjectMetaOutput;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

public class SetObjectMetaExample 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";

        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{
                    String data = "1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./   :'1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./   :'";
                    ByteArrayInputStream stream = new ByteArrayInputStream(data.getBytes());
                    ObjectMetaRequestOptions options = new ObjectMetaRequestOptions();
                    // 设置对象访问权限,此处为私有权限
                    options.setAclType(ACLType.ACL_PRIVATE);
                    // 设置对象存储类型
                    options.setStorageClass(StorageClassType.STORAGE_CLASS_STANDARD);
                    // SDK 默认会根据 objectKey 后缀识别 Content-Type,也可以自定义设置
                    options.setContentType("application/json");
                    // 设置对象内容语言
                    options.setContentLanguage("en");
                    // 设置对象被下载时的名称
                    options.setContentDisposition("attachment;filename=download.txt");
                    // 设置对象的网页缓存行为
                    options.setCacheControl("max-age=600");
                    // 设置对象MD5,用于服务端校验数据是否与客户端传输的一致
                    options.setContentMD5("yjtlyPoGKxvDj+QOPocqjg==");
                    // 设置对象的服务端加密方式,当前只支持 AES256
                    options.setServerSideEncryption("AES256");
                    // 自定义对象的元数据,对于自定义的元数据,SDK 会自动对 key 添加
                    // "X-Tos-Meta-" 的前缀,并存储在服务端。
                    Map<String, String> custom = new HashMap<>();
                    custom.put("name", "volc_user");
                    // 在 TOS 服务端存储的元数据为:"X-Tos-Meta-name: volc_user"
                    options.setCustomMetadata(custom);

                    SetObjectMetaInput input = new SetObjectMetaInput().setBucket(bucketName).setKey(objectKey)
                            .setOptions(options);
                    SetObjectMetaOutput output = tos.setObjectMeta(input);
                    Log.i("setObjectMeta", "setObjectMeta succeed, request info is " + output);
                } catch (TosException e) {
                    Log.e("TosException", "setObjectMeta failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

获取对象元数据

通过 headObject 接口可以获取对象信息,判断对象是否存在,以及获取对象的元数据。以下代码展示如何获取对象的元数据。

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

public class HeadObjectExample 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";

        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{
                    HeadObjectV2Input input = new HeadObjectV2Input().setBucket(bucketName).setKey(objectKey);
                    HeadObjectV2Output output = tos.headObject(input);
                    Log.i("headObject", "headObject succeed, object's metadata is " + output.getHeadObjectBasicOutput());
                } catch (TosException e) {
                    if (e.getStatusCode() == 404) {
                        // 下载不存在的对象会返回404
                        Log.e("TosException", "the object you want to head is not found");
                    } else {
                        Log.e("TosException", "headObject failed");
                    }
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于设置对象元数据的 API 文档,请参见 SetObjectMeta