All Products
Search
Document Center

Object Storage Service:Use ossfs to mount an OSS bucket to a local directory on the Linux operating system

Last Updated:Sep 04, 2024

ossfs allows you to mount an Object Storage Service (OSS) bucket to a local directory on the Linux operating system. This way, your application can access resources in the bucket in the same manner your application accesses local resources. The mount feature facilitates resource sharing.

Prerequisites

OSS is activated and an OSS bucket is created. For more information, see Get started with OSS and Create a bucket.

Install and configure ossfs

  1. Install ossfs.

    Ubuntu

    Run the following commands in sequence to install ossfs in Ubuntu 16.04 (x64):

    sudo wget https://gosspublic.alicdn.com/ossfs/ossfs_1.91.3_ubuntu16.04_amd64.deb
    sudo apt-get update
    sudo apt-get install gdebi-core
    sudo gdebi ossfs_1.91.3_ubuntu16.04_amd64.deb

    CentOS or Anolis

    CentOS/Anolis

    Run the following command to install ossfs in CentOS 7.0 (x64):

    sudo wget https://gosspublic.alicdn.com/ossfs/ossfs_1.91.3_centos7.0_x86_64.rpm
    sudo yum install ossfs_1.91.3_centos7.0_x86_64.rpm

    If your client uses Yellowdog Updater, Modified (YUM) to install RPM packages, dependencies may fail to be downloaded by using YUM due to network environment factors on the client node. To resolve this issue, you can use YUM to download the dependencies over a normal network to a node that runs the same operating system version, and then copy the dependencies to the required node. For example, ossfs runs on FUSE 2.8.4 or later. Run the following command to download the latest version of FUSE from the YUM source to your local device:

    sudo yum install --downloadonly --downloaddir=./ fuse
    Important

    To download other dependencies, replace FUSE with the name of the required package.

  2. Add the mime.types file if you want the content type and file name extension of the uploaded object to match. ossfs queries the content in /etc/mime.types to configure the content type of the object.

    If you do not add the mime.types file, OSS sets the content type of the uploaded object to application/octet-stream by default.

    Run the following command to add the mime.types file:

    Ubuntu

    Run the following command to add the mime.types file:

    sudo apt-get install mime-support

    CentOS or Anolis

    Run the following command to add the mime.types file:

    sudo yum install mailcap

Mount a bucket and access data in the bucket

  1. Select the bucket that you want to mount to a local directory.

    Note

    We recommend that you do not mount an Infrequent Access (IA) bucket or a bucket for which real-time access of Archive objects is enabled to the local directory because two upload requests may be initiated when you upload objects to the preceding buckets from the local directory. The first request creates a 0-byte object and the second request uploads actual data to the object in the bucket. You are charged a 30-day storage fee of 64 KB for the 0-byte object based on the IA or Archive storage class. This results in unexpected storage costs.

  2. Configure the account information that is used to access your bucket.

    Write the following information to the /etc/passwd-ossfs file: the bucket name and the AccessKey pair (AccessKey ID and AccessKey secret) of the RAM user that has access permissions on the bucket. Then, set the permissions of the file to 640.

    sudo sh -c 'echo <BucketName>:<yourAccessKeyId>:<yourAccessKeySecret> > /etc/passwd-ossfs'
    ### Run the following command if you use a root user role:###
    # echo <BucketName>:<yourAccessKeyId>:<yourAccessKeySecret> > /etc/passwd-ossfs
    sudo chmod 640 /etc/passwd-ossfs

    Replace BucketName, yourAccessKeyId, and yourAccessKeySecret with your actual bucket name, AccessKey ID, and AccessKey secret. Example:

    sudo sh -c 'echo bucket-test:LTAI************:MOk8x0y9hx******************** > /etc/passwd-ossfs'
    sudo chmod 640 /etc/passwd-ossfs
  3. Mount the bucket to the specified directory.

    Note
    • To ensure the object read and write performance after you mount the bucket to the specified directory by using ossfs, we recommend that you specify appropriate parameters based on the suggestions in New features in ossfs V1.91.2 and V1.91.3.

    • To ensure data access security after you mount the bucket to the specified directory by using ossfs, we recommend that you configure the correct account information and access permissions. For more information, see ossfs configuration and mounting.

    sudo ossfs <BucketName> <mountfolder> -o url=<Endpoint>

    Run the following command to mount a bucket named bucket-test in the China (Hangzhou) region to the /tmp/ossfs directory:

    sudo mkdir /tmp/ossfs
    sudo ossfs bucket-test /tmp/ossfs -o url=http://oss-cn-hangzhou-internal.aliyuncs.com
    Important

    To reduce traffic costs generated when you read data, we recommend that you deploy ossfs in an Elastic Compute Service (ECS) instance and use -o url to specify that the internal endpoint is used to access the destination bucket. In this example, the internal endpoint is http://oss-cn-hangzhou-internal.aliyuncs.com. To use an internal endpoint, the ECS instance and the destination bucket must be located in the same region. For more information , see Regions and endpoints.

  4. Read objects in the bucket from the local file system.

    1. Query the list of objects in the bucket.

      sudo ls -lh /tmp/ossfs

      image

      Note

      Metadata-related operations, such as list directory, involve remote access to OSS and have a specific level of latency.

    2. Copy objects.

      sudo cp /tmp/ossfs/bird.jpeg /root

      The preceding command copies the bird.jpeg object to the /root directory.

    3. Upload an object to the bucket.

      sudo cp /root/forest.jpeg /tmp/ossfs

      The preceding command adds the forest.jpeg file to the local directory to which the bucket is mounted.

      image

      Note

      If the upload is interrupted and not resumed, the uploaded data of the object is stored in the bucket as parts. To prevent additional storage fees, we recommend that you use one of the following methods to delete these parts that you no longer need:

  5. Unmount the bucket if you no longer want to keep the bucket mounted.

    sudo fusermount -u /tmp/ossfs

References

  • For more information about how to configure ossfs, see Advanced configurations.

  • For more information about the ossfs options, see Common options .

  • For more information about the commonly asked questions about ossfs, see FAQ.