tencent cloud

Smart Media Hosting

Product Introduction
Product Overview
Feature Description
Product Strengths
Use Cases
Basic Concept
Purchase Guide
Quick Start
Create Media Library
Initiate request
Service Level API Document
History
Introduction
API Category
Making API Requests
PaaS Service APIs
Official Cloud Disk APIs
Data Types
Error Codes
Business Level API Document
Introduction
Access Token Operation API
Tenant Space Operation API
File Operation API
Directory or Album Operation API
Recycle Bin Operation API
Quota Operation API
Query Task Operation API
Search Operation API
Historical Version Operations API
Directory and File Batch Operation API
Collection Operation API
Error Codes
SDK Documentation
Android SDK
iOS SDK
HarmonyOS SDK
FAQs
Enterprise Network Disk
Product Introduction
Purchase Guide
Quick Start
FAQs
Service Level Agreement
Glossary

Quick Start

PDF
フォーカスモード
フォントサイズ
最終更新日: 2026-01-07 10:49:12

Preparations

You need a HarmonyOS NEXT application, which can be an existing project or a newly created empty one. For details, see HarmonyOS NEXT Application Development Guide.
Ensure your HarmonyOS NEXT application target is API level 12 or higher.

Procedure

Step 1: Integrating the SDK

1. Run this command to install SDK dependencies:
ohpm install @tencentcloud/smh_sdk
This will add a line of dependency code to your project's oh-package.json5:
"dependencies": {
...
"@tencentcloud/smh_sdk":"1.0.2"
}
2. The SDK requires network permission to communicate with the COS server. Please add the following permission statement in the module.json5 file under the application module:
...
"requestPermissions":[
{
"name" : "ohos.permission.INTERNET",
...
}
]
...

Step 2: Initialization

Note:
After a media library is created in the console, the console will display the dedicated domain name generated for you. It is strongly recommended that you set SMHServiceConfig.sharedConfig().host to your dedicated domain name.



// Configure the access domain displayed after creating a media library in the console
SMHServiceConfig.sharedConfig().host = "<libraryId>.api.tencentsmh.cn";

// Configure the access token refresh callback
SMHAccessTokenProvider.singleProvider().accessRefreshCallBack = <T>(reqeust: SMHAPIRequest<T>) => {
return new Promise(async (resolve, reject) => {
try {
let accessToken = new SMHAccessToken();
accessToken.libraryId = "libraryId"; // Set Media Library ID.
accessToken.spaceId = "spaceId"; // Set Space ID
accessToken.accessToken = "accessToken"; //set accessToken
accessToken.expiresIn = 1800;
resolve(accessToken);
} catch (err) {
}
});
}

Step 3: Accessing the SMH Service

Uploading Files

try {
let filePath = "local file path";
// Call the uploadObject method of SMHFileApis to perform file upload
let task = SMHFileApis.uploadObject({
spaceId: "spaceId",
libraryId: "libraryId",
body: filePath, // local path
uploadPath: `smh/test.jpg` // target path
});
task.onStateChange = (state: SMHTransferState) => {
job status callback
}
task.confirmKeyInitCallback = (confimKey: string) => {
// Upload returns confirmKey callback. Used for resumable upload
}
task.onProgress = (progress) => {
// progress callback
}
task.onFinish = (result?: object, error?: SMHError) => {
// complete callback
}
// start task
task.start();

//other methods
//task.pause() pause task
//task.cancel() cancel task
//task.resume() restart task, can be used together with pause
}catch (e) {
// exception handling
}
Resumable Transfer
try {
let filePath = "local file path";
// Call the uploadObject method of SMHFileApis to perform file upload
let task = SMHFileApis.uploadObject({
spaceId: "spaceId",
libraryId: "libraryId",
body: filePath, // local path
uploadPath: `smh/test.jpg`, // target path
confirmKey:"confirmKey"// confirmKey returned by confirmKeyInitCallback.
});
task.onStateChange = (state: SMHTransferState) => {
job status callback
}
task.confirmKeyInitCallback = (confimKey: string) => {
// Upload returns confirmKey callback. Used for resumable upload
}
task.onProgress = (progress) => {
// progress callback
}
task.onFinish = (result?: object, error?: SMHError) => {
// complete callback
}
// start task
task.start();

//other methods
//task.pause() pause task
//task.cancel() cancel task
//task.resume() restart task, can be used together with pause
}catch (e) {
// exception handling
}

Downloading a File

Note:
The download interface supports resume transfer from breakpoints by default. Ensure that the local path for writing remains unchanged.
try {
// Call the downloadObject method of SMHFileApis to perform file upload
let task = SMHFileApis.downloadObject({
spaceId: "spaceId",
libraryId: "libraryId",
filePath: `smh/test.jpg`, // remote path
savePath: "local path" // local path
})
task.onStateChange = (state: SMHTransferState) => {
job status callback
}
task.onProgress = (progress: HttpProgress) => {
// progress callback
}
task.onFinish = (result?: object, error?: SMHError) => {
// complete callback
}
// start task
task.start();
//other methods
//task.pause() pause task
//task.cancel() cancel task
//task.resume() restart task, can be used together with pause
} catch (e) {
// exception handling
}

Other Requests

// listing files in the folder
try {
let result = await SMHDirectoryApis.listDirectory({
libraryId:"libraryId",
spaceId:"spaceId",
dirPath:'',
limit:'10',
orderBy:SMHFileListOrderBy.name,
orderByType:SMHOrderByType.asc,
withFavoriteStatus:true,
withInode:true
});
}catch (e) {
// exception handling
}

// create folder
try {
let result = await SMHDirectoryApis.createDirectory({
libraryId:"libraryId",
spaceId:"spaceId",
dirPath:'path',
withInode:true
});
}catch (e) {
// exception handling
}

// Delete folder
try {
let result = await SMHDirectoryApis.deleteDirectory({
libraryId:"libraryId",
spaceId:"spaceId",
dirPath:'path'});
}catch (e) {
// exception handling
}

Other Configurations

Retry Policy Configuration

// Set retry interval, default: 1s
SMHServiceConfig.sharedConfig().retrySleep = 1 * 1000;
// Set retry count, default: 3
SMHServiceConfig.sharedConfig().maxRetryCount = 3;

Timeout Configuration

// Read timeout period in milliseconds (ms), default is 30 * 1000ms.
SMHServiceConfig.sharedConfig().readTimeout = 30 * 1000;
// Connection timeout in milliseconds (ms), default is 15 * 1000ms.
SMHServiceConfig.sharedConfig().connectTimeout = 15 * 1000;

Concurrent Policy Configuration

// Set maximum number of concurrent uploads. Default: 4
SMHServiceConfig.sharedConfig().setUploadMaxConcurrentCount(4);
// Set maximum number of concurrent downloads. Default: 4
SMHServiceConfig.sharedConfig().setDownloadMaxConcurrentCount(4);

Log configuration

// Custom log output callback for log collection in the business layer
QCloudLogger.logOutputCallback = (log: string) => {
console.log(log);
}
// Want to close logs: true: off, false: on
QCloudLogger.setClose(true);


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック