Object Storage Service (OSS) supports Function Compute. You can use Function Compute to build applications and functions of any type. This topic describes how to use Function Compute to compress multiple objects into a package and download the package from OSS to your local computer.
Prerequisites
Function Compute is activated.
You can activate Function Compute on the Function Compute product page.
Function Compute is granted permissions to access OSS.
For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
The OSS bucket must reside within the same region as the function.
Multiple objects are uploaded to a specific directory of a bucket. In this example, multiple objects are uploaded to the dir directory of the examplebucket bucket in the China (Hangzhou) region. For more information, see Simple upload.
A RAM role is created for the service to which the application belongs in Function Compute, and the Alibaba Cloud Resource Name (ARN) of the RAM role is recorded. For more information, see Create a RAM role for a trusted Alibaba Cloud account.
Solution
Downloading multiple objects from OSS at a time can cause inconvenience in some special cases. For example, the download speed is slow when you download a large number of small objects at a time. To improve your experience in downloading a large number of objects at a time, you can use Function Compute to compress the objects into a package, download the package to your local computer, and then decompress the package. The following figure shows how to use Function Compute to compress objects into a package and download the package.
A user invokes a function and specifies the objects to be compressed and the bucket in which the objects are stored.
Function Compute downloads the specified objects from OSS and generates a ZIP package that has a random name.
Function Compute uploads the ZIP package to OSS.
Function Compute returns the URL that is used to download the ZIP package to the user.
The user downloads the ZIP package from OSS by using the URL, which remains valid for a long period of time.
Usage notes
Streaming download and streaming upload are used to transfer the ZIP package due to the limited disk space for running the function. Only a small amount of data is cached in the memory.
To accelerate the transfer of the ZIP package, Function Compute generates the ZIP package and uploads the package to OSS at the same time.
Multipart upload is used to upload the ZIP package in parts in parallel to OSS.
Function Compute takes up to 24 hours to compress objects into a package. Test data shows that Function Compute took approximately 63 seconds to compress 57 objects whose total size is 1.06 GB into a package.
Procedure
In this example, Function Compute is used to download multiple objects in the dir/ directory of the examplebucket bucket in the China (Hangzhou) region to your local computer.
Deploy an application and obtain its public domain name.
Use ROS to deploy an application
Perform the following steps to use Resource Orchestration Service (ROS) to deploy a Function Compute application and obtain its public domain name:
Use Create Stack wizard in the ROS console.
In the Configure Parameters step, retain the default settings for all parameters and click Next.
In the Check and Confirm step, click Create.
On the Stack Information tab of the page that appears, the status of the stack is displayed as Creating.
After the stack enters the Created status, click the Outputs tab to view the public domain name.
Use Serverless Application Center to deploy an application
Go to Serverless Application Center to deploy the start-zip-oss application template.
Select Directly Deploy for Deployment Type. Select the region of the bucket for the application template. Retain the default settings for other parameters and click Create and Deploy Default Environment. The following figure shows the configuration details.
Obtain the public domain name.
In the Deploy Version panel of the page that appears, click the Result tab.
In the triggers field, obtain the public domain name from the urlInternet parameter.
Use Serverless Devs to deploy an application
Install Serverless Devs.
curl -o- -L http://cli.so/install.sh | bash
Check whether Serverless Devs is installed.
s -v
The following output is returned if Serverless Devs is installed:
@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-access
The following table describes the parameters in the preceding command.
Parameter
Description
--AccessKeyID
The AccessKey ID used to access Function Compute.
--AccessKeySecret
The AccessKey secret used to access Function Compute.
--Access
The custom key alias.
Deploy the start-zip-oss application template.
Initialize the project.
The following table describes sample configurations for the start-zip-oss application template.
Start the project.
Deploy the project.
Record the returned value of system_url.
s init start-zip-oss -d start-zip-oss
Configuration item
Example
The ID of the region in which you want to deploy the application
cn-hangzhou
The service to which the application belongs in Function Compute
start-zip-oss
The function name of the application
zip-oss-func
The ARN of the RAM role that is created for the service to which the application belongs in Function Compute
acs:ram::137918634953****:role/test-role
cd start-zip-oss
s deploy -y
fc-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
Run the following command to generate the event.json file and specify the bucket name and the directory in which the objects that you want to download are stored:
cat <<EOF > event.json { "bucket": "examplebucket", "source-dir": "dir/" } EOF
NoteYou must set source-dir to a directory other than the root directory of the bucket.
Run the following command to trigger the function:
curl -v -L -o oss.zip -d @./event.json https://zip-oss-func-zip-oss-ayouye****.cn-hangzhou.fcapp.run
/test/oss.zip: the local path used to store objects in the dir/ directory.
https://zip-oss-func-zip-oss-ayouye****.cn-hangzhou.fcapp.run: the value of system_url returned when the project is deployed.
NoteIf you want to change the anonymous non-authenticated HTTP function in the preceding example to the authenticated HTTP function, you can use one of the following OSS SDKs:
FAQ
Can I add watermarks to objects when I use Function Compute to compress the objects into a package and download the package?
Can Function Compute generate a download URL that has a validity period when I use Function Compute to download multiple objects as a package?
References
For more information about the introduction and billing rules of Function Compute, see Product Introduction and Billing.
For more information about the template and applications when you use Function Compute together with OSS, see Sample code.