File Storage
uploadFile
1. Interface Description
Interface Function: File Upload
Interface Declaration:
2.x version
uploadFile(options)
1.x version
uploadFile(localPath: string, cloudPath: string)
Note: v2.x and v1.x are incompatible
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
localPath | Required | String | Local file path. It is recommended to pass an absolute path |
cloudPath | Required | String | Cloud file path: dir/data.txt |
onProgress | Required | Function | Upload progress callback function |
Note: When localPath is a folder, the SDK will attempt to look for a file with the same name as specified in cloudPath under this folder (first-level directory without deep traversal). For example, if cloudPath is dir/data.txt
, it will look for 'data.txt'.
3. Response
None
4. Sample Code
import path from "path";
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.uploadFile({
localPath: path.resolve("./data.txt"),
cloudPath: "files/data.txt",
onProgress: (data) => {},
});
}
test();
uploadDirectory
1. Interface Description
Interface Function: Folder Upload
Interface Declaration:
2.x version
uploadDirectory(options)
1.x version
uploadDirectory(source: string, cloudPath: string)
Note: v2.x and v1.x are incompatible
This interface will traverse all files in the target folder and upload them while preserving the folder structure.
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
localPath | Yes | String | Local folder path |
cloudPath | Yes | String | Cloud folder path |
onProgress | Required | Function | Upload progress callback function |
3. Response
None
4. Sample Code
import path from "path";
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.uploadDirectory({
localPath: path.resolve("./files"),
cloudPath: "",
onProgress: (data) => {},
});
}
test();
downloadFile
1. Interface Description
Interface Function: File Download
Interface Declaration:
2.x version
downloadFile(options)
1.x version
downloadFile(cloudPath: string, localPath)
Note: v2.x and v1.x are incompatible
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Required | String | Cloud file path: dir/data.txt |
localPath | Required | String | Local file storage path; the file must include the file name |
3. Response
None (undefined)
4. Sample Code
import path from "path";
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.downloadFile({
cloudPath: "files/data.txt",
localPath: path.resolve("./data.txt"),
});
}
test();
downloadDirectory
1. Interface Description
Interface Function: Download Folder
Interface Declaration:
2.x version
downloadDirectory(options)
1.x version
downloadDirectory(cloudPath: string, localPath: string)
Note: v2.x and v1.x are incompatible
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Yes | String | Cloud folder |
localPath | Required | String | Local file storage path; the file must include the file name |
3. Response
None
4. Sample Code
import path from "path";
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.downloadDirectory({
cloudPath: "files/music",
localPath: path.resolve("./music"),
});
}
test();
NOTE:
- This operation will traverse all files in the folder. If the number of files is too large, it may cause the operation to fail.
- When the cloudPath does not exist, the SDK will not download the file nor throw an error.
listDirectoryFiles
1. Interface Description
Function: List all files in the folder
Interface declaration: listDirectoryFiles(cloudPath: string): Promise<IListFileInfo[]>
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Required | String | Cloud folder path: dir/data/ |
3. Response
Field | Required | Type | Description |
---|---|---|---|
- | Required | Array<FileItem> | File array |
FileItem
Field | Type | Description |
---|---|---|
Key | String | Object Key |
LastModified | String | Last modified time of the object in ISO8601 format, e.g. 2019-05-24T10:56:40Z |
ETag | String | Entity tag of the object, which is an information tag assigned upon creation to identify the object content and can be used to check for changes in the content. |
Size | String | Object size in bytes |
Owner | String | Object owner information |
StorageClass | String | Object storage type, STANDARD for standard storage |
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
const res1 = await storage.listDirectoryFiles("dir/data");
const res2 = await storage.listDirectoryFiles("dir/data", 20);
const res3 = await storage.listDirectoryFiles("dir/data", 20, "dir/dat");
for (let item in res1) {
console.log(item);
}
}
test();
getFileInfo
1. Interface Description
Function: Get file information
Interface declaration: getFileInfo(cloudPath: string): Promise<IFileInfo>
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Required | String | Cloud file path dir/data.txt |
3. Response
Field | Required | Type | Description |
---|---|---|---|
Size | Required | String | File size in KB |
Type | Required | String | File type |
Date | Required | String | Modification time |
ETag | Required | String | Object's entity tag (Entity Tag) |
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
const info = await storage.getFileInfo("files/data.txt");
console.log(info);
}
test();
deleteFile
1. Interface Description
Function: Batch delete files
Interface declaration: deleteFile(cloudPathList: string[])
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPathList | Required | Array<String> | String array of cloud file paths ['dir/data.txt'] |
3. Response
None
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.deleteFile(["files/data.txt"]);
}
test();
deleteDirectory
1. Interface Description
Function: Delete folder
Interface declaration: deleteDirectory(cloudPath: string)
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Yes | String | Cloud folder path |
Note: When the cloudPath does not exist, the SDK will not throw an error.
3. Response
None
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.deleteDirectory("files/");
}
test();
getTemporaryUrl
1. Interface Description
Function: Get temporary file download URL
Interface declaration: getTemporaryUrl(fileList: (string | TempUrlInfo)[]):Promise<Array.<FileUrlItem>>
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
fileList | Required | String or Array.<TempUrlInfo> | Array of cloud file paths or Array of TempUrlInfo |
TempUrlInfo
Field | Required | Type | Description |
---|---|---|---|
cloudPath | Yes | String | Cloud file path |
maxAge | Required | Number | Validity period of the temporary download URL, in seconds |
3. Response
Field | Required | Type | Description |
---|---|---|---|
- | Required | Array.<FileUrlItem> | List of file download URLs |
FileUrlItem
Field | Required | Type | Description |
---|---|---|---|
fileId | Required | String | File Id |
url | Required | String | Download link |
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
const urls = await storage.getTemporaryUrl(["files/data.txt"]);
const urls2 = await storage.getTemporaryUrl([
{
cloudPath: "files/data.txt",
maxAge: 86400,
},
]);
for (let item in urls) {
console.log(item.url);
}
}
test();
getStorageAcl
1. Interface Description
Function: Get file storage permissions
Interface declaration: getStorageAcl(): Promise<string>
2. Input Parameters
Null
3. Response
Field | Required | Type | Description |
---|---|---|---|
- | Required | String | Permission type |
All permission types:
- READONLY: Readable by all users, writable only by the creator and administrators
- PRIVATE: Readable and writable only by the creator and administrators
- ADMINWRITE: Readable by all users, writable only by administrators
- ADMINONLY: Readable and writable only by administrators
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
const acl = await storage.getStorageAcl();
console.log(acl);
}
test();
setStorageAcl
1. Interface Description
Function: Set file storage permissions
Interface declaration: setStorageAcl(acl: string): Promise<Object>
2. Input Parameters
Field | Required | Type | Description |
---|---|---|---|
acl | Yes | String | Description of file storage permissions |
Supported options for acl:
- READONLY: Readable by all users, writable only by the creator and administrators
- PRIVATE: Readable and writable only by the creator and administrators
- ADMINWRITE: Readable by all users, writable only by administrators
- ADMINONLY: Readable and writable only by administrators
3. Response
Field | Required | Type | Description |
---|---|---|---|
RequestId | Required | String | Request ID |
4. Sample Code
import CloudBase from "@cloudbase/manager-node";
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
await storage.setStorageAcl("READONLY");
}
test();