tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary
DokumentasiCloud Object Storage

Using COS in APICloud

Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-12 16:32:21

Introduction

This article describes how to quickly integrate the COS feature module using APICloud and develop cross-platform applications.

APICloud Related Resources

Prepare

Follow the APICloud tutorial to create an App, search for the cosClient module in the module library, and click to add it.

cosClient Simple Usage

1. Obtain Module Instance

var demo = api.require('cosClient');

2. Configure Key

COS SDK module provides two key configuration methods:

Temporary Key (Recommended)

Implemented in the script tag as follows:
function refreshCredentail() {
Obtain temporary keys secretID, secretKey, token, startDate, expirationDate via a request interface;
// Concatenate in the following format and return it;
return "secretID=***&secretKey=***&token=***&startDate=***&expirationDate=***";
}

Permanent Key

Set the permanent key before registering the service:
var demo = api.require('cosClient');
demo.setupPermanentCredentail({"secretID":secretID,"secretKey":secretKey});

3. Register the COS Service Instance

demo.registerTransferManger({"serviceKey":"test","useHttps":true})

cosClient API Documentation

1. Register Basic Service

Example Code

var demo = api.require('cosClient');
demo.registerServiceForKey({"serviceKey":"test","useHttps":true})

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
COS service instance unique identifier. If not passed, the default service will be registered.
No
appId
String
Your APPID
No
region
String
Name of the service region. Enter the abbreviation of the COS region here, such as ap-beijing. For more information about regions, see Regions and Access Domains
No
isPrefixURL
Boolean
-
No
timeOut
Integer
Timeout Time
No
serviceName
String
Base name of the service. The default value is: myqcloud.com.
No
suffix
String
Custom domain: http://bucketname.suffix If this parameter is not specified, the bucket host is http://bucketname.**. If this parameter is specified as testsuffix, the bucket host is http://bucketname.testsuffix.**. This takes effect on the iOS end.
No
useHttps
Boolean
Whether to use https
No
userAgent
String
Set custom ua
No
host
String
Custom domain, excluding "http://", takes effect on the Android client.
No
port
Integer
Custom port, takes effect on the Android client.
No
Return parameters None

2. Register Transmission Service

Example Code

var demo = api.require('cosClient');
demo.registerTransferManger({"serviceKey":"test","useHttps":true})

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
The unique identifier of the COS service instance. If not passed, the default service will be registered.
No
appId
String
Your APPID.
APPID is the account you obtain after successfully applying for a Tencent Cloud account, automatically assigned by the system, is fixed and unique, and can be viewed in account information. The APPID of a Tencent Cloud account is an application ID that has a unique correspondence with the account ID.
No
region
String
Name of the service region. Enter the abbreviation of the COS region here, such as ap-beijing. For more information about regions, see Regions and Access Domains
No
isPrefixURL
Boolean
Default YES. Controls the position of the bucket name in the URL during URL concatenation.
Example:
When NO: https://ap-chengdu.cos.myqcloud.com/examplebucket-1250000000
When YES: https://examplebucket-1250000000.cos.ap-chengdu.myqcloud.com
No
timeOut
Integer
Timeout Time
No
serviceName
String
Base name of the service. The default value is: myqcloud.com.
No
suffix
String
Custom domain: http://bucketname.suffix When this parameter is not specified, the bucket host is http://bucketname.**. When this parameter is specified as testsuffix, the bucket host is http://bucketname.testsuffix.**. Takes effect on iOS.
No
useHttps
Boolean
Whether to use https
No
userAgent
String
Set custom ua
No
host
String
Custom domain, excluding "http://", takes effect on the Android client.
No
port
Integer
Custom port, takes effect on the Android client.
No
Return parameters None

3. Obtain Bucket List

Example Code

