当前位置: 首页 > wzjs >正文

做网站业务的 怎么跑客户wordpress文章底部版权声明

做网站业务的 怎么跑客户,wordpress文章底部版权声明,软件网站开发公司,做阿里巴巴网站要多少钱在 ​​Spring Boot​​ 项目中,​​MinIO​​ 通常用于 ​​对象存储​​,类似于 AWS S3,可以存储图片、视频、文档等文件。以下是 ​​MinIO 在 Spring Boot 中的常见使用场景​​ 及 ​​详细使用过程​​。 1. MinIO 在 Spring Boot 中的…

        在 ​​Spring Boot​​ 项目中,​​MinIO​​ 通常用于 ​​对象存储​​,类似于 AWS S3,可以存储图片、视频、文档等文件。以下是 ​​MinIO 在 Spring Boot 中的常见使用场景​​ 及 ​​详细使用过程​​。


1. MinIO 在 Spring Boot 中的常见使用场景​

场景说明
​图片/文件上传​用户上传头像、商品图片、文档等,存储到 MinIO,返回 URL 给前端展示。
​文件下载​从 MinIO 下载文件(如 PDF、视频、图片等)。
​文件管理​删除、更新、查询文件(如按文件名、前缀搜索)。
​静态资源托管​将静态资源(如 HTML、CSS、JS)存储在 MinIO,并通过 CDN 加速访问。
​备份与归档​存储日志、数据库备份等大文件。

2. Spring Boot 整合 MinIO 的详细步骤​

​2.1 准备工作​

  1. ​安装 MinIO​​(本地或云服务器):

    • ​本地 Docker 运行 MinIO​​:
      docker run -p 9000:9000 -p 9001:9001 --name minio \-e "MINIO_ROOT_USER=minioadmin" \-e "MINIO_ROOT_PASSWORD=minioadmin" \quay.io/minio/minio server /data --console-address ":9001"

  

        访问 MinIO 控制台:http://localhost:9001(默认账号 minioadmin / minioadmin)。

  1. ​创建 Bucket​​(存储桶):

    • 在 MinIO 控制台创建一个 Bucket(如 grass-images)。

2.2 Spring Boot 项目集成 MinIO​

​(1) 添加 MinIO Java SDK 依赖​

在 pom.xml 中添加:

<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.4</version> <!-- 使用最新版本 -->
</dependency>
(2) 配置 MinIO 连接信息​

在 application.yml 或 application.properties 中配置:

minio:endpoint: http://localhost:9000  # MinIO 服务地址access-key: minioadmin           # MinIO 访问密钥secret-key: minioadmin           # MinIO 密钥bucket-name: grass-images        # 默认 Bucket 名称
(3) 创建 MinIO 配置类​
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MinioConfig {@Value("${minio.endpoint}")private String endpoint;@Value("${minio.access-key}")private String accessKey;@Value("${minio.secret-key}")private String secretKey;@Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}
}
​(4) 创建 MinIO 服务类​
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Service
public class MinioService {@Autowiredprivate MinioClient minioClient;@Value("${minio.bucket-name}")private String bucketName;/*** 上传文件到 MinIO*/public String uploadFile(MultipartFile file, String objectName) throws Exception {// 检查 Bucket 是否存在,不存在则创建boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());if (!isExist) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());}// 上传文件minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build());// 返回文件访问 URLreturn getFileUrl(objectName);}/*** 获取文件 URL(可设置过期时间)*/public String getFileUrl(String objectName) {// 生成预签名 URL(默认 7 天过期)GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(objectName).expiry(7, java.util.concurrent.TimeUnit.DAYS).build();try {return minioClient.getPresignedObjectUrl(args);} catch (Exception e) {throw new RuntimeException("获取文件 URL 失败", e);}}/*** 删除文件*/public void deleteFile(String objectName) throws Exception {minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());}/*** 获取 Bucket 下所有文件*/public List<Map<String, String>> listFiles() throws Exception {List<Map<String, String>> fileList = new ArrayList<>();Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).recursive(true).build());for (Result<Item> result : results) {Item item = result.get();Map<String, String> fileMap = new HashMap<>();fileMap.put("name", item.objectName());fileList.add(fileMap);}return fileList;}
}
(5) 创建 Controller 接口​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.util.List;
import java.util.Map;@RestController
@RequestMapping("/api/minio")
public class MinioController {@Autowiredprivate MinioService minioService;/*** 上传文件*/@PostMapping("/upload")public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {try {String objectName = System.currentTimeMillis() + "_" + file.getOriginalFilename();String fileUrl = minioService.uploadFile(file, objectName);return Map.of("url", fileUrl);} catch (Exception e) {throw new RuntimeException("文件上传失败", e);}}/*** 获取文件 URL*/@GetMapping("/url/{objectName}")public Map<String, String> getFileUrl(@PathVariable String objectName) {String url = minioService.getFileUrl(objectName);return Map.of("url", url);}/*** 删除文件*/@DeleteMapping("/delete/{objectName}")public Map<String, String> deleteFile(@PathVariable String objectName) {try {minioService.deleteFile(objectName);return Map.of("message", "文件删除成功");} catch (Exception e) {throw new RuntimeException("文件删除失败", e);}}/*** 获取所有文件*/@GetMapping("/list")public List<Map<String, String>> listFiles() {try {return minioService.listFiles();} catch (Exception e) {throw new RuntimeException("获取文件列表失败", e);}}
}

2.3 测试 MinIO 文件上传​

