Using FunctionGraph to Compress Images in OBS
Introduction
This practice is applicable to compressing a single image or a batch of images. High-quality images consume significant storage/bandwidth, slowing website/app loading. Using OBS and FunctionGraph, you can build an image compression solution to automatically process images in buckets, optimizing storage and resource efficiency.
Constraints
- OBS Application Service trigger is available only in CN North-Beijing4, CN North-Ulanqab1, and CN East-Shanghai1. When creating a function or an OBS bucket, select one of the preceding regions.
- Ensure that the created function and OBS bucket are in the same region.
Resource and Cost Planning
Table 1 lists the resources and costs required for using FunctionGraph to compress images in OBS.
Resource |
Description |
Billing |
---|---|---|
OBS |
|
|
FunctionGraph |
|
|
Procedure
The following table describes how to use FunctionGraph to compress images in OBS.
Step |
Description |
---|---|
Create two OBS buckets. The source bucket is used to store the original image, and the target bucket is used to store the compressed image. |
|
Create a cloud service agency to authorize FunctionGraph to access other cloud services so that FunctionGraph can work with OBS. |
|
Create a function from scratch, configure the code environment, and create an OBS Application Service trigger to automatically compress images uploaded or updated in the OBS source bucket. |
|
Verify that the image is compressed in the target bucket. |
Step 1: Creating Two OBS Buckets
- Log in to the OBS console, choose Object Storage.
- Click Create Bucket.
- On the displayed Create Bucket page, set the OBS source bucket parameters by referring to Table 3.
Table 3 OBS source bucket configuration Parameter
Requirements
Example Value
Region
Mandatory
Region where the bucket is located. Select a region close to your service to reduce network latency and improve access speed. After a bucket is created, its region cannot be changed. Currently, the OBS Application Service trigger supports only CN North-Beijing4, CN North-Ulanqab1, and CN East-Shanghai1.
CN North-Beijing4
Bucket Name
Mandatory.
Bucket name, which must be globally unique. After a bucket is created, its name cannot be changed.
your-bucket-input
Data Redundancy Policy
Mandatory.
- Multi-AZ storage: Data is stored in multiple AZs to achieve higher reliability.
- Single-AZ storage: Data is stored in a single AZ, which costs less.
- After a bucket is created, its data redundancy policy cannot be changed.
Single-AZ storage
Storage Class
Mandatory.
- Standard: For storing a large number of hot files or small files that are frequently accessed (multiple times per month on average) and require fast access. Both single-AZ and multi-AZ storage are supported.
- Infrequent Access: For storing data that is less frequently accessed (less than 12 times per year on average), but when needed, the access has to be fast. Both single-AZ and multi-AZ storage are supported.
- Archive: For archiving data that is rarely accessed (once a year on average) and does not require fast access. Only single-AZ storage is supported.
Standard
Bucket Policy
Mandatory.
Controls read and write permissions for the bucket.
- Private: Only users granted permissions by the bucket ACL can access the bucket.
- Public Read: Anyone can read objects in the bucket.
- Public Read/Write: Anyone can read, write, or delete objects in the bucket.
Private
Enterprise Project
Mandatory.
Enterprise projects let you manage cloud resources by projects. The default project is default.
default
Properties
Optional.
For details, see Creating a Bucket.
- Direct Reading: allows you to download data from the Archive storage class without restoring them in advance. Direct reading is a billable function.
- Server-Side Encryption: the OBS server encrypts the objects uploaded from the client before storing them. If this feature is enabled, you must specify an encryption key.
- WORM: When enabled, you can configure a retention policy for the current bucket. The object version which the retention policy is applied to cannot be deleted within a specified period.
- Tags: Tags are used to identify and classify OBS buckets.
- Direct Reading: disabled
- Server-Side Encryption: disabled
- WORM: disabled
- Tags: -
- Repeat 3 to create the target bucket. Set the Bucket Name to your-bucket-output and keep other parameters the same as those of the source bucket.
- View your-bucket-input and your-bucket-output in the bucket list.
Step 2: Creating a Cloud Service Agency
- Log in to IAM console. In the navigation pane, choose Agencies. On the displayed page, click Create Agency in the upper right corner.
- Set the following parameters:
- Agency Name: Enter serverless_trust.
- Agency Type: Select Cloud service.
- Cloud Service: Select FunctionGraph.
- Validity Period: Select Unlimited.
- Description: Retain the default value.
- Click OK. The system displays a message indicating that the creation is successful. Click Authorize.
- On the Select Policy/Role page, search for and select the OBS Administrator policy, and click Next.
Figure 1 Selecting a policy
- Set the minimum authorization scope. Select All resources for Scope and click OK.
The OBS Administrator policy does not support specifying specific region or project resources.
- The system displays a message indicating that the authorization is successful. Click Finish to return to the agency list. If serverless_trust is displayed in the list, the agency is created successfully.
Step 3: Creating an Image Compression Function
- Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
- Click Create Function.
- Select Create from scratch, set the function information by referring to Table 4, and click Create Function.
Table 4 Configuring function parameters Parameter
Requirements
Example Value
Function Type
Mandatory.
- Event Function: triggered by triggers.
- HTTP Function: triggered once HTTP requests are sent to specific URLs.
Event Function
Region
Mandatory.
Region where the code is deployed. The region must be the same as that of the OBS bucket.
CN North-Beijing4
Function Name
Mandatory.
Function name, which contains letters, digits, underscores (_), and hyphens (-). It must start with a letter and end with a letter or digit. The length cannot exceed 60 characters.
fss_examples_image_thumbnail
Enterprise Project
Mandatory.
Enterprise project to which the function is added. The enterprise project must be the same as that of the OBS bucket. The default enterprise project is default.
default
Agency
This parameter is optional but mandatory in this practice.
Name of the agency used by FunctionGraph to access other cloud services. Select the agency created in Step 2: Creating a Cloud Service Agency.
serverless_trust
Runtime
Mandatory.
Development language and language version of the function. CloudIDE supports Node.js, Python, and PHP only.
Python3.6
- On the fss_examples_image_thumbnail details page, configure the following information:
- Download the sample code fss_examples_image_thumbnail_eg.zip.
- On the Code tab page, select Upload > Local ZIP, add the downloaded fss_examples_image_thumbnail_eg.zip file, and click OK. The code is automatically deployed.
- Click Add at the bottom of the page, add the public dependency package pillow-7.1.2, retain the default version 1, and click OK.
- On the Configuration > Basic Settings tab page, modify the following configuration:
- For Execution Timeout, enter 40.
- For Memory, select 256.
Click Save.
- Choose Configuration > Environment Variables, click Edit Environment Variable. In the dialog box that is displayed, click Add, add information in Table 5, and click OK.
Table 5 Environment variables Key
Value
Description
output_bucket
your-bucket-output
Name of the OBS bucket where compressed images are stored.
obs_endpoint
obs.cn-north-4.myhuaweicloud.com
OBS endpoint in CN North-Beijing4 (For other regions, see Regions and Endpoints.)
- On the Configuration > Triggers tab page, click Create Trigger. In the displayed dialog box, configure basic information by referring to Table 6 and click OK.
Table 6 Configuring trigger parameters Parameter
Requirements
Example Value
Trigger Type
Mandatory.
Add an OBS Application Service trigger to trigger the function when an operation is performed on an OBS bucket.
OBS Application Service
Trigger Name
Mandatory.
Name of the trigger to be created. Only letters, digits, underscores (_), and hyphens (-) are allowed. The value cannot start with a digit or hyphen (-). Maximum length: 128 characters.
Image
Bucket Name
Mandatory.
Select the created OBS bucket to store the original images.
your-bucket-input
Event Type
Mandatory.
Triggering event type. In this practice, the function is triggered by uploading or updating bucket objects.
Create or override bucket objects via UI or Put request Create or override bucket objects via Post request
Object Name Prefix
Optional.
Enter a keyword for limiting notifications to those about objects whose names start with the matching characters. This limit can be used to filter the names of OBS objects.
Leave this parameter blank.
Object Name Suffix
Optional.
Enter a keyword for limiting notifications to those about objects whose names end with the matching characters. This limit can be used to filter the names of OBS objects.
Leave this parameter blank.
Object Name Encoding
Mandatory.
Specifies whether to encode the object name.
Enabled by default.
Step 4: Verifying Image Compression
- Log in to the OBS console, click the your-bucket-input bucket. The Objects tab page is displayed.
- Click Upload Object, set Storage Class to Standard, and upload an image to be compressed. After the upload is successful, the page shown in Figure 2 is displayed.
- Go to the Objects page of the your-bucket-output bucket and view the size of the compressed image.
Figure 3 Viewing compressed images
To avoid unnecessary storage fees, you can delete the images stored in the two OBS buckets as required after the practice. Deleted data cannot be restored. Exercise caution when performing this operation.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot