Mount Object Storage
Overview
Cloud hosting services requiring persistent storage can be implemented by mounting CloudBase Object Storage or Tencent Cloud Object Storage. COS provides high-availability, high-reliability, and low-cost data storage services. After mounting it to cloud hosting instances, applications can access and manage data stored in COS as if they were using a local file system.
Main Advantages
- Data Persistence: Unlike temporary storage, data in COS remains preserved after instance destruction.
- High Reliability: COS provides 99.999999999% data reliability.
- Multi-instance Sharing: All instances can access the same storage content, facilitating data sharing.
- Pay-as-you-go: Billing based on actual storage usage and request count, cost-effective
Application Scenarios
COS Object Storage mount is particularly suitable for the following scenarios:
- Static Resource Storage: Store static resources such as website images, videos, and documents
- User Uploaded Content: Save user-uploaded files, such as avatars and attachments
- Data Backup and Archiving: Storing information requiring long-term retention, such as application logs and data backups
- Cross-instance Data Sharing: Sharing data among multiple instances
- Large File Processing: Handling large files that exceed temporary storage capacity limits
Configuration Steps
Preparation
- Obtain Access Keys: Go to the Tencent Cloud Access Management Console to create or obtain SecretID and SecretKey
You can create a separate sub-user for object storage and grant only the necessary COS access permissions, following the principle of least privilege.
Detailed Steps
Configure Access Keys
- Go to the Cloud Development Platform Console
- Select Resource Connections > Connection Management, or go to Storage Mounts > Add Access Key in the Cloud Hosting Service
- Add Tencent Cloud API keys and fill in the obtained SecretID and SecretKey
Enable Storage Mounting
- In the Cloud Hosting Service details page, choose Storage Mount
- Click Enable Storage Mounting, and select the storage type as Object Storage
Select Bucket
- Select CloudBase Object Storage: The system automatically populates the bucket information.
- Select Tencent Cloud Object Storage: You need to manually enter the bucket name.
Configure Mount Path
- Object Storage Mount Path: Specifies the COS directory to be mounted. The default is the root directory "/".
- Instance Mount Path: Specifies the mount location of COS in the instance. The default is "/mnt"
Confirm and Save Configuration
Mount Path Restriction
The following directories in the instance cannot be mounted: /, /proc, /sys, /dev, /var/run, and /app/cert
:::
Code Example
Node.js Example
const fs = require('fs');
const path = require('path');
// Assume COS is mounted at the /mnt/cos directory
const cosPath = '/mnt/cos';
// Write to file
app.post('/upload', (req, res) => {
const fileName = `file-${Date.now()}.txt`;
const filePath = path.join(cosPath, 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(cosPath, 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
from flask import Flask, request, jsonify
app = Flask(__name__)
# Assume COS is mounted at the /mnt/cos directory
cos_path = '/mnt/cos'
@app.route('/upload', methods=['POST'])
def upload_file():
file_name = f"file-{int(time.time())}.txt"
file_path = os.path.join(cos_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(cos_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
- Cache policy: Frequently accessed files should be cached in memory or temporary storage.
- Batch Operations: Try to batch read and write files to reduce the number of I/O operations.
- File Size: For large file operations, consider splitting into chunks.
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: When reading and writing shared files, implement an appropriate locking mechanism to avoid conflicts.
- Atomic Operations: Use techniques such as atomic rename to ensure the integrity of file operations.
// 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 a temporary file first
await fs.writeFile(tempPath, newContent);
// Atomically rename to replace the original file
await fs.rename(tempPath, filePath);
}
Security
- Access Control: Regularly check the access permission settings for COS buckets.
- Sensitive Data: Avoid storing unencrypted sensitive information in COS
- Public Access: When using CloudBase Object Storage, note that public read permissions may result in files being externally accessed.
Cost Optimization
- Lifecycle Management: Configure lifecycle rules for infrequently accessed data to automatically transition storage classes or delete.
- On-Demand Mounting: Mount COS only when needed, and unmount when not required to reduce resource consumption.