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 use GridFs of Mongo to realize distributed file storage operation in SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "SpringBoot how to use Mongo's GridFs to realize distributed file storage operations". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

directory

preface

GridFs

When to use GridFs

Principles of GridFs

environment

Introducing dependencies and project configurations

Manipulating GridFs with GridFsTemplate

preface

During this period of time in the company internship, arranged to give me a task, so that in the system to achieve a knowledge base module, product said, just like Baidu net disk. I f * ck... that's exactly what they say,"this is a simple request, I don't care how it's done."

But how can I Google small expert admit defeat, originally also said to study FastDFS what, but because our project uses Mongo as a database, understand that Mongo comes with a distributed file system GridFs, this is simply God help me also.

GridFs Introduction When to use GridFs

We usually use Mongo can also directly save the binary file in Document, which is equivalent to mysql blob format, but Mongo limits Document to a maximum of 16MB, then we can do more than 16MB files, you can use GridFs to store.

In some cases, it may be more efficient to store large files in a MongoDB database than on a system-level file system.

If the file system limits the number of files in the directory, you can use GridFS to store as many files as you want. If you want to access information from parts of a large file without having to load the entire file into memory, you can use GridFS to call parts of the file without having to read the entire file into memory.

If you want to keep files and metadata automatically synchronized and deployed across multiple systems and facilities, use GridFS. With geographically dispersed replica sets, MongoDB can automatically distribute files and their metadata across multiple mongod instances and tools.

Principles of GridFs

Instead of storing files in a single document, GridFS divides files into sections or blocks and stores each block as a separate document.

GridFS uses two collections to store files. One set stores file blocks and the other stores file metadata.

By default, the size of each block is 255kB; all but the last block. The last block is only the necessary size. Similarly, files no larger than block size have only the final block, using only the required space and some extra metadata.

When querying the GridFS file, the driver reassembles the blocks as needed. Range queries can be performed on files stored through GridFS. Information can also be accessed from any part of the file, such as "skip" to the middle of a video or audio file.

environment

Spring Boot 2.0.4

Maven 3.5

Java 1.8

MongoDB 4.0

Robo 1.3.1

Introducing dependencies and project configurations

First add Mongo client dependencies

org.springframework.boot spring-boot-starter-data-mongodb

Then write the configuration file

spring: application: name: demo data: mongodb: uri: mongodb://root: your password @localhost:27019 authentication-database: admin database: your database using GridFsTemplate manipulation GridFs

GridFsTemplate is a client provided by Spring that specializes in manipulating GridFs and provides a series of out-of-the-box methods

Just inject it into our Conteoller, you can happily CRUD, need to note that when getting files to inject MongoDbFactory, we use the default configuration, direct injection is good.

import com.mongodb.Block;import com.mongodb.MongoClient;import com.mongodb.client.MongoDatabase;import com.mongodb.client.gridfs.GridFSBucket;import com.mongodb.client.gridfs.GridFSBuckets;import com.mongodb.client.gridfs.model.GridFSFile;import com.mongodb.client.gridfs.model.GridFSUploadOptions;import com.mongodb.gridfs.GridFS;import com.mongodb.gridfs.GridFSDBFile;import org.bson.BsonValue;import org.bson.Document;import org.bson.types.ObjectId;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse;import java.io.*; import java.util.*; @RestController@RequestMapping("/files")public class FileController { private Logger logger = LoggerFactory.getLogger(GridFsServiceImpl.class); //Regular matching file ID private static Pattern NUMBER_PATTERN = Pattern.compile("(?

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: 0

*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

Development

Wechat

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

12
Report