Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the upload of Serverless files

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to optimize the upload of Serverless files, in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Preface

When developers need to build a Web application or mobile application, they can use the cloud function as the back-end service. The API gateway receives the client request and triggers the cloud function for processing. This kind of Serverless architecture has the advantages of simple and convenient, flexible expansion, high availability and so on, and is becoming the common choice of more and more people.

However, when building an application, developers will inevitably encounter the scenario of uploading files, such as App uploading user profile photos, personal blog article images, and website comment images, which all need to upload files to the backend. If your business is hosted on the host, there are often no restrictions on uploading files, so you can use multipart/form-data to upload files directly. However, under Serverless architecture, since only JSON data is supported between the API gateway and cloud functions, it is difficult to upload files using traditional methods. The general solution is for the client to convert files from binary to characters through Base64 and other algorithms before uploading.

Recently, Tencent Cloud Serverless team optimized the experience of uploading files and launched the Base64 encoding feature of API gateway. When uploading files, the Base64 encoding process originally done by the client was changed to be performed by the API gateway, which allows developers to upload binary files to the cloud function SCF without changing the client code. At the same time, the front-end development can generally complete the storage and display of pictures based on Base64 format, which makes this function very friendly to front-end developers.

Below, the process of uploading multiple files by Serverless and traditional multipart is compared, and the configuration of Base64 coding function is introduced.

The request process compares with the traditional file upload process.

If your backend service is hosted on a CVM, the request process for uploading files is as follows:

Step 1: the client can upload files directly using multipart/form-data

Step 2: get the binaries in the back-end service.

The following is a Python 3 reference code for the client to upload two images pic-1.jpg and pic-2.jpg to the backend service:

Import requestsfrom requests_toolbelt.multipart.encoder import MultipartEncoderfrom requests_toolbelt.multipart import decoder m = MultipartEncoder (fields= [('files []', ('file.jpg', open (' pic-2.jpg', 'rb'),' image/jpeg')), ('files []', ('file2.jpg', open (' pic-1.jpg', 'rb'),' image/jpeg') ]) res = requests.post (url=' https://yourwebsite.com/upload', data=m, headers= {'Content-Type': m.content_type}) json = res.json () print (json) Serverless upload file process

As shown in the figure, the request process for uploading files after enabling the Base64 feature using API gateway and cloud function:

The client can upload files directly using multipart/form-data.

Get the Base64-encoded text in the cloud function.

Contrast conclusion

Through the comparison of the above two ways, it is not difficult to see that the biggest advantage of Base64 coding function is that Serverless has the same file upload experience as the traditional way, which can be uploaded directly using the traditional client code.

In addition, after obtaining the Base64-encoded text in the cloud function, you only need to decode the event.body to get the binary file. The following is a Python 3 reference code that decodes multiple files in the cloud function:

#-*-coding: utf-8-*-import sysimport loggingimport requestsfrom requests_toolbelt.multipart.encoder import MultipartEncoderfrom requests_toolbelt.multipart import decoderimport base64import jsonprint ('Loading function') logger = logging.getLogger () def main_handler (event, context): logger.info ("start main handler") content_type_header = event [' headers'] ['content-type'] body = event [' body'] is_me = base64.b64decode (body) for part in decoder.MultipartDecoder (is_me) Content_type_header) .parts: print (part.content)

Note: related dependencies need to be installed in the form of layers or uploading zip packages.

Actual combat configuration

To meet the requirements of different scenarios, Base64 coding feature also provides "all trigger" and "Header trigger" trigger methods for you to choose from:

All triggers: after API enables all triggers, the request content of each request will be encoded by Base64 and then passed to the cloud function.

Header trigger: after API enables Header trigger, you must configure trigger rules. The API gateway will verify the request headers according to the trigger rules. Only requests with specific Content-Type or Accept request headers will be encoded by Base64 and then passed to the cloud function. Requests that do not meet the conditions will not be Base64 encoded and will be directly passed to the cloud function.

The following describes the configuration process of the two trigger methods:

Configure all triggers

Log in to the API gateway console and click "Service" in the left navigation bar.

In the list of services, click the service ID of the target service to view the list of API.

Click "New", fill in the API frontend configuration, and click "next".

Select "Cloud function SCF" for the API backend type, and check "Base64 Encoding" to complete the subsequent configuration process. The API created at this time has Base64 encoding enabled and defaults to "trigger all".

Configure Header trigger

Log in to the API gateway console and click "Service" in the left navigation bar.

In the list of services, click the service ID of the target service to view the list of API.

In the API list, click the API ID of the target API (the target API must be the API of the backend docking SCF) to view the API details page. In the API details page, click the * * basic configuration * * tab to find the * * Base64 Encoding * * configuration item.

Click "Edit" after "Base64", and select "Header trigger" as the trigger method. Click [add trigger Rule], select the parameter and fill in the parameter value.

After confirming that the configuration information is correct, click * * Save * *.

This is the answer to the question about how to optimize the upload of Serverless files. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report