全部產品
Search
文件中心

Object Storage Service:安全傳輸層協議(Java SDK V1)

更新時間:Nov 26, 2025

本文介紹如何使用Java SDK為Bucket設定安全傳輸層協議。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見地區和Endpoint

  • 本文以從環境變數讀取存取憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證

  • 本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見常見情境配置樣本

範例程式碼

以下樣本展示調用PutBucketHttpsConfig介面為Bucket設定TLS版本,並使用GetBucketHttpsConfig介面擷取Bucket的TLS版本資訊。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetBucketHttpsConfigResult;
import com.aliyun.oss.model.PutBucketHttpsConfigRequest;

import java.util.ArrayList;
import java.util.List;

public class PutBucketHttpsConfig {
    public static void main(String[] args) throws Exception {
        // Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填寫Bucket名稱,例如examplebucket。
        String bucketName = "examplebucket";
        // 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
        String region = "cn-hangzhou";

        // 建立OSSClient執行個體。
        // 當OSSClient執行個體不再使用時,調用shutdown方法以釋放資源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // 調用PutBucketHttpsConfig介面為Bucket設定TLS版本。
            List<String> tlsVersion = new ArrayList<String>();
            tlsVersion.add("TLSv1.2");
            tlsVersion.add("TLSv1.3");

            PutBucketHttpsConfigRequest request = new PutBucketHttpsConfigRequest(bucketName)
                    .withEnabled(true)
                    .withTlsVersion(tlsVersion);

            ossClient.putBucketHttpsConfig(request);

            // 調用GetBucketHttpsConfig介面擷取Bucket的TLS版本資訊。
            GetBucketHttpsConfigResult result = ossClient.getBucketHttpsConfig(bucketName);
            System.out.println("Enable:" + result.isEnable());
            System.out.println("TLSVersion:" + result.getTlsVersion().get(0));
            System.out.println("TLSVersion:" + result.getTlsVersion().get(1));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

相關文檔