Skip to main content

Mount Cloud File Storage (CFS)

Overview

When CloudBase Run services require persistent storage, you can mount Tencent Cloud File Storage (CFS) to achieve this. Cloud File Storage (CFS) provides a scalable shared file storage service based on the standard NFS file system access protocol, offering a shared data source for multiple instances with elastic capacity and performance scaling. Existing applications can mount and use it without any modifications.

Key Advantages

  • Data Persistence: Unlike temporary storage, data in CFS is retained even after instances are destroyed
  • High Availability & Reliability: Distributed file system architecture ensures data security and service availability
  • Multi-Instance Sharing: All instances can mount and access the same storage content simultaneously, facilitating data collaboration
  • Elastic Scaling: Storage capacity and performance can be scaled on demand without advance planning
  • Seamless Compatibility: Standard NFS protocol, existing applications can use it without modifications

Use Cases

CFS file storage mounting is particularly suitable for the following scenarios:

  • Big Data Analysis: Multiple instances read and write the same dataset in parallel, improving processing efficiency
  • Media Processing: Store and process large files such as videos and images, supporting multi-instance collaborative transcoding
  • Content Management: Store templates, configuration files, and other content that needs shared access across multiple instances
  • Log Aggregation: Multiple instances write logs to the same directory for centralized management and analysis
  • Machine Learning: Share training datasets and model files

Configuration Steps

Prepare Cloud File Storage (CFS)

  1. Go to the Tencent Cloud Console
  2. Prepare a VPC and subnet in the Shanghai region (the region where your CloudBase environment is located). For details, refer to the VPC Product Documentation
  3. Create a CFS file system in the Shanghai region, ensuring it is in the same subnet as the previous step. For details, refer to the CFS Product Documentation

Configure Storage Mounting

  1. Enable Private Network

    • On the CloudBase Run service details page, enable "Private Network"
    • Configure the VPC and subnet to match those prepared in the previous steps
  2. Enable Storage Mounting

    • On the CloudBase Run service details page, select "Storage Mounting"
    • Click "Enable Storage Mounting" and select the storage type as "Cloud File Storage (CFS)"
  3. Select File System

    • Select "File System ID": Choose the file system created during preparation
    • Select "Mount Point ID": Choose the mount point created during preparation
    • Configure "Remote Directory": Specify the remote directory to mount, default is the root directory "/"
    • Select "Permission": Choose read-write or read-only permission, default is "Read-Write"
  4. Configure Mount Path

    • Instance Mount Path: Specify the mount location within the instance, default is "/mnt"
  5. Confirm and Save Configuration

Mount Path Restrictions

The following directories within the instance cannot be mounted: /, /proc, /sys, /dev, /var/run, /app/cert

Code Examples

Node.js Example

const fs = require('fs');
const path = require('path');

// Assuming CFS is mounted at /mnt/cfs
const cfsPath = '/mnt/cfs';

// Write file
app.post('/upload', (req, res) => {
const fileName = `file-${Date.now()}.txt`;
const filePath = path.join(cfsPath, fileName);

fs.writeFileSync(filePath, req.body.content);
res.send(`File saved to: ${fileName}`);
});

// Read file
app.get('/files/:fileName', (req, res) => {
const filePath = path.join(cfsPath, req.params.fileName);

if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
res.send(content);
} else {
res.status(404).send('File not found');
}
});

Python Example

import os
import time
from flask import Flask, request, jsonify

app = Flask(__name__)

# Assuming CFS is mounted at /mnt/cfs
cfs_path = '/mnt/cfs'

@app.route('/upload', methods=['POST'])
def upload_file():
file_name = f"file-{int(time.time())}.txt"
file_path = os.path.join(cfs_path, file_name)

with open(file_path, 'w') as f:
f.write(request.json.get('content', ''))

return jsonify({"message": f"File saved to: {file_name}"})

@app.route('/files/<file_name>', methods=['GET'])
def get_file(file_name):
file_path = os.path.join(cfs_path, file_name)

if os.path.exists(file_path):
with open(file_path, 'r') as f:
content = f.read()
return content
else:
return jsonify({"error": "File not found"}), 404

Notes and Best Practices

Performance Considerations

  • Caching Strategy: Consider caching frequently accessed files in memory or temporary storage first
  • Batch Operations: Try to read and write files in batches to reduce I/O operation counts
  • File Size: For large file operations, consider chunked processing

Multi-Instance Collaboration

  • Path Isolation: When operating across different instances, it is recommended to create subdirectories using unique identifiers (such as instance IDs)
  • File Locking: Implement appropriate locking mechanisms when reading and writing shared files to avoid conflicts
  • Atomic Operations: Use techniques such as atomic rename to ensure file operation integrity
// Safe file update example (Node.js)
const fs = require('fs').promises;
const path = require('path');

async function safelyUpdateFile(filePath, newContent) {
const tempPath = `${filePath}.tmp`;

// Write to temporary file first
await fs.writeFile(tempPath, newContent);

// Atomically rename to replace the original file
await fs.rename(tempPath, filePath);
}

Security

  • Permission Control: Set CFS access permissions appropriately, following the principle of least privilege
  • Sensitive Data: Avoid storing unencrypted sensitive information
  • Security Group Configuration: Ensure security group rules only allow necessary network access