Management-Side SDK Management
You can deploy the management-side manager-node SDK locally, on cloud servers, or even in cloud functions to perform batch operations on files in cloud storage space, including upload, download, deletion, file information retrieval, temporary link acquisition, and file permission setting.
Upload files
Call the uploadFile method on the management side to upload a single file.
const CloudBase = require("@cloudbase/manager-node");
const path = require("path");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function uploadFile() {
await storage.uploadFile({
localPath: path.resolve("./data.txt"),
cloudPath: "files/data.txt",
onProgress: (data) => {}
});
}
uploadFile();
Upload folders
Calling uploadDirectory on the management side enables batch uploading of files within a folder to cloud storage space.
const CloudBase = require("@cloudbase/manager-node");
const path = require("path");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function uploadDirectory() {
await storage.uploadDirectory({
localPath: path.resolve("./files"),
cloudPath: "",
onProgress: (data) => {}
});
}
uploadDirectory();
Download files
Call the downloadFile method on the management side to download a single file.
const CloudBase = require("@cloudbase/manager-node");
const path = require("path");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function downloadFile() {
await storage.downloadFile({
cloudPath: "files/data.txt",
localPath: path.resolve("./data.txt")
});
}
downloadFile();
Download folders
Calling the downloadDirectory method on the management side enables downloading a specified folder from cloud storage space to a designated folder on the management side.
const CloudBase = require("@cloudbase/manager-node");
const path = require("path");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function downloadDirectory() {
await storage.downloadDirectory({
cloudPath: "files/music",
localPath: path.resolve("./music")
});
}
downloadDirectory();
Deleting Files
Call the deleteFile method on the management side to perform batch deletion of single or multiple files.
const CloudBase = require("@cloudbase/manager-node");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function deleteFile() {
await storage.deleteFile(["files/data.txt", "uploads/logo.png"]);
}
deleteFile();
Delete folders
Calling the deleteDirectory method on the management side enables deletion of entire folders within cloud storage space.
const CloudBase = require("@cloudbase/manager-node");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function deleteDirectory() {
await storage.deleteDirectory("files/");
}
deleteDirectory();
Getting File Information
Calling the getFileInfo method on the management side retrieves the file's size, type, modification time, and the entity tag (ETag) of the object.
const CloudBase = require("@cloudbase/manager-node");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function getFileInfo() {
const info = await storage.getFileInfo("files/data.txt");
console.log(info);
}
getFileInfo();
Listing Files in a Folder
Calling the listDirectoryFiles method on the management side lists all files and their information within the specified folder in cloud storage space.
const CloudBase = require("@cloudbase/manager-node");
const { storage } = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, can be obtained from the Tencent Cloud CloudBase console
});
async function listDirectoryFiles() {
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);
}
}
listDirectoryFiles();
References
The Management SDK can also obtain temporary file links for files and get/set file storage permissions. For more details, refer to: