You need to enable JavaScript to run this app.
导航

预签名机制(Android SDK)

最近更新时间2024.02.04 18:31:00

首次发布时间2022.12.01 16:31:39

SDK 也支持构造带签名的 URL,您可以直接用该 URL 发起 HTTP 请求,也可以将该 URL 共享给第三方实现访问授权。

普通预签名

下面给出使用预签名的 URL 下载对象的示例。

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.comm.HttpMethod;

import java.time.Duration;

public class PreSignedUrlGetObjectExample 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() {
                String signed = tos.preSignedURL(HttpMethod.GET, bucketName, objectKey, Duration.ofHours(1));
                Log.i("preSignedURL", "generated url is " + signed);
            }
        });

        tosThread.start();
    }
}