Object Storage Service (OSS) integrates with Function Compute, allowing you to build various applications and functions. This topic explains how to use Function Compute to package multiple OSS objects into a single ZIP file for local download.
Prerequisites
-
Function Compute is activated.
You can activate Function Compute on the Function Compute product details page.
-
You must grant Function Compute permissions to access OSS.
For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
-
The bucket and the application function must be in the same region.
-
You must upload multiple objects to a specified directory in a bucket. This tutorial uses the
dir/directory in theexamplebucketbucket in the China (Hangzhou) region as an example. For more information, see Simple upload. -
You must create a RAM role for the Function Compute service and record its ARN. For more information, see Create a RAM role for a trusted Alibaba Cloud account.
How it works
Downloading multiple objects from OSS in bulk, especially many small ones, can be slow and inconvenient. By using Function Compute, you can package the objects on OSS first, download the compressed package to your local computer, and then decompress it. This method enables faster bulk downloads. The workflow for using Function Compute to package and download OSS objects is shown in the following figure.
-
A user invokes the function and specifies the bucket and objects to be compressed.
-
Function Compute downloads the specified objects from OSS and generates a ZIP package with a random name.
-
Function Compute uploads the ZIP package to OSS.
-
Function Compute returns the download URL of the ZIP package to the user.
-
The user downloads the file from OSS using the returned permanent download URL.
Usage notes
-
The disk space in the function's runtime environment is limited. Therefore, the function streams data for both downloads and uploads to cache only a small amount of data in memory.
-
To accelerate the process, Function Compute uploads the ZIP file to OSS while it is being generated.
-
When uploading the ZIP file to OSS, the function uses the multipart upload feature of OSS to upload parts concurrently through multiple threads.
-
The maximum processing time for compressing files using Function Compute is 24 hours. (Test data: 57 objects with a total size of 1.06 GB were processed in 63 seconds).
Procedure
This example describes how to use Function Compute to package and download multiple objects from the dir/ directory in the examplebucket bucket in the China (Hangzhou) region to a local computer.
-
Deploy the application and obtain its public domain name.
One-click with ROS
Use Resource Orchestration Service (ROS) to deploy the Function Compute application in one click.
-
Open the one-click configuration template link.
-
On the Configure Parameters tab, keep the default settings for all parameters and click Next: Check and Confirm.
-
On the Check and Confirm tab, click Create.
The Status on the Stack Information tab shows Creating.
-
When the Status of the stack changes to Create_Success, click the Outputs tab to find the public domain name.
One-click with Serverless Application Center
-
Deploy the start-zip-oss application template from Serverless Application Center.
For the deployment type, select Directly Deploy. Ensure the application template and the bucket are in the same region. Keep the default settings for other options. The configuration is shown in the following figure.

-
Obtain the public domain name.
-
Click the Result tab.
-
In the triggers field, obtain the public domain name (urlInternet).

-
Manual with Serverless Devs CLI
-
Install Serverless Devs.
npm install -g @serverless-devs/sVerify the installation.
s -vThe following output indicates a successful installation:
@serverless-devs/s: 2.1.1, s-home: /root/.s, linux-x64, node-v14.19.3 -
Configure Serverless Devs.
s config add --AccessKeyID LTAI******** --AccessKeySecret KZo1******** --access fc-accessThe following table describes the parameters.
Parameter
Description
--AccessKeyID
The AccessKey ID for accessing Function Compute.
--AccessKeySecret
The AccessKey Secret for accessing Function Compute.
--access
A custom alias for the credentials.
-
Deploy the start-zip-oss application template.
-
Initialize the project.
-
Specify values for the configuration items based on the following examples.
-
Go to the project directory.
-
Deploy the project.
-
Record the
system_urlfrom the output.
s init start-zip-oss -d start-zip-ossParameter
Example
Region where the application is located
cn-hangzhou
Function Compute service to which the application belongs
start-zip-oss
Function name of the application
zip-oss-func
ARN of the RAM role configured for the Function Compute service to which the application belongs
acs:ram::137918634953****:role/test-role
cd start-zip-osss deploy -yfc-zip-oss-service: region: cn-hangzhou service: name: start-zip-oss function: name: zip-oss-func runtime: python2.7 handler: main.main_handler memorySize: 3072 timeout: 1800 url: system_url: https://zip-oss-func-start-zip-oss-ayouye****.cn-hangzhou.fcapp.run triggers: - type: http name: http-test -
-
-
On your server or an ECS instance, run the following command to generate an
event.jsonfile. In the file, specify the bucket name and the directory that contains the objects to be downloaded.cat <<EOF > event.json { "bucket": "examplebucket", "source-dir": "dir/" } EOFNoteThe
source-dirparameter must be a directory within a bucket; the root directory is not supported. -
The following command is a sample request.
curl -v -L -o oss.zip -d @./event.json https://zip-oss-func-zip-oss-ayouye****.cn-hangzhou.fcapp.run-
oss.zip: The local path where the downloaded ZIP package is saved.NoteIf the built-in Windows decompression tool fails, use a third-party tool, such as 7-Zip or WinRAR.
-
https://zip-oss-func-zip-oss-ayouye****.cn-hangzhou.fcapp.run: thesystem_urlreturned when you deploy the project.
To invoke this as an authenticated HTTP function instead of an anonymous one, use one of the following SDKs:
-
FAQ
Adding watermarks
Time-limited download URLs
References
-
For more information about Function Compute's features and billing, see What is Function Compute? and Billing overview.
-
For templates and sample applications demonstrating Function Compute and OSS integration, see Sample code.
-
For more examples of using Function Compute with OSS, see Use Function Compute to automatically decompress ZIP files in OSS.