/// Module name cosClient, use the module name to obtain the module instance.
var demo = api.require('cosClient');
demo.getBucketList(
{"serviceKey":"test"}
,function(ret,err){
/// When err is empty, return "".
if(err != ""){
alert(err.data);
}else{
alert(ret.data);
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
Response Parameters
{
"result":"success",
"data":{
buckets = [ /// Bucket list
{
createDate = "2021-06-02T07:33:33Z"; /// Creation time
location = "ap-chengdu"; /// Region
name = "examplebucket-1250000000"; /// Bucket name
type = ""; /// Bucket type
}];
owner = {
displayName = 10000000001; /// Owner's name
id = "qcs::cam::uin/10000000001:uin/10000000001"; /// Owner ID
};
}
}


4. Delete Bucket

Note:
After a bucket is deleted, the corresponding data will no longer be accessible.

Example Code

/// Module name cosClient, use the module name to obtain the module instance.
var demo = api.require('cosClient');
demo.deleteBucket({
"serviceKey":"test",
"region":"ap-chengdu",
"bucket":"0-appid",},
function(ret,err){
/// Returns "" when err is null.
if(err != ""){
alert(err.data);
}else{
alert(ret.data);
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
Response Parameters
The cluster is deleted.
{
"result":"success"
}
The deletion failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

5. Create a Bucket

Example Code

/// Module name cosClient, use the module name to obtain the module instance.
var demo = api.require('cosClient');
demo.createBucket({
"serviceKey":"test",
"name":"apicloudtest",
"appId":"appId",
"region":"ap-chengdu"},function(ret,err){
/// Returns "" when err is null.
if(err != ""){
alert(err.data);
}else{
alert(ret.data);
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
name
String
Bucket name does not contain APPID.
Yes
appId
String
User AppID.
Yes
region
String
Bucket region.
Yes
accessControlList
String
Defines the acl attribute of the bucket. Valid values: private, public-read-write, public-read; Default value: private.
No
readAccount
String
Grant read permission to the grantee, id="OwnerUin";
No
writeAccount
String
Grant write permission to the grantee. Format: id="OwnerUin";
No
readWriteAccount
String
Grant read-write permission to the grantee. Format: id="OwnerUin";
No
enableMAZ
Boolean
Whether to use multi-AZ. Default is no.
No
Response Parameters
The subnet is created.
{
"result":"success"
}
Creation failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

6. Obtain Bucket Content

Example Code

var demo = api.require('cosClient');
demo.listBucketContent({"serviceKey":"test","bucket":"0-appid","region":"ap-chengdu"},function(ret,err){
/// Returns "" when err is null.
if(err != ""){
alert(err.data);
}else{
alert(ret.data);
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
prefix
String
Prefix matching, used to specify the file prefix address to be returned.
No
delimiter
String
The delimiter is a symbol. If the Prefix is specified, the identical paths between the Prefix and the delimiter are grouped into a category and defined as a Common Prefix, and all Common Prefixes are listed. If the Prefix is not specified, identical paths from the beginning to the delimiter are grouped and defined as a Common Prefix.
No
marker
String
By default, entries are listed in UTF-8 binary order, with all entries starting from the marker.
No
maxKeys
String
Maximum number of entries returned per request, default: 1000
No
Response Parameters
Obtained successfully
{
"result":"success",
"data":{
"Contents":[
{
"ETag":"\\"c57b7cd3647561480b355a92dc2e971a\\"", // MD5 digest computed based on the object content, such as "22ca88419e2ed4721c23807c678adbe4c08a7880". Note that the value is enclosed in double quotes.
"Owner":{
"ID":"1250000000", // Complete ID of the object owner, in the format qcs::cam::uin/[OwnerUin]:uin/[OwnerUin], such as qcs::cam::uin/100000000001:uin/100000000001, where 100000000001 is the uin
"DisplayName":"1250000000" // name of the object owner
},
"StorageClass":"STANDARD", // Storage type is STANDARD
"Key":"02_Common Syntax.pdf", // File name
"LastModified":"2023-01-09T11:15:25.000Z", // Last modified time
"Size":816838 // File size
}
],
"MaxKeys":1000,
"IsTruncated":"False", // whether the response entries are truncated, with values 'true' or 'false'
"CommonPrefixes":[ // groups the identical paths between the Prefix and the delimiter into a category and defines them as Common Prefix
{
"Prefix":"1\\/" // the prefix of a single Common Prefix
}
],
"Delimiter":"\\/", // delimiter
"Name":"examplebucket-1250000000" // Name of the bucket, in the format BucketName-APPID, such as examplebucket-1250000000
}
}
Obtain failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

7. Upload File

Example Code

var uploadTaskId;
var demo = api.require('cosClient');
demo.uploadObject({
"serviceKey":"test",
"region":"ap-chengdu",
"bucket":"0-appid",
"filePath":"file://test.gif",
"object":"test.gif"},function(ret,err){
if(err != ""){
alert(err.data);
}else{
if(ret.result == "begin"){
// At begin, obtain the taskId to cancel the task.
uploadTaskId = JSON.parse(ret.data).taskId;
alert(uploadTaskId);
}
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
object
String
The file name uploaded to the server-side.
Yes
filePath
String
Local file path
Yes
uploadId
String
No need to set during the initial file upload; must be specified when resuming the upload.
No
storageClass
String
Object storage class
No
trafficLimit
String
The value of the speed limit for traffic control during this object upload must be a numeric value, with the default unit being bit/s. The value of the speed limit must be within the range of 819200 - 838860800 (that is, 800 Kb/s - 800 Mb/s). If the range is exceeded, a 400 error will be returned.
No
enableVerification
String
Whether to verify the MD5 of the file returned by COS against the locally calculated MD5 after the upload is completed. Enabled by default. If verification fails, the file will still be uploaded to COS, but an error caused by verification failure will be thrown locally.
No
Response Parameters
Upload Initialization Phase
{
"result":"begin",
"data":{
"taskId":taskId, // Current task ID, which can be used to cancel the task.
"uploadId":uploadId // Current upload ID for resuming the upload.
}
}
Upload Phase
{
"result":"processing",
"data":{
"totalBytesSent":totalBytesSent, // Current transfer progress.
"totalBytesExpectedToSend":totalBytesExpectedToSend // Total file size.
}
}
Complete
The upload is successful.
{
"result":"success",
"data":{} // File information
}
Upload failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

8. Pause Upload

Example Code

var demo = api.require('cosClient');
// uploadTaskId is obtained from the upload interface and persisted by the business logic.
demo.pauseUploadObject({"taskId":uploadTaskId});

Request Parameter

Parameter Name
Type
Description
Required
taskId
String
Being obtained from the upload interface, it is persisted by the business logic to be used to cancel the current task.
No

9. Download File

Example Code

var downloadTaskId;
var demo = api.require('cosClient');
demo.registerTransferManger({"serviceKey":"test","useHttps":true})
demo.downloadObject({
"serviceKey":"test",
"region":"ap-chengdu",
"bucket":"0-appid",
"localPath":"file://test.gif",
"object":"test.gif"},function(ret,err){
if(err != ""){
alert(err.data);
}else{
if(ret.result == "begin"){
// At begin, obtain the taskId to cancel the task.
downloadTaskId = JSON.parse(ret.data).taskId;
alert(downloadTaskId);
}
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
The unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
object
String
Server-side file name
Yes
versionId
String
File version number
No
localPath
String
Local file path
Yes
trafficLimit
String
The value of the speed limit for traffic control during this object download must be a numeric value, with the default unit being bit/s. The configurable range is 819200 - 838860800 (equivalent to 800Kb/s - 800Mb/s). Requests exceeding this range will return a 400 error.
No
enableVerification
String
Whether to verify the file MD5 returned by COS against the locally calculated MD5 after download completion. Enabled by default; if verification fails, a validation error will be thrown.
No
Response Parameters
Download Initialization Phase
{
"result":"begin",
"data":{
"taskId":taskId, // Current task ID, which can be used to cancel the task.
}
}
Download phase
{
"result":"processing",
"data":{
"totalBytesDownload":totalBytesDownload, // Current transfer progress.
"totalBytesExpectedToDownload":totalBytesExpectedToDownload // Total file size.
}
}
Complete
The statistics are downloaded successfully.
{
"result":"success",
"data":{} // File information
}
Download failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

10. Pause Download

Example Code

var demo = api.require('cosClient');
// downloadTaskId is obtained from the download interface and persisted by the service.
demo.pauseDownloadObject({"taskId":downloadTaskId});

Request Parameter

Parameter Name
Type
Description
Required
taskId
String
Obtained from the download interface, persisted by the service, and used to cancel the current task.
No

11. Obtain File Information

Example Code

var demo = api.require('cosClient');
demo.headObject({
"serviceKey":"test",
"region":"ap-chengdu",
"bucket":"0-appid",
"object":"test.jpg"},function(ret,err){
if(err != ""){
alert(err.data);
}else{
alert(ret.data);
}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
object
String
Server-side file name
Yes
versionId
String
File version number
No
Response Parameters
Obtained successfully
{
"result":"success",
"data":{} // File object information
}
Obtain failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

12. Deleting Files

Note:
After a file is deleted, its corresponding data will no longer be accessible.

Example Code

var demo = api.require('cosClient');
demo.deleteObject({
"serviceKey":"test",
"region":"ap-chengdu",
"bucket":"examplebucket-1250000000",
"object":"test.jpg"},function(ret,err){
if(err != ""){
alert(err.data);
}else{
alert(ret.result);

}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance. If not passed, the default service instance is used for network requests.
No
region
String
Bucket region.
Yes
bucket
String
Bucket name in the format of BucketName-APPID
Yes
object
String
Server-side file name
Yes
versionId
String
File version number
No
Response Parameters
The cluster is deleted.
{
"result":"success",
}
The deletion failed and an error message is returned.
{
"result":"error",
"error":{} /// Error message
}

13. Cancel All Request Tasks

Example Code

var demo = api.require('cosClient');
demo.cancelAll({
"serviceKey":"test"
},function(ret,err){
if(err != ""){
alert(err.data);
}else{
alert(ret.result);

}
});

Parameter Description

Request parameters.
Parameter Name
Type
Description
Required
serviceKey
String
Unique identifier of the COS service instance, canceling network requests initiated by the corresponding service instance
No
Response Parameters
N/A

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan