Skip to main content

File Storage

uploadFile

1. Interface Description

Interface Function: Upload files to the File Management Service

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

Note

After version 1.0.1, to improve file upload performance, the file upload method has been modified to direct upload to object storage. To prevent CORS errors during usage, you need to log in to the CloudBase Console and add domains in the Security Domains section on the Security Sources page. If existing domains encounter CORS errors, please delete them from the security domains and add them again.

2. Input Parameters

FieldTypeRequiredDescription
cloudPathstringYesAbsolute path of the file, including the file name
filePathHTML upload fileYesFile object to be uploaded
onUploadProgressfunctionNoUpload progress callback
Note

cloudPathis the absolute path of the file, including the file name, e.g., 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](https://cloud.tencent.com/document/product/436/13324)

3. Output Parameters

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
fileIDfileIDYesFile unique ID, used to access the file, it is recommended to store it
requestIdstringNoRequest ID, used for error troubleshooting

4. Sample Code

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: "xxxx-yyy"
});

app
.uploadFile({
cloudPath: "test-admin.jpeg",
filePath: document.getElementById("file").files[0],
onUploadProgress: function (progressEvent) {
console.log(progressEvent);
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
})
.then((result) => {
// Upload result
});

getTempFileURL

1. Interface Description

Function: Get file CDN download URL

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

2. Input Parameters

FieldTypeRequiredDescription
fileList<Array>.stringYesArray of file IDs to be downloaded
fileList
FieldTypeRequiredDescription
fileIDstringYesFile ID
maxAgeIntegerYesValidity period of the file link

3. Output Parameters

FieldTypeRequiredDescription
codestringNoStatus code, which is 'SUCCESS' for successful operations
messagestringNoError description
fileList<Array>.objectNoArray storing download links
requestIdstringNoRequest ID, used for error troubleshooting
fileList
FieldTypeRequiredDescription
codestringNoDeletion result, which is 'SUCCESS' for successful operations
messagestringNoError description
fileIDstringRequiredFile ID
tempFileURLstringRequiredFile access link

4. Sample Code

//Initialize the SDK instance, see code above
app
.getTempFileURL({
fileList: ["cloud://jimmytest-088bef.jimmytest-088bef-1251059088/a|b test.jpeg"]
})
.then((res) => {
res.fileList.forEach((el) => {
if (el.code === "SUCCESS") {
console.log(el.tempFileURL);
} else {
// Failed to get the download URL
}
});
});

deleteFile

1. Interface Description

Function: Delete file

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

2. Input Parameters

FieldTypeRequiredDescription
fileList<Array>.stringRequiredArray of file IDs to be deleted

3. Output Parameters

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
fileList<Array>.objectNoArray of deletion results
requestIdstringNoRequest ID, used for error troubleshooting

fileList

FieldTypeRequiredDescription
codestringNoDeletion result, 'SUCCESS' indicates successful operation
fileIDstringRequiredFile ID

4. Sample Code

app
.deleteFile({
fileList: ["cloud://jimmytest-088bef/1534576354877.jpg"]
})
.then((res) => {
res.fileList.forEach((el) => {
if (el.code === "SUCCESS") {
// Deleted successfully
} else {
}
});
});

downloadFile

1. Interface Description

Interface Function: Download files locally

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

2. Input Parameters

FieldTypeRequiredDescription
fileIDstringRequiredFile id to download

3. Output Parameters

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
requestIdstringNoRequest ID, used for error troubleshooting

4. Sample Code

cloudbase
.downloadFile({
fileID: "cloud://aa-99j9f/my-photo.png"
})
.then((res) => {});