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

对象小于 1GiB 时,您可以使用普通拷贝。本文介绍普通拷贝的示例代码。

示例代码

以下代码用于通过 copyObject 拷贝 srcBucketName 桶中 srcObjectKey 对象到 bucketName 桶中,并设置对象名为 objectKey。

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

public class CopyObjectExample 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 srcBucketName = "your src bucket name";
        String srcObjectKey = "your src object key";
        String bucketName = "your bucket name";
        String objectKey = "your object key";
        String securityToken = "your security token";
        
        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{
                    CopyObjectV2Input input = new CopyObjectV2Input().setBucket(bucketName).setKey(objectKey)
                            .setSrcBucket(srcBucketName).setSrcKey(srcObjectKey);
                    CopyObjectV2Output output = tos.copyObject(input);
                    Log.i("copyObject", "copyObject succeed, object's etag is " + output.getEtag());
                    Log.i("copyObject", "copyObject succeed, object's crc64 is " + output.getHashCrc64ecma());
                } catch (TosException e) {
                    Log.e("TosException", "copyObject failed");
                    e.printStackTrace();
                }
            }
        });

        tosThread.start();
    }
}

相关文档

关于普通拷贝的 API 文档,请参见 CopyObject