A method for file storage management based on a strategy pattern
By adopting a policy-based file storage management method, the problems of code coupling and high maintenance costs are solved, and flexible file storage management and efficient file server switching are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING WIT SCI & TECH CO LTD
- Filing Date
- 2023-06-30
- Publication Date
- 2026-05-12
AI Technical Summary
In existing technologies, engineers need to frequently modify code when dealing with different file servers, resulting in high code coupling, high maintenance costs, and low efficiency.
A strategy-based file storage management method is adopted. By setting a strategy interface and a storage strategy class, the file storage type parameter and the server type parameter are matched, and the GetStrategy component is used to automatically select the function of the file server client to perform file storage operations.
实现了无需频繁修改代码即可切换文件存储服务器,降低了维护成本,提高了存储效率,并支持扩展更多类型的文件服务器。
Abstract
Description
Technical Field
[0001] This invention relates to the field of software development, and more particularly to a method for file storage management based on a policy pattern. Background Technology
[0002] Enterprise file servers are an important application for companies. They are usually installed on the internal network to provide employees with file sharing and storage services. File servers can improve the efficiency of team collaboration, reduce repetitive work, and prevent data leaks. When engineers are developing projects, they will generate a series of files. When storing these files, different companies or projects may use different file services.
[0003] Existing file storage methods have the following drawbacks:
[0004] 1. When a company or project uses different file servers, engineers have to modify the code every time they choose a file server to store files. This leads to highly coupled code and high maintenance costs in the future.
[0005] 2. Saving files to different file servers and frequently modifying the code results in a large workload, low efficiency, and a high risk of code errors. Summary of the Invention
[0006] The purpose of this invention is to address the shortcomings of existing technologies by proposing a file storage management method based on a policy pattern.
[0007] To achieve the above objectives, the present invention adopts the following technical solution:
[0008] A file storage management method based on a policy pattern includes the following steps:
[0009] S1: Configure the policy interface;
[0010] Engineers define a series of file function calls in the strategy interface;
[0011] The file function includes functions for uploading files, deleting files, and downloading files;
[0012] S2: Obtain the file server client;
[0013] The file server clients include MinIO client, Alibaba OSS client, local OSS client, Alibaba OSS client, Qiniu storage client, Tencent OSS client, etc.
[0014] Engineers obtain various file server clients through the Spring container, encapsulate the file server clients, and generate utility classes for specific operations of the file server clients.
[0015] The utility class for the specific operations of the file server client includes the specific implementation methods of the function.
[0016] S3: Create a specific storage strategy class;
[0017] Engineers create file storage strategy classes corresponding to the file server client;
[0018] The file storage strategy classes include MinIO storage strategy class, Alibaba OSS storage strategy class, local OSS storage strategy class, Qiniu storage strategy class, Tencent OSS storage strategy class, etc.
[0019] Each storage strategy class includes a specific server type parameter value, and the server type parameter value in each storage strategy class is unique.
[0020] S4: Match the file to be stored with the storage policy class;
[0021] Engineers store the files to be stored during project development on the corresponding file server;
[0022] The file to be stored includes file storage type parameters;
[0023] The file storage type parameter of the file to be stored is matched with the server type parameter value in the storage strategy class. If the file storage type parameter of the file to be stored is the same as the server type parameter value in the specific storage strategy class, the storage strategy class is obtained through the GetStrategy component. The function in the file server client corresponding to the storage strategy class is called through the storage strategy class. The file upload function in the file server client is called to upload the file to be stored to the corresponding file server, thus completing the file upload and storage.
[0024] S5: File storage complete;
[0025] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0026] The file storage management method based on the strategy pattern proposed in this invention creates a strategy class corresponding to the file server client, matches the storage type parameter of the file to be stored with the parameters in the strategy class, matches it to the storage strategy class of the corresponding file storage client, and calls the function in the file server client corresponding to the storage strategy class to upload the file to be stored to the corresponding file server, thus completing the file upload and storage.
[0027] By using strategy classes to manage supported file servers, there is no need to modify the code every time a server is selected. As long as the strategy class supports it, file storage servers can be switched at will without modifying the code. This is independent of the business code, decoupled, and has low code maintenance costs.
[0028] It also facilitates future expansion to support more file server types. Simply modify the StorageType policy class and add a file service implementation class, reducing workload, lowering the risk of errors, and improving efficiency. Implementation
[0029] To provide a further understanding of the purpose, structure, features, and functions of the present invention, detailed descriptions are provided below with reference to specific embodiments.
[0030] A file storage management method based on a policy pattern includes the following steps:
[0031] S1: Configure the policy interface;
[0032] Engineers define a series of file function calls in the strategy interface;
[0033] The file function includes uploading files, deleting files, downloading files, etc.
[0034] The file upload function includes a file upload function that directly uploads a file stream to the file storage and a file upload function that uploads a file to the file storage via a file path;
[0035] By setting up two file upload functions, when the file is a regular file, the file path is obtained and the file is uploaded to the file server; when the file is large, the file stream is obtained through the MultipartFile component and uploaded to the file server, thus meeting the needs of uploading files of different sizes to the file server.
[0036] S2: Obtain the file server client;
[0037] The file server clients include MinIO client, Alibaba OSS client, local OSS client, Alibaba OSS client, Qiniu storage client, Tencent OSS client, etc.
[0038] Engineers obtain various file server clients through the Spring container, encapsulate the file server clients, and generate utility classes for specific operations of the file server clients.
[0039] The utility class for the specific operations of the file server client includes the specific implementation methods of the function.
[0040] The file server client is bound to the corresponding file server;
[0041] S3: Create a specific storage strategy class;
[0042] Engineers create file storage strategy classes corresponding to the file server client;
[0043] The file storage strategy classes include MinIO storage strategy class, Alibaba OSS storage strategy class, local OSS storage strategy class, Qiniu storage strategy class, Tencent OSS storage strategy class, etc.
[0044] Each storage strategy class includes a specific server type parameter value, and the server type parameter value in each storage strategy class is unique.
[0045] S4: Match the file to be stored with the storage policy class;
[0046] Engineers store the files to be stored during project development on the corresponding file server;
[0047] The file to be stored includes file storage type parameters;
[0048] The file storage type parameter of the file to be stored is matched with the server type parameter value in the storage strategy class. If the file storage type parameter of the file to be stored is the same as the server type parameter value in the specific storage strategy class, the storage strategy class is obtained through the GetStrategy component. The function in the file server client corresponding to the storage strategy class is called to perform relevant operations on the file to be stored.
[0049] For example:
[0050] The file to be stored is uploaded to the corresponding file server by calling the file upload function in the file server client;
[0051] The file to be stored is downloaded to the corresponding file server by calling the file download function in the file server client;
[0052] The file to be deleted is deleted from the file server by calling the file deletion function in the file server client.
[0053] S5: File storage complete;
[0054] The present invention has been described in the above-described embodiments; however, these embodiments are merely examples for implementing the present invention. It must be noted that the disclosed embodiments do not limit the scope of the present invention. Conversely, any modifications and refinements made without departing from the spirit and scope of the present invention are within the scope of patent protection of the present invention.
Claims
1. A file storage management method based on a policy pattern, characterized in that: Includes the following steps: S1: Configure the policy interface; Engineers define a series of file function calls in the strategy interface; The file function functions include functions for uploading files, deleting files, and downloading files; The file upload function includes a file upload function that directly uploads the file stream to the file storage and a file upload function that uploads the file to the file storage via a file path; when the file is a regular file, the file is uploaded to the file server by obtaining the file path; when the file is large, the file stream is obtained through the MultipartFile component and uploaded to the file server. S2: Obtain the file server client; The file server clients include MinIO client, Alibaba OSS client, local OSS client, Alibaba OSS client, Qiniu storage client, and Tencent OSS client; Engineers obtain various file server clients through the Spring container, encapsulate the file server clients, and generate utility classes for specific operations of the file server clients. The utility class for the specific operations of the file server client includes the specific implementation methods of the function. S3: Create a specific storage strategy class; Engineers create file storage strategy classes corresponding to the file server client; The file storage strategy classes include MinIO storage strategy class, Alibaba OSS storage strategy class, local OSS storage strategy class, Qiniu storage strategy class, and Tencent OSS storage strategy class; Each storage policy class includes specific server type parameter values; S4: Match the file to be stored with the storage policy class; Engineers store the files to be stored during project development on the corresponding file server; The file to be stored includes file storage type parameters; The file storage type parameter of the file to be stored is matched with the server type parameter value in the storage policy class. If the match is successful, the file to be stored is uploaded to the file server through the storage policy class. The server type parameter value is unique in each storage policy class; The file storage type parameter of the file to be stored is matched with the server type parameter value in the storage strategy class. If the file storage type parameter of the file to be stored is the same as the server type parameter value in the specific storage strategy class, the storage strategy class is obtained through the GetStrategy component. The function in the file server client corresponding to the storage strategy class is called through the storage strategy class. The file upload function in the file server client is called to upload the file to be stored to the corresponding file server, thus completing the file upload and storage. S5: File storage complete.
2. The file storage management method based on policy pattern as described in claim 1, characterized in that: In step S4, the file to be stored can also be downloaded to the corresponding file server by calling the file download function in the file server client through the storage strategy class; The file is deleted from the file server by calling the file delete function in the file server client through the storage strategy class.