Object Storage Service (OSS) is a highly reliable, secure, and cost-effective cloud storage service that provides massive data storage capacity. You can configure an OSS mount for Function Compute, which allows your functions to access OSS as if it were a local file system. This streamlines resource access and data processing.
Limits
You can configure a maximum of five NAS mount targets and five OSS mount targets for a function in Function Compute in the same region.
The local directories for NAS mount targets and OSS mount targets in the function's runtime environment cannot conflict.
For more information about how to configure a NAS file system mount target, see Configure a NAS file system.
Prerequisites
Object Storage Service (OSS)
Function Compute
You must grant the required permissions to the role that is assumed by the function. When you enable the OSS mounting feature, you must configure a role for your function that has permissions to access OSS. For more information, see Use function roles to grant Function Compute access to other Alibaba Cloud services.
Procedure
Step 1: Configure an OSS mount
Log on to the Function Compute console. In the left navigation pane, choose .
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configuration tab, and then click Edit in the Advanced Configuration section. In the Advanced Configuration panel, find the Storage option, turn on the Mount OSS switch, configure the following parameters, and then click Deploy.
Configuration item
Description
Example
OSS Mount Target: Configure OSS mount targets.
Bucket
Select an existing bucket. To create a new OSS bucket, click Create New OSS Bucket below to go to the OSS console and create one manually. For more information about OSS billing, see OSS Billing Overview.
example-bucket
Bucket Subdirectory
Set an absolute path for the subdirectory in the bucket. If you leave this field empty or set it to /, the root directory of the bucket is mounted.
/files
OSS Endpoint
The endpoint that corresponds to the selected bucket is used by default. As needed, select Custom Endpoint to change the endpoint value. For more information about OSS endpoints in different regions, see OSS regions and endpoints.
NoteIf you select a bucket in the same region as the Function Compute function, use an internal OSS endpoint.
If you select a bucket in a different region, you must use a public OSS endpoint. In this case, you are charged for outbound traffic over the internet.
Default endpoint
Local Function Folder
Specify a local directory in the function runtime. The directory must be a subdirectory of /home, /mnt, or /data.
/mnt/oss
Permissions On Local Function Directory
The access permissions for the local directory after the bucket is mounted. The permissions can be set to Read-only or Read/write.
Read and write
NoteThe OSS mounting feature depends on the network configuration of the function. If you set Function Invocation Only By Specified VPCs to Yes and Allow Default NIC To Access Internet to No, your function must be able to access the Internet through the specified VPC to use a public OSS endpoint. For more information, see Assign a static public IP address.
Step 2: Access files in the mounted directory
After you configure the OSS mount, you can access the files in the mounted directory in the same way that you access local files. Follow these steps:
On the function details page, click the Code tab, write code in the code editor, and then click Deploy.
The following sample code provides an example in Python.
import os def handler(event, context): # The mounted directory. mount_path = '/mnt/oss' # List files in the mounted directory. files = os.listdir(mount_path) print("Files in OSS mount:", files) # Read a file in the mounted 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 mounted 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."NoteIn the code above, replace
example.txtwith the name of an existing file in the mounted OSS directory.After the code is deployed, click Test Function on the Code tab.
After the execution is complete, you can view the execution result below the Code tab. On the Log Output tab, you can view the content of the
example.txtfile that was read from the OSS directory. In the OSS console, you can also view the content of theoutput.txtfile that was written to the corresponding mounted directory.
FAQ
An OSS mount fails with the bucket not found error.
Confirm that the OSS endpoint and bucket name are correct.
What do I do if OSS mounting fails and the host resolv error or deadline exceeded error is reported?
Confirm that the endpoint is correct.
A
host resolv erroroccurs if the domain name of the endpoint cannot be resolved.Internal endpoints cannot be used across regions. If you use an internal endpoint from another region, a connection timeout occurs, which results in a
deadline exceedederror.
Mounting fails with the error invalid credentials.
Check whether the Resource Access Management (RAM) role that you configured for your function has permissions to access OSS. The required permissions are listed below. For more information, see Use function roles to grant Function Compute access to other Alibaba Cloud services.
Read-only: Includes the
oss:ListObjectsandoss:GetObjectpermissions.Read and write: Includes the
oss:ListObjects,oss:GetObject,oss:PutObject,oss:DeleteObject,oss:ListParts, andoss:AbortMultipartUploadpermissions.
oss:ListObjects is a bucket-level action. If you grant access to a specific bucket, the policy must include the bucket-level resource, such as acs:oss:*:*:bucketName. For more information, see OSS Resource description.
When you read a mounted file, the error message is Input/output error.
This error occurs if the storage class of your OSS bucket is Archive or Cold Archive. Objects in these storage classes are in a frozen state and you must restore them before you can access them. To resolve this issue, set the storage class of the OSS bucket to Standard.
How do I view files in my configured local function directory?
On the function details page, click the Instances tab. In the list of instances, find an instance that is in the Running state, and then click Connect To Instance in the Actions column.
After you log on to the instance, run commands to view the file information in the configured local directory. For example:

What do I do if the Transport endpoint is not connected error is reported when I access a mount target in a function instance?
This error can occur if the OSS mounting feature becomes unavailable due to insufficient memory, which can be caused by low memory specifications or high memory usage. To resolve this issue, increase the function's memory. We recommend that you specify at least 512 MB of memory for functions that use OSS mount targets.
Is data written to the function directory permanently stored?
No, it is not. When a function instance is destroyed, the data written in the function directory is also deleted. If you want to permanently store data, we recommend that you mount a file system, such as a NAS file system or an OSS file system. For more information, see Configure a NAS file system and Configure an OSS file system.
How do I use a permission policy to allow only read-only access to a specified bucket?
How do I use a permission policy to allow read and write access to a specified bucket?
How do I use a permission policy to allow only read-only access to a subdirectory of a specified bucket?
How do I use a permission policy to allow read and write access to a subdirectory of a specified bucket?
Why is a file empty on the OSS side while it is being written from an OSS mount point?
When you write a file from an OSS mount point, the system uploads the content to OSS only when you explicitly call the flush operation or close the file.
Slow response for operations such as compression, decompression, or file transfer in an OSS mount target.
OSS does not natively support file system APIs. After you mount an OSS bucket as a directory, Function Compute integrates and encapsulates the OSS API to simulate file system API behavior. For example, OSS does not support random writes. To modify an existing file on an OSS mount target using a file system API, Function Compute downloads the source file from OSS, rewrites the file, and then uploads the modified file to OSS.
Performance is optimal when file system API features directly correspond to OSS API features, such as sequential file reads and writes. However, operations that require a combination of encapsulated OSS APIs, such as random file reads and writes for compression or decompression, may require multiple interactions with OSS. This results in lower performance compared to a local file system.
Are accesses to an OSS mount point coordinated and synchronized between different function instances?
No, they are not. Function instances are independent of each other. The content of an OSS mount point queried from different instances may be different. For example, after an instance creates a file in the OSS mount point, another instance may not be able to query the file in real time.