All Products
Document Center

NAS sample

Last Updated: Oct 16, 2018

Alibaba Cloud Network Attached Storage (NAS) is a highly reliable and available distributed file system service that supports standard file access protocols. One can write code that access the shared file-system exactly the same way as its local file-system after enabling NAS configuration in a Function Compute service.


This tutorial illustrates how to create two functions that read and write to the NAS from scratch. We recommend that you read the article NAS access before you follow this tutorial. Here are the steps involved:

  1. Create a NAS mount point in the console.

  2. Create a service with NAS configuration.

  3. Create a "write to NAS" function.

  4. Create a "read from NAS" function.

Create a NAS mount point in the console

  1. Log on to the NAS console.

  2. Select a region.

  3. In the upper-right corner, click Create File System.


  4. In the prompted dialog box, enter all the fields. Particularly, in the Protocol Type row, select NFS (including NFSv3 and NFSv4).


  5. Click OK to create a file system.

  6. Locate the just created file system, and click Add Mount Point so that Function Compute can use the file system.

    Add Mount Point

  7. In the prompted dialog, enter all the required fields. Specifically:

    • In the Mount Point Type row, make sure you select VPC.

    • In the Permission Group row, make sure that the VPC default permission group selected allow access from all the IP addresses within the VPC.

    • Remember the id of the VPC you selected.


  8. Click OK to create a mount point.

Create a service with NAS configuration

  1. Log on to the Function Compute console.

  2. Select the target region in which your NAS is located.

  3. Create a service that uses the file system you created in step 7 of previous section. In this example:

    1. Enter the Service Name and Description.

    2. Enable Advanced Settings.

    3. Finish the VPC Configs field, make sure that you select the VPC used in step 7 of previous section. After the VPC Configs are complete, the NAS Config fields appear.


  4. Enter the Nas Config fields.

    • The UserId field is the uid under which the function runs. It determines the owner of all the files created on the NAS file system. We recommend that you maintain the -1 for UserId, which is a default value and indicates a random owner.

    • The GroupId field is the gid under which the function runs. We recommend that you select a gid and open the group access right on the files to be created, so that all the functions within the specified service can be shared easily.

    • The NAS Mount Point has options for valid NAS mount points that are accessible from the chosen VPC.

    • The Remote Path is a directory on the NAS file system, which does not need to be the root directory.

    • The Local Mount Path is the directory where the function can access the remote directory.

      You can add at most 5 directories, which do not necessarily use different mount points.

  5. Complete the rest of the service configuration.

  6. Click OK.

Create a "write to NAS" function

In this sample, we demonstrated a function written in Python 2.7 creates a file on the NAS file system. The root_dir field in the event must be one of the local mount paths configured in the step 4 of the previous section. You can see that you don’t need to use any specific client or import any NAS library to write to the NAS file system.

  1. import json
  2. import logging
  3. import random
  4. import string
  5. import os
  6. def handler(event, context):
  7. logger = logging.getLogger()
  8. evt = json.loads(event)
  9. root_dir = evt["root_dir"]
  10. sub_dir = randomString(16)
  11.'uid : ' + str(os.geteuid()))
  12.'gid : ' + str(os.getgid()))
  13. file_name = randomString(6)+'.txt'
  14. newDir = root_dir + '/' + sub_dir + '/'
  15. content = "NAS here I come"
  16. os.mkdir(newDir)
  17. fw = open(newDir+file_name, "w+")
  18. fw.write(content)
  19. fw.close()
  20. return sub_dir + '/' + file_name
  21. def randomString(n):
  22. return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))

Create a "read from NAS" function

In the same way, a function written in Node.js reads a file on the NAS file system as if reading from a local file. The event has the same root_dir as the "write to NAS" function. The file_path is the output of the invocation for the demonstrated "write to NAS" function. Again, you don’t need to use any NAS related library to do that.

  1. module.exports.handler = function(event, context, callback) {
  2. var fs = require('fs');
  3. var path = require('path');
  4. var eventObj = JSON.parse(event.toString());
  5. var root = eventObj["root_dir"]
  6. var file = eventObj["file_path"]
  7. var contents = fs.readFileSync(path.join(root, file), 'utf8');
  8. callback(null, contents);
  9. };

The following figure shows the output of the invocation, which is exactly what we wrote in the "write to NAS" function.