All Products
Search
Document Center

Drive and Photo Service:Use the PDSClient class

Last Updated:Dec 22, 2025

This topic describes the PDSClient class, including its construction method, public methods, and event listeners.

PDSClient construction method

Creates a PDSClient instance that can call all the API operations of Photo and Drive Service (PDS).

const client = new PDSClient(config, context);

Request parameters

Parameter

Type

Required

Description

config

IClientParams

Yes

For more information, see the IClientParams section of this topic.

context

IContext

The context of the runtime environment. For more information, see the IContext section of this topic.

IClientParams

Parameter

Type

Required

Description

token_info

TokenInfo

The information about the access token that is obtained in the OAuth access process or is issued by a JSON Web Token (JWT) application.

share_token

string

The share token.

api_endpoint

string

Y1

The endpoint of the PDS API. In most cases, the format is https://{domainId}.api.aliyunpds.com.

path_type

string

The data storage mode of the domain. Only StandardMode is supported. Default value: StandardMode.

version

string

The version number. Only v2 is supported. Default value: v2.

refresh_token_fun

Function

The callback method to invoke when the access token expires. This method returns the Promise<TokenInfo> parameter.

refresh_share_token_fun

Function

The callback method to invoke when the share token expires. This method returns the Promise<string> parameter, which contains a new share token.

  • You must specify one of the api_endpoint and auth_endpoint parameters.

TokenInfo

Parameter

Type

Required

Description

access_token

string

Yes

The access token that represents the identity of the user. In most cases, the access token is valid for 2 hours.

refresh_token

string

The refresh token, which is used to refresh the access token that has expired.

expire_time

string

The expiration time of the access token. In most cases, the expiration time of the access token is 2 hours later than the time when the access token is issued.

expires_in

string

The validity period of the access token. Unit: seconds. Example: 7200.

token_type

string

The type of the access token. Default value: Bearer.

user_id

string

The unique ID of the current user.

role

string

The role that is assumed by the current user.

Other parameters

For more information, see Token.

IContext

Parameter

Type

Required

Description

isNode

boolean

Yes

Specifies whether the environment is a Node.js environment.

Axios

object

Yes

For more information, see axios - npm.

AxiosNodeAdapter

object

The AxiosNodeAdapter parameter in Context that is exported from the SDK for JavaScript in the Node.js environment.

platform

string

The process platform in the Node.js environment.

Valid values: win32, darwin, and linux.

os

object

The native os module in the Node.js environment.

fs

object

The native fs module in the Node.js environment.

path

object

The native path module in the Node.js environment.

cp

object

The native child_process module in the Node.js environment.

http

object

The native http module in the Node.js environment.

https

object

The native https module in the Node.js environment.

crypto

object

The native crypto module in the Node.js environment.

worker_threads

object

The native worker_threads module in the Node.js environment.

Obtain an access token

You can obtain an access token in multiple ways based on the application type. For more information, see Application overview.

Refresh an access token

An access token is valid for 2 hours, and a refresh token is valid for seven days. During the validity period of a refresh token, you can refresh an access token regardless of whether the access token expires.

After you refresh the access token, the old access token and refresh token become invalid.

Procedure

  • When you initialize a PDSClient instance, specify the refresh_token_fun parameter.

const client = new PDSClient({
   refresh_token_fun: async ()=>{
     // Call the backend API to refresh the access token.  my_refresh_token_fun is a custom method used to refresh an access token. 
     const token_info = await my_refresh_token_fun()
     return token_info
   }
});

  • For information about how to encapsulate a method that is used to refresh an access token, see Token.

Basic public methods

setToken

Specifies an access token. If you do not specify the token_info parameter when you call the construction method, you can call this method to specify the parameter.

Make sure that the token_info parameter in the current PDSClient instance is valid before you call this method.

client.setToken(token_info)

Request parameters

Parameter

Type

Required

Description

token_info

TokenInfo

Yes

The information about the access token.

Response parameters

None

setShareToken

Specifies a share token when you call share-related methods.

If you do not specify the share_token parameter when you call the construction method, you can call this method to specify the parameter.

Make sure that the share_token parameter in the current PDSClient instance is valid before you call this method.

client.setShareToken(share_token)

Request parameters

Parameter

Type

Required

Description

share_token

string

Yes

The share token.

Response parameters

None

postAPI

Calls the basic API operations of PDS. Based on the postAPI method, you can encapsulate methods to call all the API operations of PDS.

const result = await client.postAPI(pathname, data={}, options={})
Note

The options parameter of the IPDSRequestConfig type is encapsulated based on the request config options of axios. For more information, see AxiosRequestConfig.

Request parameters

Parameter

Type

Required

Description

pathname

string

Yes

The path to the API operation that you want to call. If you want to call the /v2/file/list operation, set this parameter to /file/list.

data

object

The JSON-formatted data used to call the API operation specified by the pathname parameter.

options

IPDSRequestConfig

Other request parameters.

Response parameters

Parameter

Type

Required

Description

result

object

The returned data.

Sample code

let {items =[]} = await client.postAPI('/file/list', {
  drive_id: '1050', 
  parent_file_id: 'root',
})
console.log(items)

Event listeners

You can listen to only error events.

error event

client.on('error', (error, req_opt)=> {
   console.log('Error',  error)
   console.log('Request Options:', req_opt)
})

Callback parameters

Parameter

Type

Required

Description

error

PDSError

Yes

The error message. This parameter is of the PDSError type and generally contains the following parameters: code, message, reqId, and status.

req_opt

IPDSRequestConfig

The request parameters.

PDSError

Parameter

Type

Required

Description

status

number

The HTTP status code that is returned if the server reports an error. This parameter is left empty if the client reports an error.

code

string

The code that is returned if the server reports an error. A value of ClientError is returned if the client reports an error.

message

string

Yes

The message that is returned if the server reports an error.

reqId

string

The request ID that is returned if the server reports an error. This parameter is left empty if the client reports an error.

stack

string

The error stack that is used for debugging.

type

string

The type of the error. Valid values: ClientError and ServerError.