This topic describes how to use Function Compute-based edge applications to upload on-premises files to and download files from Alibaba Cloud Object Storage Service (OSS).

Prerequisites

Background information

Link IoT Edge provides the function compute capability that allows you to manage Alibaba Cloud resources. You can upload various files such as statistical reports, logs, images, and videos that are generated when devices are running at the edge. You can also download configuration files from Alibaba Cloud to configure programs that run at the edge. If you use Function Compute-based edge applications to access OSS, you can choose to permanently store device data.

To access Alibaba Cloud services by using Function Compute-based edge applications, follow these steps:

  • Visit the Alibaba Cloud official website, activate the corresponding Alibaba Cloud service, download the SDK, and package the SDK and Function Compute code into a .zip file.
  • Log on to the Function Compute console, create a function, and upload the .zip file.
  • Log on to the IoT Platform console, assign the function to the edge instance, and deploy the function to the edge gateway.

1. Create an OSS bucket

  1. For more information about how to create a bucket, see Create an OSS bucket. The following table describes parameters to be configured. For other parameters that are excluded in the table, you can use default values.
    Parameter Description
    Bucket Name Enter le-fc-bucket.
    Region Select the data center where the bucket resides from the drop-down list.

    You can select a data center that is close to your location to improve the access speed.

  2. Create an on-premises file named ossCloudFile.txt, edit the file based on your needs, and upload the file to the le-fc-bucket bucket. For more information about how to upload files to buckets, see Upload objects.
    The ossCloudFile.txt file is used for subsequent testing when you use a function to download OSS files.

2. Create a function to access OSS

  1. Download the following package of the function. The function is used to upload on-premises files to and download files from OSS.
  2. Log on to the Function Compute console.
    If you have not activated Function Compute, read the terms, select I have read and agree, and then click Activate Now.
  3. Optional. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, click Create Service. On the Create Service page, set the parameters as required and click Submit.
    The Service Name parameter is required. In this example, the Service Name parameter is set to EdgeFC. You can set other parameters based on your needs.
    Note If the EdgeFC service has been created for other scenarios or applications, you do not need to recreate the service.
  4. After you create the service, you must create a function in the service. On the Services and Functions page, click Create Function. On the Create Function page, click Configure and Deploy in the Event Function section.
  5. Configure the following parameters of the function.
    Parameter Description
    Service Name The service where the function resides. Select EdgeFC.
    Function Name Enter accessAliOSS.
    Runtime The runtime environment of the function. Select nodejs8.
    Function Handler The handler of the function. Use the default value index.handler.
    Memory The size of memory that is required to execute the function. Select 512MB.
    Timeout The timeout period of the function. Enter 10. Unit: seconds.
    Single Instance Concurrency The number of concurrent requests that can be processed by an instance. Use the default value.

    You can set other parameters based on your needs or leave them unspecified. For more information, see What is Function Compute?

    Confirm the function parameters and click Create.

  6. After the function is created, the function details page appears. Click the Code tab. On this tab, select Upload Zip File and click Select File to upload the accessAliOSS-code.zip package that you downloaded in step 1. Then, click Save.
    After the code is uploaded, you can modify the source code on the In-line Edit tab.The In-line Edit tab

    Edit the code as follows:

    • Replace <Your OSS Region> with the region that you selected when creating the bucket in 1. Create an OSS bucket. You can view the region on the Overview page of the OSS console.The bucket details page
    • Replace <Your OSS Bucket> with the bucket name le-fc-bucket that you specified when creating the bucket in 1. Create an OSS bucket.
  7. Log on to the gateway that has been created in Prerequisites and run the following command to generate an on-premises file named localFile.txt that is used to test the uploading feature.
    sudo echo "Hi, this is the file from edge GW." > /linkedge/run/localFile.txt
    Note When you use Function Compute code to access files of the gateway host, you must note the limit on the file path. If you use Link IoT Edge Standard Edition, Function Compute code runs in a file system that is isolated from the host. Function Compute only shares the /linkedge/run directory with the host environment and is authorized to read and write this directory. Therefore, you must specify /linkedge/run for the path of the test file.
  8. Go to the Function Compute console and find the accessAliOSS function. On the Code Management page, click Save and Invoke.
  9. Optional. To download OSS files, you must go to the function details page to modify the sample code of accessAliOSS. Choose Code > In-line Edit. In the code editor, go to line 45 to remove the client.put method, go to line 48 to remove the // character, and use the OSS SDK to call the following method: client.get('ossCloudFile.txt', "/linkedge/run/fileFromCloud.txt");.
    ossCloudFile.txt indicates the file that is uploaded to the le-fc-bucket bucket, and /linkedge/run/fileFromCloud.txt indicates the file that is downloaded from Alibaba Cloud to the edge side.1. Create an OSS bucket
