Skip to main content

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

FieldRequiredTypeDescription
localPathRequiredStringLocal file path. It is recommended to pass an absolute path
cloudPathRequiredStringCloud file path: dir/data.txt
onProgressRequiredFunctionUpload 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

FieldRequiredTypeDescription
localPathYesStringLocal folder path
cloudPathYesStringCloud folder path
onProgressRequiredFunctionUpload 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

FieldRequiredTypeDescription
cloudPathRequiredStringCloud file path: dir/data.txt
localPathRequiredStringLocal 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

FieldRequiredTypeDescription
cloudPathYesStringCloud folder
localPathRequiredStringLocal 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

FieldRequiredTypeDescription
cloudPathRequiredStringCloud folder path: dir/data/

3. Response

FieldRequiredTypeDescription
-RequiredArray<FileItem>File array

FileItem

FieldTypeDescription
KeyStringObject Key
LastModifiedStringLast modified time of the object in ISO8601 format, e.g. 2019-05-24T10:56:40Z
ETagStringEntity 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.
SizeStringObject size in bytes
OwnerStringObject owner information
StorageClassStringObject 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

FieldRequiredTypeDescription
cloudPathRequiredStringCloud file path dir/data.txt

3. Response

FieldRequiredTypeDescription
SizeRequiredStringFile size in KB
TypeRequiredStringFile type
DateRequiredStringModification time
ETagRequiredStringObject'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

FieldRequiredTypeDescription
cloudPathListRequiredArray<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

FieldRequiredTypeDescription
cloudPathYesStringCloud 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

FieldRequiredTypeDescription
fileListRequiredString or Array.<TempUrlInfo>Array of cloud file paths or Array of TempUrlInfo

TempUrlInfo

FieldRequiredTypeDescription
cloudPathYesStringCloud file path
maxAgeRequiredNumberValidity period of the temporary download URL, in seconds

3. Response

FieldRequiredTypeDescription
-RequiredArray.<FileUrlItem>List of file download URLs

FileUrlItem

FieldRequiredTypeDescription
fileIdRequiredStringFile Id
urlRequiredStringDownload 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

FieldRequiredTypeDescription
-RequiredStringPermission 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

FieldRequiredTypeDescription
aclYesStringDescription 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

FieldRequiredTypeDescription
RequestIdRequiredStringRequest 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();