Storage
uploadFile
1. Interface Description
Interface Function: Upload files to the File Management Service
Interface declaration: uploadFile(object: Object): Promise<Object>
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
cloudPath | String | Yes | The absolute path of the file, including the file name. For example, foo/bar.jpg, foo/bar/baz.jpg, etc. Must not contain characters other than [0-9, a-z, A-Z], /, !, -, _, ., ,, *, and Chinese. Uses the / character to implement a hierarchical structure similar to traditional file systems. See details |
fileContent | fs.ReadStream | Yes | buffer or the file to be uploaded Readable stream |
3. Response
Field | Type | Required | Description |
---|---|---|---|
fileID | string | No | File unique ID, used to access the file, it is recommended to store it |
requestId | string | Yes | Request ID, used for error troubleshooting |
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description, not returned if the operation is successful |
4. Sample Code
// Initialize
const tcb = require('@cloudbase/node-sdk')
const fs = require('fs')
const app = tcb.init({
env: 'xxx'
})
exports.main = async (event, context) => {
const result = await app.uploadFile({
cloudPath: 'test-admin.jpeg',
fileContent: fs.createReadStream(`${__dirname}/cos.jpeg`)
})
console.log(result.fileID) // Output the file ID
}
getTempFileURL
1. Interface Description
Function: Get file CDN download URL
Interface declaration: getTempFileURL(object: IGetTempFileURLOpts, opts: Object): Promise<Object>
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
object | IGetTempFileURLOpts | Yes | Request parameters for obtaining download links |
opts | Object | No | Custom configurations, currently only supports timeout setting, {timeout: number} |
IGetTempFileURLOpts
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.string Or fileItem | Yes | Array of file IDs to be downloaded |
fileItem
Field | Type | Required | Description |
---|---|---|---|
fileID | string | Yes | File ID |
maxAge | number | Yes | Validity period of the file link |
3. Response
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.fileUrlItem | No | Array storing download links |
requestId | string | Yes | Request ID, used for error troubleshooting |
code | string | No | Status code, which is 'SUCCESS' for successful operations |
message | string | No | Error description |
fileUrlItem
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Deletion result, which is 'SUCCESS' for successful operations |
fileID | string | Required | File ID |
tempFileURL | string | Required | File access link |
4. Sample Code
// Initialize
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
exports.main = async (event, context) => {
const result = await app.getTempFileURL({
fileList: ['cloud://test-28farb/a.png']
})
result.fileList.forEach(item => {
console.log(item.tempFileURL) // Print file access link
})
}
deleteFile
1. Interface Description
Function: Delete file
Interface declaration: deleteFile(object: IDeleteFileOpts, opts: Object): Promise<Object>
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
object | IDeleteFileOpts | Required | Delete file request parameters |
opts | Object | No | Custom configurations, currently only supports timeout setting, {timeout: number} |
IDeleteFileOpts
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.string | Required | Array of file IDs to be deleted |
3. Response
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description |
fileList | <Array>.deleteFileItem | No | Array of deletion results |
requestId | string | Yes | Request ID, used for error troubleshooting |
deleteFileItem
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Deletion result, 'SUCCESS' indicates successful operation |
fileID | string | Required | File ID |
4. Sample Code
// Initialize
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
exports.main = async (event, context) => {
const result = await app.deleteFile({
fileList: ['HHOeahVQ0fRTDsums4GVgMCsF6CE3wb7kmIkZbX+yilTJE4NPSQQW5EYks']
})
result.fileList.forEach(item => {
if (item.code === 'SUCCESS') {
// File deleted successfully
}
})
}
downloadFile
1. Interface Description
Interface Function: Download files locally
Interface declaration: downloadFile(object: IDownloadFileOpts, opts: Object): Promise<Object>
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
object | IDownloadFileOpts | Required | Download file request parameters |
opts | Object | No | Custom configurations, currently only supports timeout setting, {timeout: number} |
IDownloadFileOpts
Field | Type | Required | Description |
---|---|---|---|
fileID | string | Required | File id to download |
tempFilePath | string | Optional | Storage path for the downloaded file |
3. Response
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description |
fileContent | buffer | No | Content of the downloaded file. This field is not returned if tempFilePath is provided |
requestId | string | Yes | Request ID, used for error troubleshooting |
4. Sample Code
// Initialize
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
exports.main = async (event, context) => {
const result = await app.downloadFile({
fileID: 'cloud://aa-99j9f/my-photo.png'
// tempFilePath: '/tmp/test/storage/my-photo.png'
})
// If tempFilePath is not provided, fileContent can be printed; if provided, go to the corresponding directory to view the file
console.log(result.fileContent)
}
copyFile
1. Interface Description
Interface function: Batch copy files
Interface declaration: copyFile({ fileList }: { fileList: ICopyFileParam[] }): Promise<ICopyFileResponse>
Note
This operation will not change the file permissions.
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.ICopyFileParam | Required | Array of file information to be copied |
ICopyFileParam
:
Field | Type | Required | Description |
---|---|---|---|
srcPath | string | Required | The absolute path of the source file, including the file name. Examples: foo/bar.jpg, foo/bar/baz.jpg. Only allowed characters: [0-9, a-z, A-Z], /, !, -, _, ., ,, *, and Chinese. Use / to implement hierarchical structure like traditional file systems |
dstPath | string | Required | The absolute path of the destination file, including the file name. Examples: foo/bar.jpg, foo/bar/baz.jpg. Only allowed characters: [0-9, a-z, A-Z], /, !, -, _, ., ,, *, and Chinese. Use / to implement hierarchical structure like traditional file systems |
overwrite | boolean | Optional | Whether to overwrite existing files when the target file already exists. Defaults to true |
removeOriginal | boolean | Optional | Whether to delete source files after copying, defaults to false |
3. Response
Field | Type | Required | Description |
---|---|---|---|
requestId | string | Required | Request ID, used for error troubleshooting |
fileList | <Array>.{fileId?: string,code?: string,message?: string} | Required | List of request results. If the request succeeds, fileId is the file id (e.g., cloud://xxx.yyy/abc.png); if the request fails, code and message describe the error information |
4. Sample Code
// Initialize the sdk
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx' // Fill in the environment ID
})
const path = 'a.png' // Fill in the source file path
const fileList = [
{
srcPath: path,
dstPath: `dst/${path}`, // Fill in the target file path
removeOriginal: true // Delete source files after copying, equivalent to moving files
}
]
const result = await app.copyFile({fileList})