Skip to main content

Storage

uploadFile​

1. Interface Description​

Interface Function: Upload files to the File Management Service

Interface declaration: uploadFile(object: Object): Promise<Object>

2. Input Parameters​

FieldTypeRequiredDescription
cloudPathStringYesThe 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
fileContentfs.ReadStreamYesbuffer or the file to be uploaded Readable stream

3. Response​

FieldTypeRequiredDescription
fileIDstringNoFile unique ID, used to access the file, it is recommended to store it
requestIdstringYesRequest ID, used for error troubleshooting
codestringNoStatus code, not returned if the operation is successful
messagestringNoError 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​

FieldTypeRequiredDescription
objectIGetTempFileURLOptsYesRequest parameters for obtaining download links
optsObjectNoCustom configurations, currently only supports timeout setting, {timeout: number}
IGetTempFileURLOpts​
FieldTypeRequiredDescription
fileList<Array>.string Or fileItemYesArray of file IDs to be downloaded
fileItem​
FieldTypeRequiredDescription
fileIDstringYesFile ID
maxAgenumberYesValidity period of the file link

3. Response​

FieldTypeRequiredDescription
fileList<Array>.fileUrlItemNoArray storing download links
requestIdstringYesRequest ID, used for error troubleshooting
codestringNoStatus code, which is 'SUCCESS' for successful operations
messagestringNoError description
fileUrlItem​
FieldTypeRequiredDescription
codestringNoDeletion result, which is 'SUCCESS' for successful operations
fileIDstringRequiredFile ID
tempFileURLstringRequiredFile 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​

FieldTypeRequiredDescription
objectIDeleteFileOptsRequiredDelete file request parameters
optsObjectNoCustom configurations, currently only supports timeout setting, {timeout: number}
IDeleteFileOpts​
FieldTypeRequiredDescription
fileList<Array>.stringRequiredArray of file IDs to be deleted

3. Response​

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
fileList<Array>.deleteFileItemNoArray of deletion results
requestIdstringYesRequest ID, used for error troubleshooting
deleteFileItem​
FieldTypeRequiredDescription
codestringNoDeletion result, 'SUCCESS' indicates successful operation
fileIDstringRequiredFile 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​

FieldTypeRequiredDescription
objectIDownloadFileOptsRequiredDownload file request parameters
optsObjectNoCustom configurations, currently only supports timeout setting, {timeout: number}
IDownloadFileOpts​
FieldTypeRequiredDescription
fileIDstringRequiredFile id to download
tempFilePathstringOptionalStorage path for the downloaded file

3. Response​

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
fileContentbufferNoContent of the downloaded file. This field is not returned if tempFilePath is provided
requestIdstringYesRequest 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​

FieldTypeRequiredDescription
fileList<Array>.ICopyFileParamRequiredArray of file information to be copied

ICopyFileParam:

FieldTypeRequiredDescription
srcPathstringRequiredThe 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
dstPathstringRequiredThe 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
overwritebooleanOptionalWhether to overwrite existing files when the target file already exists. Defaults to true
removeOriginalbooleanOptionalWhether to delete source files after copying, defaults to false

3. Response​

FieldTypeRequiredDescription
requestIdstringRequiredRequest ID, used for error troubleshooting
fileList<Array>.{fileId?: string,code?: string,message?: string}RequiredList 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})