Note The sample code of the function includes the following three operations.
  1. Retrieve a temporary STS token that is used to access Alibaba Cloud services.
    credChain.resolvePromise()
  2. Enter the name of the region where your OSS bucket resides and the bucket name.
    region: '<Your OSS Region>',
    bucket: '<Your OSS Bucket>',
  3. Use the OSS SDK to call the put() method to upload an on-premises file to OSS.
    client.put('fileFromEdge.txt', "/linkedge/run/localFile.txt");

    fileFromEdge.txt indicates the file that is stored in Alibaba Cloud after uploading is complete, and /linkedge/run/localFile.txt indicates the on-premises file to be uploaded. You can modify these two file names based on your needs. If you want to modify file names, you must ensure that the on-premises file exists.

3. Assign the function to the edge instance

Assign a RAM role that is attached with the AliyunOSSFullAccess RAM policy to the edge instance. Alternatively, you can attach the AliyunOSSFullAccess RAM policy to a role that the edge instance is authorized to assume. For more information about how to enable the edge instance to manage other Alibaba Cloud resources, see Access resources of other Alibaba Cloud services.

  1. In the left-side navigation pane, click Applications.
  2. Create a Function Compute-based edge application from the function that is created in step 2: Create a function to access OSS. For more information about how to create a Function Compute-based edge application, see Use Function Compute to create edge applications.

    The following table describes the application parameters.

    Parameter Description
    Application Name The name of the application. Example: accessAliOSS.
    Application Type Select Function Compute.
    Region Select the region where the service is deployed.
    Service Select EdgeFC.
    Function Select accessAliOSS.
    Authorization Select AliyunIOTAccessingFCRole.
    Application Version The version of the application. You must specify a unique version number for a single application.
    The following table describes the function parameters.
    Parameter Description
    Running Mode Two running modes are available. In this example, select Continuous. The application runs immediately after being deployed.
    Memory Limit (MB) The maximum memory that is available for running the function. Unit: MB. Enter 512. If the memory that is used by the function exceeds the limit, the Function Compute application is forced to restart.
    Timeout Limit (Seconds) The maximum processing period after the function receives an event. Use the default value 5. If the function does not return the result within the specified period, the Function Compute application is forced to restart.
    Scheduled Execution Turn on the Scheduled Execution switch and enter * * * * * in the text box. The CRON expression indicates that the function is scheduled to run every minute. For more information about CRON expressions, visit the CRONTAB website.

    You do not need to specify other parameters.

  3. In the left-side navigation pane, click Edge Instances.
  4. Find the created edge instance and click View in the Actions column.
  5. On the Instance Details page, click the Configurations tab. On this tab, click Add Role & Permission to assign the AliyunIOTAccessingFCRole role to the edge instance. Click OK.
  6. On the Instance Details page, click the Edge Applications tab. On the Edge Applications tab, click Assign Application.
  7. Assign the accessAliOSS function to the edge instance, and click Close.

4. Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper right corner. In the message that appears, click OK to assign resources such as sub-devices and Function Compute-based edge applications to the edge instance.
  2. Wait about one minute after the instance is deployed and go to the OSS console. Choose le-fc-bucket > Files to view the fileFromEdge.txt file that has been uploaded.
    In the Actions column, choose More > Download to save the file on your computer.View the uploaded file

    You have completed the procedure of using Function Compute to access Alibaba Cloud OSS.

What to do next

The following section describes possible causes if you do not see the fileFromEdge.txt file on the Files page of the OSS console:

  • The function has not been triggered. The function is scheduled to run every minute. After the edge application is deployed, the running of the function takes time. Wait two minutes and refresh the page.
  • The local system time of the gateway is invalid. You can use the date command to query the local system time. If the interval between the local system time and server time exceeds the limit, the OSS server rejects the request.
  • The file to be uploaded does not exist. You can run the ls /linkedge/run/localFile.txt command on the gateway to check whether a file exists. If the command output shows that no file exists, you must create a file. For more information about how to create a file, see Generate an on-premises test file.
  • The OSS configurations are invalid. For more information about how to configure OSS, see Edit code.
  • If the issue persists, you can use the cat /linkedge/run/logger/fc-base/accessAliOSS/log.INFO command to view operations logs of the function and find the cause.