简介
本文档提供关于如何使用自定义 DNS 请求 COS 服务。
设置动态自定义 DNS 回调
更推荐此方式,因为更灵活,更方便业务层控制。
示例代码
// 一般在初始化密钥后进行设置await Cos().initCustomerDNSFetch(FetchDns());import 'package:flutter/foundation.dart';import 'package:tencentcloud_cos_sdk_plugin/fetch_dns.dart';// 实现IFetchDns回调接口class FetchDns implements IFetchDns{static Map<String, List<String>> dnsMap = {'service.cos.myqcloud.com': ["106.119.174.56", "106.119.174.57", "106.119.174.55"],'cos.ap-guangzhou.myqcloud.com': ["27.155.119.179", "27.155.119.180", "27.155.119.166", "27.155.119.181"],};@overrideFuture<List<String>?> fetchDns(String domain) async {// 通过endsWith进行筛选,可以适配更多子域名,例如 000000-1253960454.cos.ap-guangzhou.myqcloud.com也可以适配到cos.ap-guangzhou.myqcloud.comfinal matchedEntries = dnsMap.entries.where((entry) => domain.endsWith(entry.key));for (var entry in matchedEntries) {if (kDebugMode) {print('Host: ${entry.key}, IPS: ${entry.value}');}return entry.value;}return null;}}
设置静态自定义 DNS 配置
以下代码展示了如何设置静态自定义 DNS 配置。
示例代码
Map<String, List<String>> dnsMap = {'service.cos.myqcloud.com': ["106.119.174.56", "106.119.174.57", "106.119.174.55"],'000000-1253960454.cos.ap-guangzhou.myqcloud.com': ["27.155.119.179", "27.155.119.180", "27.155.119.166", "27.155.119.181"],'cos.ap-guangzhou.myqcloud.com': ["27.155.119.179", "27.155.119.180", "27.155.119.166", "27.155.119.181"],};// 一般在初始化密钥后进行设置await Cos().initCustomerDNS(dnsMap);