springBoot接入阿里云oss的实现步骤

时间:2021-05-19

maven导入依赖

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- 阿里云OSS --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> </dependencies>

定义阿里云上传结果实体

package com.example.demo.entity;import lombok.Data;/** * 阿里云上传结果集 * * @author wushuai * @create 2021-01-25 */@Datapublic class AliyunOssResult { /** * code:200成功 * code: 400失败 */ private int code; /** * 上传成功的返回url */ private String url; /** * 提示信息 */ private String msg;}

yml设置阿里云oss参数

aliyunOss: endpoint: "http://oss-cn-shanghai.aliyuncs.com" accessKeyId: "xxxxxxx" accessKeySecret: "xxxxxxx" bucketName: "xxxxxx" urlPrefix: "http://bucketName.oss-cn-shanghai.aliyuncs.com/"

yml设置上传文件大小限制

spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB

工具类封装

package com.example.demo.util;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.model.DeleteObjectsRequest;import com.aliyun.oss.model.DeleteObjectsResult;import com.aliyun.oss.model.GeneratePresignedUrlRequest;import com.example.demo.entity.AliyunOssResult;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.io.InputStream;import java.net.URL;import java.util.Date;import java.util.List;@Componentpublic class AliyunOSSUtil { @Value("${aliyunOss.endpoint}") private String endpoint; @Value("${aliyunOss.accessKeyId}") private String accessKeyId; @Value("${aliyunOss.accessKeySecret}") private String accessKeySecret; @Value("${aliyunOss.bucketName}") private String bucketName; @Value("${aliyunOss.urlPrefix}") private String urlPrefix; /** * 上传文件,以IO流方式 * * @param inputStream 输入流 * @param objectName 唯一objectName(在oss中的文件名字) */ public AliyunOssResult upload(InputStream inputStream, String objectName) { AliyunOssResult aliyunOssResult = new AliyunOssResult(); try { // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。 ossClient.putObject(bucketName, objectName, inputStream); // 关闭OSSClient。 ossClient.shutdown(); aliyunOssResult.setCode(200); aliyunOssResult.setUrl(urlPrefix+objectName); aliyunOssResult.setMsg("上传成功"); } catch (Exception e) { e.printStackTrace(); aliyunOssResult.setCode(400); aliyunOssResult.setMsg("上传失败"); } return aliyunOssResult; } /** * 删除OSS中的单个文件 * * @param objectName 唯一objectName(在oss中的文件名字) */ public void delete(String objectName) { try { // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 删除文件。 ossClient.deleteObject(bucketName, objectName); // 关闭OSSClient。 ossClient.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * 批量删除OSS中的文件 * * @param objectNames oss中文件名list */ public void delete(List<String> objectNames) { try { // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 批量删除文件。 DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames)); List<String> deletedObjects = deleteObjectsResult.getDeletedObjects(); // 关闭OSSClient。 ossClient.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取文件临时url * * @param objectName oss中的文件名 * @param effectiveTime 有效时间(ms) */ public String getUrl(String objectName,long effectiveTime){ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 设置URL过期时间 Date expiration = new Date(new Date().getTime() + effectiveTime); GeneratePresignedUrlRequest generatePresignedUrlRequest ; generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, objectName); generatePresignedUrlRequest.setExpiration(expiration); URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest); return url.toString(); }}

controller接收调用

package com.example.demo.controller;import com.example.demo.util.AliyunOSSUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.io.IOException;import java.util.UUID;@RestController@RequestMapping("/file")public class FileController { @Autowired private AliyunOSSUtil aliyunOSSUtil; @RequestMapping(value = "/uploadFile") public @ResponseBody Object uploadFile(@RequestParam(value = "file", required = false) MultipartFile file, String strPath) throws IOException { String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); String objectName = strPath+"/"+ UUID.randomUUID().toString()+"."+suffix; return aliyunOSSUtil.upload(file.getInputStream(),objectName); }}

postman测试

到此这篇关于springBoot接入阿里云oss的实现步骤的文章就介绍到这了,更多相关springBoot接入阿里云oss内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章