Configure an OSS mount

更新时间:
复制 MD 格式

Object Storage Service (OSS) is a massive, secure, cost-effective, and highly reliable cloud storage service. You can mount an OSS bucket to Function Compute, which lets your function interact with OSS as if it were a local file system. This simplifies resource access and data processing workflows.

Limits

  • A Function Compute function supports up to five NAS and five OSS mount targets in the same region.

  • The local directories for NAS and OSS mount targets must be unique within the function's runtime environment.

    For more information about configuring NAS mount targets, see Configure a NAS file system.

Prerequisites

Procedure

Step 1: Configure an OSS mount target

  1. Log in to the Function Compute console. In the navigation pane on the left, choose Function Management > Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the name of the target function.

  3. On the function details page, click the Configuration tab. Click Advanced Settings to the right of Modify. In the Advanced Settings panel, find the Storage section, turn on the switch for Mount OSS, configure the parameters, and then click Deploy.

    Parameter

    Description

    Example

    OSS Mount Target: Configure the OSS mount target.

    Bucket

    Select an existing bucket. To create a new one, click Create OSS Bucket to open the OSS console. For information about fees associated with OSS, see OSS billing overview.

    example-bucket

    Bucket Subdirectory

    Specify a subdirectory in the bucket. This must be an absolute path. If you leave this empty or set it to /, the root directory of the bucket is mounted.

    /files

    OSS Endpoint

    When you select a bucket, the system automatically selects its endpoint. You can select Custom Endpoint to modify the endpoint address. For a list of OSS endpoints by region, see Regions and endpoints.

    Note
    • If the bucket and your Function Compute function are in the same region, use an internal endpoint.

    • If the bucket is in a different region, use a public endpoint. This incurs outbound data transfer fees.

    Default Endpoint

    Local directory of a function

    Specify a local directory in the function's runtime environment. The directory must be a subdirectory of /home, /mnt, or /data.

    /mnt/oss

    Permissions on the local directory of the function

    Select the permissions for the local directory. You can select Read-only or Read and Write.

    Read/write

    Note

    The OSS mount feature depends on the function's network configuration. If your function is configured to access only a VPC and Allow Default NIC to Access Internet is set to No, the function must be able to access the internet through the specified VPC to use a public OSS endpoint. For details, see Configure a static public IP address.

Step 2: Access files in the mounted directory

After you configure the OSS mount target, you can access files in the mounted directory as if they were local.

  1. On the function details page, click the Code tab. Write your code in the editor, and then click Deploy.

    The following code provides an example.

    import os
    
    
    def handler(event, context):
        # Mount directory
        mount_path = '/mnt/oss'
        
        # List files in the mount directory
        files = os.listdir(mount_path)
        print("Files in OSS mount:", files)  
        # Read a specific file from the mount directory
        file_path = os.path.join(mount_path, 'example.txt')
        if os.path.exists(file_path):
            with open(file_path, 'r') as file:
                content = file.read()
                print("Content of example.txt:", content)
        else:
            print("example.txt does not exist.")
        # Write a file to the mount directory
        write_path = os.path.join(mount_path, 'output.txt')
        with open(write_path, 'w') as file:
            file.write("Hello, OSS mount!")
            print("Wrote to output.txt in OSS mount.")
        
        return "Function execution completed."
      
    Note

    In the code, replace example.txt with the name of an actual file that exists in your mounted OSS directory.

  2. After the code is deployed, click Code on the Test Function tab.

    After the execution is complete, you can view the results at the bottom of the Code tab. The Log Output tab shows the content of the example.txt file read from the OSS directory. You can also go to the OSS console to verify that the output.txt file was written to the corresponding mounted directory.

FAQ

OSS mount error: bucket not found

Verify that the OSS access endpoint and bucket name are correct.

OSS mount error: host resolv error or deadline exceeded

Verify that the endpoint address is correct.

  • The host resolv error occurs if the endpoint address cannot be resolved.

  • Internal endpoints cannot be used across regions. Using an internal endpoint for a function in a different region causes a connection timeout, resulting in the deadline exceeded error.

Mount error: invalid credentials

Verify that the RAM role configured for your function has the necessary permissions to access OSS. For details, see Grant a function permissions to access other cloud services by using a function role. The required permissions are as follows:

  • Read-only: Requires the oss:ListObjects and oss:GetObject permissions.

  • Read and Write: Requires the oss:ListObjects, oss:GetObject, oss:PutObject, oss:DeleteObject, oss:ListParts, and oss:AbortMultipartUpload permissions.

Note

The oss:ListObjects action is performed at the bucket level. Therefore, when you grant access to a specific bucket, the permission policy must include a resource ARN at the bucket level, such as acs:oss:*:*:bucketName. For more information, see OSS Resource.