  1. ​前端调用上传接口​​(如 POST /api/minio/upload):

    const formData = new FormData();
    formData.append("file", fileInput.files[0]); // fileInput 是 <input type="file"> 元素fetch("http://localhost:8080/api/minio/upload", {method: "POST",body: formData,
    })
    .then(response => response.json())
    .then(data => console.log("文件 URL:", data.url));

    2.​​访问文件 URL​​:返回的 URL 可以直接在浏览器打开,如:

    http://localhost:9000/grass-images/1620000000000_example.jpg

3. 总结​

步骤说明
​1. 安装 MinIO​本地 Docker 或云服务器部署 MinIO。
​2. 创建 Bucket​在 MinIO 控制台创建存储桶(如 grass-images)。
​3. Spring Boot 集成​添加依赖、配置连接信息、创建服务类和 Controller。
​4. 文件上传/下载​使用 MinioClient 进行文件操作。
​5. 测试接口​前端调用上传接口,获取文件 URL 并展示。

这样,你的 Spring Boot 项目就可以轻松使用 MinIO 进行文件存储和管理了! 🚀

 


文章转载自:

http://GXnGZZpN.jghty.cn
http://tkd90cSH.jghty.cn
http://BETN1y9H.jghty.cn
http://Mk18hLKo.jghty.cn
http://TAMb5044.jghty.cn
http://SR3MLm1z.jghty.cn
http://vaPChfHW.jghty.cn
http://ag1wpNYr.jghty.cn
http://Tz2BdDWw.jghty.cn
http://ax507QHi.jghty.cn
http://Zk2snM1X.jghty.cn
http://GH03nqzC.jghty.cn
http://0YypxhvO.jghty.cn
http://HaE2Awet.jghty.cn
http://RSSSWvoi.jghty.cn
http://bIwtI5TE.jghty.cn
http://UskVpjdW.jghty.cn
http://bR1DKdzW.jghty.cn
http://j9uSXCE2.jghty.cn
http://WGgPk7x1.jghty.cn
http://dmyyoZaY.jghty.cn
http://834uKwTl.jghty.cn
http://52JwGa07.jghty.cn
http://QRdRRu4q.jghty.cn
http://S6zoDL8h.jghty.cn
http://iv3ZJZLK.jghty.cn
http://abSmNqlF.jghty.cn
http://KdMNYRb2.jghty.cn
http://9plSZa52.jghty.cn
http://g1sV1uAJ.jghty.cn
http://www.dtcms.com/wzjs/668925.html

相关文章:

  • 网站刷排名工具创新实用小产品设计
  • 昆明网站建设公司哪家便宜建e网站官网案例
  • 如何对网站用户分析wordpress 虚拟下载插件
  • 电商建网站运营东城建设网站
  • 地方门户网站怎么赚钱南昌网络营销公司
  • 部门网站集约化建设方案韩国设计教程网站
  • 海贼王路飞和女帝做的网站触摸屏互动网站建设案例
  • 河南省建设执业资格中心网站有哪些做场景秀的网站
  • 简述电子商务网站建设的过程获取免费域名
  • 简单网页制作代码模板网站怎么优化排名的方法
  • 做企业网站还有市场吗做英语阅读的网站
  • 网站开发准备网络加速器手机版
  • 网站优化检测工具shopify做全品类网站
  • 用phpcms建站的网站青岛海川建设集团网站
  • 手机端网站建设教程视频江门免费模板建站
  • 山东大汉建设机械有限公司网站网站建设好学么
  • 手机网站代码卸载 wordpress
  • 建网站入门2023新闻摘抄10条
  • wdcp网站无法访问.简述网站开发的流程
  • 蓝色网站设计汽车网址
  • 从事网站类网站建设的智慧团建注册入口
  • 理查德西尔斯做的网站电子政务网站建设方案
  • 河南省建设监理协会网站军事热点事件2022
  • 建设部网站事故快报微信里的小程序怎么添加
  • 房屋 哪个网站做的最好建设一个连接的网站
  • dw手机网站怎么做wordpress播放器修改
  • 增加网站访客手机商城积分兑换
  • 做海外视频的网站有哪些wordpress 本地视频链接
  • 阿里云网站建设部署与发布视频四川住房和城乡建设厅网站打不开
  • 网站建设意识形态工作自动化的网站建设