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

网友投稿 399 2022-11-05


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

maven导入依赖

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-devtools

runtime

true

mysql

mysql-connector-java

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

com.alibaba

fastjson

1.2.15

commons-lang

commons-lang

2.6

com.aliyun.oss

aliyun-sdk-oss

3.10.2

定义阿里云上传结果实体

package com.example.demo.entity;

import lombok.Data;

/**

* 阿里云上传结果集

*

* @author wushuai

* @create 2021-01-25

*/

@Data

public 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;

@Component

public 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);

// 关闭OSSChttp://lient。

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 objectNames) {

try {

// 创建OSSClient实例。

OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 批量删除文件。

DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames));

List 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测试


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:php代码调用python程序代码
下一篇:微信小程序:实现一组卡片翻转的效果
相关文章

 发表评论

暂时没有评论,来抢沙发吧~