File Storage
uploadFile
1. Interface Description
Interface Function: Upload files to the File Management Service
Interface declaration: uploadFile(object: Object): Promise<Object>
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
Field | Type | Required | Description |
---|---|---|---|
cloudPath | string | Yes | Absolute path of the file, including the file name |
filePath | HTML upload file | Yes | File object to be uploaded |
onUploadProgress | function | No | Upload progress callback |
cloudPath
is 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
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description |
fileID | fileID | Yes | File unique ID, used to access the file, it is recommended to store it |
requestId | string | No | Request 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
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.string | Yes | Array of file IDs to be downloaded |
fileList
Field | Type | Required | Description |
---|---|---|---|
fileID | string | Yes | File ID |
maxAge | Integer | Yes | Validity period of the file link |
3. Output Parameters
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, which is 'SUCCESS' for successful operations |
message | string | No | Error description |
fileList | <Array>.object | No | Array storing download links |
requestId | string | No | Request ID, used for error troubleshooting |
fileList
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Deletion result, which is 'SUCCESS' for successful operations |
message | string | No | Error description |
fileID | string | Required | File ID |
tempFileURL | string | Required | File 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
Field | Type | Required | Description |
---|---|---|---|
fileList | <Array>.string | Required | Array of file IDs to be deleted |
3. Output Parameters
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description |
fileList | <Array>.object | No | Array of deletion results |
requestId | string | No | Request ID, used for error troubleshooting |
fileList
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Deletion result, 'SUCCESS' indicates successful operation |
fileID | string | Required | File 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
Field | Type | Required | Description |
---|---|---|---|
fileID | string | Required | File id to download |
3. Output Parameters
Field | Type | Required | Description |
---|---|---|---|
code | string | No | Status code, not returned if the operation is successful |
message | string | No | Error description |
requestId | string | No | Request ID, used for error troubleshooting |
4. Sample Code
cloudbase
.downloadFile({
fileID: "cloud://aa-99j9f/my-photo.png"
})
.then((res) => {});