OSS结合CDN加速服务使用时,会产生CDN流出流量费用、CDN回源流出流量费用。如果您希望在使用OSS SDK时,可以使用回源流量包抵扣相关费用,您需要在初始化SDK时,将Bucket域名替换为已配置CDN加速服务的自定义域名,并添加CNAME记录。
以下提供多语言SDK的配置示例:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// 填写已配置CDN加速服务的自定义域名,例如example.com。
String endpoint = "http://example.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
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;
}
文档内容是否对您有帮助?