Input/output error on reading mounted files.

Check the storage class of your OSS bucket. If the storage class is Archive or Cold Archive, objects in the bucket are in a frozen state and must be restored before they can be accessed. Set the storage class of your OSS bucket to Standard.

How to view files in the local directory

  1. On the function details page, click the Instance tab. In the list of instances that are in the Running state, select an instance and click Connect to Instance in the Actions column.

  2. After you connect to the instance, you can run commands to view information about the files in the configured local directory. For example:

    image

Error accessing mount target: Transport endpoint is not connected

This error occurs when the OSS mount feature fails due to insufficient memory, which may be caused by low memory specifications or high memory usage. Increase the memory specification for your function based on your business needs. A minimum of 512 MB is recommended when using an OSS mount target.

Data persistence in function directories

Data written to a function's directory is deleted when the function instance is destroyed. To ensure data persistence, you can mount a NAS file system or an OSS bucket. For details, see Configure a NAS file system and Configure an OSS mount.

Read-only access for a bucket

Expand to view an example permission policy. Replace bucketName in the example with the name of your bucket. For more information, see RAM Policy overview.

{
  "Version": "1",
  "Statement": [
    {
      "Action": [
        "oss:ListObjects",
        "oss:GetObject"
      ],
      "Resource": [
        "acs:oss:*:*:bucketName",
        "acs:oss:*:*:bucketName/*"
      ],
      "Effect": "Allow"
    }
  ]
}

Read/write access for a bucket

Expand to view an example permission policy. Replace bucketName in the example with the name of your bucket. For more information, see RAM Policy overview.

{
  "Version": "1",
  "Statement": [
    {
      "Action": [
        "oss:ListObjects",
        "oss:GetObject",
        "oss:PutObject",
        "oss:DeleteObject",
        "oss:AbortMultipartUpload",
        "oss:ListParts"
      ],
      "Resource": [
        "acs:oss:*:*:bucketName",
        "acs:oss:*:*:bucketName/*"
      ],
      "Effect": "Allow"
    }
  ]
}

Read-only access for a subdirectory

Expand to view an example permission policy. Replace bucketName with the name of your bucket and bucketPath with the specific subdirectory. For more information, see RAM Policy overview.

{
  "Version": "1",
  "Statement": [
    {
      "Action": "oss:ListObjects",
      "Effect": "Allow",
      "Resource": [
        "acs:oss:*:*:bucketName"
      ],
      "Condition": {
        "StringLike": {
          "oss:Prefix": [
            "bucketPath/*"
          ]
        }
      }
    },
    {
      "Action": [
        "oss:GetObject"
      ],
      "Effect": "Allow",
      "Resource": [
        "acs:oss:*:*:bucketName/bucketPath/*"
      ]
    }
  ]
}

Read/write access for a subdirectory

Expand to view an example permission policy. Replace bucketName with the name of your bucket and bucketPath with the specific subdirectory. For more information, see RAM Policy overview.

{
  "Version": "1",
  "Statement": [
    {
      "Action": "oss:ListObjects",
      "Effect": "Allow",
      "Resource": [
        "acs:oss:*:*:bucketName"
      ],
      "Condition": {
        "StringLike": {
          "oss:Prefix": [
            "bucketPath/*"
          ]
        }
      }
    },
    {
      "Action": [
        "oss:GetObject",
        "oss:PutObject",
        "oss:DeleteObject",
        "oss:AbortMultipartUpload",
        "oss:ListParts"
      ],
      "Effect": "Allow",
      "Resource": [
        "acs:oss:*:*:bucketName/bucketPath/*"
      ]
    }
  ]
}

Files appear empty during write operations

When you write to a file through an OSS mount target, the system typically uploads the content to OSS only after you explicitly call the flush() method or close the file.

Slow performance for compression or file transfer

OSS does not natively support file system APIs. When you mount an OSS bucket as a directory, Function Compute emulates file system API behavior by combining and wrapping OSS APIs. For example, because OSS does not support random writes, modifying an existing file on an OSS mount target requires Function Compute to download the entire source file from OSS, modify it, and then re-upload it.

Performance is generally good for operations where file system APIs correspond directly to OSS API functions, such as sequential file reads and writes. However, operations that require combining multiple OSS APIs, such as the random read and write operations used in file compression or decompression, involve numerous interactions with OSS and are therefore slower than on a local file system.

Synchronization between function instances

No. Function instances are independent of each other, so access to an OSS mount target is not synchronized between them. For example, if instance A creates a file in the mount target, instance B might not see that file immediately.