Integrate HTTPDNS with the Alibaba Cloud OSS SDK on HarmonyOS to improve file transfer stability and prevent DNS hijacking.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a general-purpose cloud storage service for storing and accessing files such as profile pictures, images, audio, and video. On HarmonyOS, you access OSS by integrating the OSS HarmonyOS SDK.
By combining the HTTPDNS SDK with the OSS HarmonyOS SDK, you can resolve OSS domain names through HTTPDNS. This provides anti-hijacking protection, accelerates DNS resolution, and improves the network experience when your application accesses OSS.
2. Integration steps
Integrating HTTPDNS with the OSS SDK on HarmonyOS involves two 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
Create an rcp.Session instance and embed a custom DNS resolution rule that calls the HTTPDNS service to resolve domain names.
For more information about HTTPDNS integration, 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
Pass the rcp.Session instance from the previous step as a configuration parameter when you initialize the OSS client. All network requests from the client then automatically use the HTTPDNS resolution logic.
For more information about 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
HTTPDNS integrates with the OSS SDK on HarmonyOS through an rcp.Session instance that contains a custom DNS resolution rule. This approach optimizes network performance and security.
Key points:
-
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.