OSS結合CDN加速服務使用時,會產生CDN流出流量費用、CDN回源流出流量費用。如果您希望在使用OSS SDK時使用回源流量包抵扣相關費用,您需要在初始化SDK時,將Bucket網域名稱替換為已配置CDN加速服務的自訂網域名,並添加CNAME記錄。
以下提供多語言SDK的配置樣本。
重要
請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET,並填寫已配置CDN加速服務的自訂網域名,例如example.com。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
public class Demo {
public static void main(String[] args) throws Exception {
// 填寫已配置CDN加速服務的自訂網域名,例如example.com。
String endpoint = "http://example.com";
// 填寫Endpoint對應的Region資訊,例如cn-hangzhou。
String region = "cn-hangzhou";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 建立OSSClient執行個體。
// 當OSSClient執行個體不再使用時,調用shutdown方法以釋放資源。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// 顯式聲明使用 V4 簽名演算法
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
// 開啟CNAME。CNAME用於將自訂網域名綁定到目標Bucket。
clientBuilderConfiguration.setSupportCname(true);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 建立ClientConfiguration執行個體,您可以根據實際情況修改預設參數。
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
// 開啟CNAME。CNAME用於將自訂網域名綁定到目標Bucket。
conf.setSupportCname(true);
} 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();
}
}
}
}<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// 填寫已配置CDN加速服務的自訂網域名,例如example.com。
$endpoint = "https://example.com";
try {
// 開啟CNAME。CNAME用於將自訂網域名綁定到目標Bucket。
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
} catch (OssException $e) {
print $e->getMessage();
} # -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 填寫已配置CDN加速服務的自訂網域名,例如example.com。
cname = 'http://example.com'
# 通過設定is_cname=True來開啟CNAME,CNAME用於將自訂網域名綁定到目標Bucket。
bucket = oss2.Bucket(auth, cname, 'examplebucket', is_cname=True) using Aliyun.OSS;
using Aliyun.OSS.Common;
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填寫已配置CDN加速服務的自訂網域名,例如example.com。
const string endpoint = "https://example.com";
// 建立ClientConfiguration執行個體,按照實際需求修改預設參數。
var conf = new ClientConfiguration();
// 開啟CNAME,CNAME用於將自訂網域名綁定到目標Bucket。
conf.IsCname = true;
// 建立OssClient執行個體。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf); package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 建立OSSClient執行個體。
// yourEndpoint填寫已配置CDN加速服務的自訂網域名,例如example.com。
client, err := oss.New("https://example.com", "", "", oss.UseCname(true), oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
#include "oss_api.h"
#include "aos_http_io.h"
/* 填寫已配置CDN加速服務的自訂網域名,例如example.com。*/
const char *endpoint = "https://example.com";
void init_options(oss_request_options_t *options) {
options->config = oss_config_create(options->pool);
/* 用char*類型的字串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
/* 開啟CNAME,CNAME用於將自訂網域名綁定到目標Bucket。*/
options->config->is_cname = 1;
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main() {
aos_pool_t *p;
oss_request_options_t *options;
/* 初始化全域變數。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return -1;
}
/* 初始化記憶體池和options。*/
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_options(options);
/* 釋放記憶體池,相當於釋放了請求過程中各資源分派的記憶體。*/
aos_pool_destroy(p);
/* 釋放此前分配的全域資源。*/
aos_http_io_deinitialize();
return 0;
}