Introduction
For applications that need to read and write local files directly, ossfs provides a convenient solution, allowing the application to access data in Alibaba Cloud Object Storage Service (OSS) without modification. By using ossfs, you can mount an OSS bucket to a Linux system and map it to a local directory. This enables you to handle data in OSS as if it were local files, facilitating file sharing. This approach simplifies access to OSS data and leverages existing application logic without the need for redevelopment.
Functional Principle
s3fs is a userspace filesystem that allows you to mount an Amazon S3 bucket as a local filesystem on Linux. ossfs is built on top of s3fs and inherits all its features, including:
- Support for most POSIX file protocol standards, such as uploading/downloading files and directories and setting user permissions.
- Default use of OSS multipart upload and resumable upload for file transfers.
- MD5 checksum verification to ensure data integrity.
Runtime Environment
ossfs is based on FUSE (Filesystem in Userspace) and therefore can only run on machines that support FUSE. ossfs provides installation packages for Ubuntu and CentOS systems. If you need to run it in other environments, you can build the target program from source. Since we are using UnionTech UOS, which does not have an official RPM package, we must install it from source.
Installing ossfs
Install Dependencies
sudo apt-get install automake autotools-dev g++ git libcurl4-gnutls-dev \
libfuse-dev libssl-dev libxml2-dev make pkg-config
Download Source
git clone https://github.com/aliyun/ossfs.git
cd ossfs
Run Autogen Script
./autogen.sh
After running, the necessary build files will be generated.
Compile and Install
./configure # Checks dependencies
make
sudo make install
Verify Installation
ossfs --help
Mounting OSS on an ECS Insatnce
Configure Account Access Information
echo "my-bucket:YOUR_ACCESS_KEY_ID:YOUR_ACCESS_KEY_SECRET" > /etc/passwd-ossfs
chmod 640 /etc/passwd-ossfs
Create Mount Point
mkdir /mnt/oss
Mount a Single Bucket
ossfs my-bucket /mnt/oss -o url=http://oss-cn-shanghai-internal.aliyuncs.com -o allow_other
After muonting, files dragged from the local disk to /mnt/oss will have default permissions of 640, so you may need to run chmod to adjust them. Subsequent write operations by programs in that directory will set permissions to 755.
Parameter Notes:
url: Use the internal endpoint for the region (to avoid data transfer costs).allow_other: Allows non-root users (e.g., Nginx) to operate on the mounted directory, avoiding permission issues.
Mount Multiple Buckets
echo "bucket-alpha:YOUR_ACCESS_KEY_ID:YOUR_ACCESS_KEY_SECRET" > /etc/passwd-ossfs
echo "bucket-beta:YOUR_ACCESS_KEY_ID:YOUR_ACCESS_KEY_SECRET" >> /etc/passwd-ossfs
chmod 640 /etc/passwd-ossfs
mkdir -p /mnt/oss/bucket-alpha
mkdir -p /mnt/oss/bucket-beta
ossfs bucket-alpha /mnt/oss/bucket-alpha -o url=http://oss-cn-shanghai-internal.aliyuncs.com
ossfs bucket-beta /mnt/oss/bucket-beta -o url=http://oss-cn-shanghai-internal.aliyuncs.com
Mount a Specific Bucket Path
ossfs my-bucket:/app/files/ /mnt/oss -o url=http://oss-cn-shanghai-internal.aliyuncs.com
Unmount OSS
umount -l /mnt/oss # For paths created with mkdir
# Or use:
fusermount -u /mnt/oss