Cloud storage web

From bighardperson/computer-science-skills-collection · View interactive version

Cloud storage web is an AI agent skill from the GitHub repository bighardperson/computer-science-skills-collection. Complete guide for CloudBase cloud storage using Web SDK (@cloudbase/js-sdk) - upload, download, temporary URLs, file management, and best practices.

How to use this skill

Skill file

Cloud Storage Web SDK

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).

When to use this skill

Use this skill for file storage operations in web applications when you need to:

Do NOT use for:

How to use this skill (for a coding agent)

  1. Initialize CloudBase SDK
  1. Choose the right storage method
  1. Handle CORS requirements
  1. Follow file path rules

SDK Initialization

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
  env: "your-env-id", // Replace with your CloudBase environment ID
});

Initialization rules:

File Upload (uploadFile)

Basic Usage

const 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
}

Advanced Upload with Progress

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
  }
});

Parameters

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

Cloud Path Rules

CORS Configuration

⚠️ IMPORTANT: To prevent CORS errors, add your domain to CloudBase console:

  1. Go to CloudBase Console → Environment → Security Sources → Security Domains
  2. Add your frontend domain (e.g., https://your-app.com, http://localhost:3000)
  3. If CORS errors occur, remove and re-add the domain

Temporary Download URLs (getTempFileURL)

Basic Usage

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
  }
});

Multiple Files

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
    }
  ]
});

Parameters

| Parameter | Type | Required | Description |

|-----------|------|----------|-------------|

| fileList | Array | Yes | Array of file objects |

fileList Item Structure

| Parameter | Type | Required | Description |

|-----------|------|----------|-------------|

| fileID | string | Yes | Cloud storage file ID |

| maxAge | number | Yes | URL validity period in seconds |

Response Structure

{
  code: "SUCCESS",
  fileList: [
    {
      code: "SUCCESS",
      fileID: "cloud://env-id/folder/filename.jpg",
      tempFileURL: "https://temporary-download-url"
    }
  ]
}

Best Practices

File Deletion (deleteFile)

Basic Usage

const 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);
  }
});

Multiple Files

const result = await app.deleteFile({
  fileList: [
    "cloud://env-id/old-avatar.jpg",
    "cloud://env-id/temp-upload.jpg",
    "cloud://env-id/cache-file.dat"
  ]
});

Parameters

| Parameter | Type | Required | Description |

|-----------|------|----------|-------------|

| fileList | Array<string> | Yes | Array of file IDs to delete |

Response Structure

{
  fileList: [
    {
      code: "SUCCESS",
      fileID: "cloud://env-id/folder/filename.jpg"
    }
  ]
}

Best Practices

File Download (downloadFile)

Basic Usage

const result = await app.downloadFile({
  fileID: "cloud://env-id/folder/filename.jpg"
});

// File is downloaded to browser default download location

Parameters

| Parameter | Type | Required | Description |

|-----------|------|----------|-------------|

| fileID | string | Yes | Cloud storage file ID |

Response Structure

{
  // Success response (no specific data returned)
  // File is downloaded to browser
}

Best Practices

Error Handling

All 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);
}

Common Error Codes

Best Practices

  1. File Organization: Use consistent folder structures (uploads/, avatars/, documents/)
  2. Naming Conventions: Use descriptive filenames with timestamps if needed
  3. Progress Feedback: Show upload progress for better UX
  4. Cleanup: Delete temporary/unused files to save storage costs
  5. Security: Validate file types and sizes before upload
  6. Caching: Cache download URLs appropriately but respect expiration
  7. Batch Operations: Use arrays for multiple file operations when possible

Performance Considerations

  1. File Size Limits: Be aware of CloudBase file size limits
  2. Concurrent Uploads: Limit concurrent uploads to prevent browser overload
  3. Progress Monitoring: Use progress callbacks for large file uploads
  4. Temporary URLs: Generate URLs only when needed, with appropriate expiration

Security Considerations

  1. Domain Whitelisting: Always configure security domains to prevent CORS issues
  2. Access Control: Use appropriate file permissions (public vs private)
  3. URL Expiration: Set reasonable expiration times for temporary URLs
  4. User Permissions: Ensure users can only access their own files when appropriate
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.