File Storage NAS integrates seamlessly with Function Compute. You can configure a NAS file system for a function to access NAS files as if they were on a local file system. This provides persistent storage, removes local disk space limits, and allows different function instances to share file data. This topic describes how to configure a NAS file system for a function.
Prerequisites
A NAS file system is created. For more information, see Create a file system.
A function is created. For more information, see Create a web function.
Network settings are configured for the function. For more information, see Configure network settings.
Limits
A Function Compute function supports a maximum of five NAS mount targets in a single region.
The local directories specified for the NAS mount targets in the function's runtime environment cannot conflict.
Configure a NAS file system
-
Log on to the Function Compute console. In the left-side navigation pane, choose .
-
In the top navigation bar, select a region. On the Functions page, click the target function.
-
On the function details page, select the Configuration tab and click Modify next to Advanced Settings. In the Advanced Settings panel, go to the Storage section, enable Mount NAS file system , configure the required settings, and then click Deploy.
You can configure the NAS file system using either Automatic Configuration or Custom Configuration.
Auto config
The system automatically creates a general-purpose NAS file system named
Alibaba-Fc-V3-Component-Generated, along with a VPC, vSwitch, and security group with the same name.When you use Automatic Configuration again in the same region, the system reuses the existing NAS file system and VPC configurations instead of creating new ones. For more information about pricing, see VPC billing and Billing of general-purpose NAS.
Custom config
You must manually select a NAS file system and configure its parameters, such as user, user group, and mount target.
The following table describes key parameters.
Parameter
Description
Example
NAS
Select an existing NAS file system.
To create a NAS file system, click Create NAS File System and create one in the File Storage console.
ImportantOnly NAS file systems that use the NFS protocol are supported. NAS file systems that use the SMB protocol are not supported.
01d394****
User, User Group
Specify a custom user ID and user group ID. If unspecified, the configuration defaults to the root user:
UID=0andGID=0. For more information, see NAS users and user groups.1
Remote Directory
-
For a general-purpose NAS file system, this directory must start with /.
-
For an extreme NAS file system, this directory must start with /share.
If the specified directory does not exist on the remote NAS, Function Compute automatically creates it. The new directory will be owned by the user and user group you specified and will have
777permissions.For more information, see Remote directory.
/
Local directory of a function
We recommend using a subdirectory of /home , /mnt, /tmp, or /data.
NoteDo not use common Linux and Unix system directories or their subdirectories, such as /bin, /opt, /var, or /dev. Otherwise, the mount may fail.
For more information, see Local function directory.
/mnt/nas
Note-
To share NAS resources across multiple functions, configure each function with the same user and user group.
-
Files written to NAS will have the same permissions as files created locally within the function.
-
Verify the mount
Prepare the function code to access NAS
After configuring the NAS file system, you can access it in your code using the local directory path you specified for the mount.
-
On the function details page, click the Code tab. Write your code in the code editor, and then click Deploy.
This topic uses a Python event-driven function as an example. The code writes content to and reads content from the NAS file system.
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import subprocess import string import os def handler(event, context): # report file system disk space usage and check NAS mount target out, err=subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE).communicate() print('disk: ' + str(out)) lines = [ l.decode() for l in out.splitlines() if str(l).find(':') != -1 ] nas_dirs = [ x.split()[-1] for x in lines ] print('uid : ' + str(os.geteuid())) print('gid : ' + str(os.getgid())) for nas_dir in nas_dirs: sub_dir = randomString(16) file_name = randomString(6)+'.txt' new_dir = nas_dir + '/' + sub_dir + '/' print('test file: ' + new_dir + file_name) # Write to a NAS file. content = "NAS here I come" os.mkdir(new_dir) fw = open(new_dir + file_name, "w+") fw.write(content) fw.close() # Display the directory tree on the NAS mount for home, dirs, files in os.walk(nas_dir): level = home.replace(nas_dir, '').count(os.sep) indent = ' ' * 2 * (level) print('{}{}/'.format(indent, os.path.basename(home))) subindent = ' ' * 2 * (level + 1) for f in files: print('{}{}'.format(subindent, f)) # Read from a NAS file. f = open(new_dir + file_name, "r") print(f.readline()) f.close() return 'success' def randomString(n): return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))
Verify the result
-
After the code is deployed, click Test Function on the Code tab.
After the execution is complete, you can view the results at the bottom of the Code tab. On the Log Output tab, you can see that the function successfully wrote to and read from the NAS file.
C9JFL0.txt DCFIC887M7R74WYH/ 5CXI5H.txt H0630SH2QHPL9MHZ/ EUV7ZA.txt H4UNM032L2TDLPPQ/ YXDBPM.txt HYMF79FH11AYW66D/ LU3WNG.txt Z3QNEH0MY0K3VM6G/ 9D3FM2.txt fc-1/ modelscope-60c13a-model-download-func/ NAS here I come FC Invoke End RequestId: 1-66878b18-16cd285c-27f5980efe23 -
(Optional) After the function runs, you can log on to the instance to view the files in the local function directory and confirm that they match the log output.
-
On the function details page, click the Instance tab. In the Actions column of the target instance, click Connect to Instance.
If no instances are running, click Test Function on the Code tab to run the function again and create an instance.
-
After you log on to the instance, you can run commands to view the files in the configured local function directory.
root@c-668b4cff-xxx:/code# cd /mnt/nas root@c-668b4cff-xxx:/mnt/nas# ls 0Q0V5ZYWMW7SLUYV BVT5FA0OV9ZLIZMU DCFIC887M7R74WYH F8I2CGH79QQGPZKC HO63OSH2QHPL9MHZ H4UNM032L2TDLPPQ HYMF79FH11AYW66D KEDRGMD67X64A6FD Z3QNEH0MY0K3VM6G fc-1 modelscope-60c13a-model-download-func root@c-668b4cff-xxx:/mnt/nas# cd ../ root@c-668b4cff-xxx:/mnt# cd nas/Z3QNEH0MY0K3VM6G root@c-668b4cff-xxx:/mnt/nas/Z3QNEH0MY0K3VM6G# ls 9D3FM2.txt root@c-668b4cff-xxx:/mnt/nas/Z3QNEH0MY0K3VM6G#
-
Related concepts
NAS users and user groups
The UserID and GroupID values can range from 0 to 65534. If you do not specify these values, they both default to 0, which corresponds to the root user ID and root group ID, respectively. You must set the file owner and corresponding group permissions as needed to ensure consistent read and write access. For example, if you want different functions to share NAS file resources, you must use the same user and user group when you configure the NAS file system for these functions.
Remote directory and local function directory
Mounting a NAS file system maps a Local directory of a function in the function's environment to a Remote Directory in the NAS file system.
-
Remote Directory
The remote directory is a directory path on the NAS file system, such as /workspace/document. This path is combined with a mount target, for example xxxx-nas.aliyuncs.com, to form the full address for mounting. The complete mount source would look like xxxx-nas.aliyuncs.com:/workspace/document.
To find the mount target, log on to the NAS console, click your file system, and then click Mount Usage.
-
Local directory of a function
The local directory in the function's runtime environment serves as the mount point on the local file system. We recommend using a subdirectory of /home, /mnt, /tmp, or /data. Do not use common Linux and Unix system directories or their subdirectories to mount a NAS file system. Examples include /bin, /opt, /var, and /dev.
References
For more information about the scenarios for using NAS with Function Compute and the differences between storage types, see Select a function storage type.