Complete guide for CloudBase cloud storage using Web SDK (@cloudbase/js-sdk) - upload, download, temporary URLs, file management, and best practices.
Use this skill when building web applications that need to upload, download, or manage files using CloudBase cloud storage via the @cloudbase/js-sdk (Web SDK).
Use this skill for file storage operations in web applications when you need to:
Do NOT use for:
Initialize CloudBase SDK
Choose the right storage method
uploadFile - For uploading files from browser to cloud storagegetTempFileURL - For generating temporary download linksdeleteFile - For deleting files from storagedownloadFile - For downloading files to browserHandle CORS requirements
Follow file path rules
[0-9a-zA-Z], /, !, -, _, ., , *, Chinese characters/ for folder structure (e.g., folder/file.jpg)import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace with your CloudBase environment ID
});
Initialization rules:
app instance across your applicationconst result = await app.uploadFile({
cloudPath: "folder/filename.jpg", // File path in cloud storage
filePath: fileInput.files[0], // HTML file input element
});
// Result contains:
{
fileID: "cloud://env-id/folder/filename.jpg", // Unique file identifier
// ... other metadata
}
const result = await app.uploadFile({
cloudPath: "uploads/avatar.jpg",
filePath: selectedFile,
method: "put", // "post" or "put" (default: "put")
onUploadProgress: (progressEvent) => {
const percent = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
console.log(`Upload progress: ${percent}%`);
// Update UI progress bar here
}
});
| Parameter | Type | Required | Description |
|---|---|---|---|
cloudPath | string | Yes | Absolute path with filename (e.g., "folder/file.jpg") |
filePath | File | Yes | HTML file input object |
method | "post" | "put" | No | Upload method (default: "put") |
onUploadProgress | function | No | Progress callback function |
[0-9a-zA-Z], /, !, -, _, ., , *, Chinese characters/ to create folder hierarchy"avatar.jpg""uploads/avatar.jpg""user/123/avatar.jpg"⚠️ IMPORTANT: To prevent CORS errors, add your domain to CloudBase console:
https://your-app.com, http://localhost:3000)const result = await app.getTempFileURL({
fileList: [
{
fileID: "cloud://env-id/folder/filename.jpg",
maxAge: 3600 // URL valid for 1 hour (seconds)
}
]
});
// Access the download URL
result.fileList.forEach(file => {
if (file.code === "SUCCESS") {
console.log("Download URL:", file.tempFileURL);
// Use this URL to download or display the file
}
});
const result = await app.getTempFileURL({
fileList: [
{
fileID: "cloud://env-id/image1.jpg",
maxAge: 7200 // 2 hours
},
{
fileID: "cloud://env-id/document.pdf",
maxAge: 86400 // 24 hours
}
]
});
| Parameter | Type | Required | Description |
|---|---|---|---|
fileList | Array | Yes | Array of file objects |
| Parameter | Type | Required | Description |
|---|---|---|---|
fileID | string | Yes | Cloud storage file ID |
maxAge | number | Yes | URL validity period in seconds |
{
code: "SUCCESS",
fileList: [
{
code: "SUCCESS",
fileID: "cloud://env-id/folder/filename.jpg",
tempFileURL: "https://temporary-download-url"
}
]
}
maxAge based on use case (1 hour to 24 hours)SUCCESS/ERROR codes in responseconst result = await app.deleteFile({
fileList: [
"cloud://env-id/folder/filename.jpg"
]
});
// Check deletion results
result.fileList.forEach(file => {
if (file.code === "SUCCESS") {
console.log("File deleted:", file.fileID);
} else {
console.error("Failed to delete:", file.fileID);
}
});
const result = await app.deleteFile({
fileList: [
"cloud://env-id/old-avatar.jpg",
"cloud://env-id/temp-upload.jpg",
"cloud://env-id/cache-file.dat"
]
});
| Parameter | Type | Required | Description |
|---|---|---|---|
fileList | Array<string> | Yes | Array of file IDs to delete |
{
fileList: [
{
code: "SUCCESS",
fileID: "cloud://env-id/folder/filename.jpg"
}
]
}
const result = await app.downloadFile({
fileID: "cloud://env-id/folder/filename.jpg"
});
// File is downloaded to browser default download location
| Parameter | Type | Required | Description |
|---|---|---|---|
fileID | string | Yes | Cloud storage file ID |
{
// Success response (no specific data returned)
// File is downloaded to browser
}
getTempFileURL insteadAll storage operations should include proper error handling:
try {
const result = await app.uploadFile({
cloudPath: "uploads/file.jpg",
filePath: selectedFile
});
if (result.code) {
// Handle error
console.error("Upload failed:", result.message);
} else {
// Success
console.log("File uploaded:", result.fileID);
}
} catch (error) {
console.error("Storage operation failed:", error);
}
INVALID_PARAM - Invalid parametersPERMISSION_DENIED - Insufficient permissionsRESOURCE_NOT_FOUND - File not foundSYS_ERR - System erroruploads/, avatars/, documents/)