This document describes how to integrate HTTPDNS with the Alibaba Cloud OSS SDK on a HarmonyOS client.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a universal cloud storage service that allows developers to easily store and access files, such as profile pictures, images, audio, and video, in various applications. To use OSS on HarmonyOS, integrate the OSS HarmonyOS SDK.
To improve the stability and speed of file transfers, you can combine the HTTPDNS SDK with the OSS HarmonyOS SDK to resolve OSS domain names. This combination provides effective anti-hijacking protection and accelerates DNS resolution. This enhances the network experience when your application accesses OSS.
2. Integration steps
To integrate HTTPDNS with the OSS SDK on HarmonyOS, follow these two major steps:
Create an
rcp.Sessionthat integrates the HTTPDNS resolution capability.Use this custom session to initialize the OSS client.
2.1 Create a session that integrates HTTPDNS
First, create an rcp.Session instance. Then, embed a custom DNS resolution rule in its configuration. This rule calls the HTTPDNS service to resolve domain names.
For more information about the HTTPDNS integration process, see HarmonyOS SDK Integration.
import { httpdns } from '@aliyun/httpdns';
import { rcp } from '@kit.RemoteCommunicationKit';
/**
* Creates a network session (rcp.Session) that integrates the Alibaba Cloud HTTPDNS resolution capability.
*
* @param httpdnsAccountId Your Alibaba Cloud HTTPDNS AccountId.
* @returns An rcp.Session instance configured with the DNS resolution rule.
*/
async function createHttpDnsEnhancedSession(httpdnsAccountId: string): Promise<rcp.Session> {
const httpdnsService = await httpdns.getService(httpdnsAccountId);
const sessionConfig: rcp.SessionConfiguration = {
requestConfiguration: {
dns: {
dnsRules: (host: string): string[] => {
try {
const result = httpdnsService.getHttpDnsResultSyncNonBlocking(host);
let addresses: string[] = [];
if (result.ipv4s && result.ipv4s.length > 0) {
addresses.push(...result.ipv4s);
}
if (result.ipv6s && result.ipv6s.length > 0) {
addresses.push(...result.ipv6s);
}
if (addresses.length > 0) {
return addresses; // Return the merged list of IP addresses.
}
} catch (error) {
console.error(`[HttpDNS] Parsing failed: ${host}`, error);
}
// If resolution fails or returns no result, return an empty array to fall back to the system's default DNS.
return [];
}
}
}
};
return rcp.createSession(sessionConfig);
}
2.2 Initialize the OSS client with the session
Next, pass the rcp.Session instance that you created in the previous step as a configuration parameter when you initialize the OSS client. This ensures that all network requests from this client automatically use the HTTPDNS resolution logic that you defined.
For more information about how to integrate and use the OSS SDK, see OSS Harmony SDK (Preview).
import Client from '@aliyun/oss';
// The createHttpDnsEnhancedSession function is omitted.
/**
* Demonstrates how to initialize an OSS client that uses an HTTPDNS-enhanced session.
*/
async function initializeOssClientWithHttpDns() {
const YOUR_HTTPDNS_ACCOUNT_ID = 'your_httpdns_account_id'; // Replace with your AccountId.
const YOUR_ACCESS_KEY_ID = 'your_access_key_id'; // Replace with your AccessKey ID.
const YOUR_ACCESS_KEY_SECRET = 'your_access_key_secret'; // Replace with your AccessKey secret.
const YOUR_REGION = 'oss-cn-hangzhou'; // Replace with your region.
// Call the function from Step 1 to create a session that integrates HTTPDNS.
const httpDnsSession = await createHttpDnsEnhancedSession(YOUR_HTTPDNS_ACCOUNT_ID);
// Initialize the OSS client and pass the created session instance to it.
const ossClient = new Client({
accessKeyId: YOUR_ACCESS_KEY_ID,
accessKeySecret: YOUR_ACCESS_KEY_SECRET,
region: YOUR_REGION,
session: httpDnsSession, // Core: Pass the custom session.
});
console.log('OSS Client with HttpDNS initialized successfully!');
// This ossClient instance has successfully integrated HTTPDNS.
// Network requests for subsequent operations, such as uploads and downloads, will automatically be resolved through HTTPDNS.
// await ossClient.putObject(...);
return ossClient;
}
The HTTPDNS SDK must be initialized before the OSS SDK.
3. Summary
You can combine HTTPDNS with the OSS SDK on HarmonyOS by providing the OSS client with an rcp.Session instance that contains a custom DNS resolution rule. This method optimizes network performance and security.
Note the following key points during integration:
Initialization order: Initialize the HTTPDNS service before you initialize the OSS client.
Fallback handling: In the
dnsRulescallback, return an empty array ([]) if HTTPDNS resolution fails or returns no result. This enables a fallback to the system DNS.