springboot获取七牛云文件上传凭证token
springboot获取七牛云文件上传凭证token
1.pom.xml中添加依赖:
<!-- 七牛云 -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.13.0</version>
</dependency>
2.获取密钥
登陆七牛云获取
https://portal.qiniu.com/user/key
3.在yml中添加七牛云配置信息
qiniu:
accessKey: ******************************
secretKey: **********************************
bucket: ******************************* # 创建的存储空间名 https://portal.qiniu.com/kodo/bucket
path: **************** # 对应存储空间的访问域名
expireSeconds: 1800 #policy过期时间 秒
4.创建配置文件QiniuConfig :
package com.xxbb.springbootapi.config;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 七牛云配置
*
* @Auther: yanghaoxing
* @Date: 2024/10/31
*/
@AllArgsConstructor//有参数构造
@Accessors(chain = true)//链式调用
@NoArgsConstructor//无参数构造
@Data
@Configuration
public class QiniuConfig {
@Value("${qiniu.accessKey}")
private String accessKey;
@Value("${qiniu.secretKey}")
private String secretKey;
@Value("${qiniu.bucket}")
private String bucket;
@Value("${qiniu.path}")
private String path;
@Value("${qiniu.expireSeconds}")
private Long expireSeconds;
/**
* 华东机房
*/
public com.qiniu.storage.Configuration qnConfig() {
return new com.qiniu.storage.Configuration(Region.region0());
}
/**
* 构建一个七牛上传工具实例
*/
public UploadManager uploadManager() {
return new UploadManager(qnConfig());
}
/**
* 认证信息实例
*
* @return
*/
public Auth auth() {
return Auth.create(accessKey, secretKey);
}
/**
* 构建七牛空间管理实例
*/
public BucketManager bucketManager() {
return new BucketManager(auth(), qnConfig());
}
}
5.Controller:
package com.xxbb.springbootapi.controller;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.storage.model.FetchRet;
import com.xxbb.springbootapi.config.AppConfig;
import com.xxbb.springbootapi.config.QiniuConfig;
import com.xxbb.springbootapi.entity.dto.JsonResult;
import com.xxbb.springbootapi.entity.dto.JsonResultData;
import com.xxbb.springbootapi.entity.dto.NetAddress;
import com.xxbb.springbootapi.service.IFileService;
import com.xxbb.springbootapi.service.impl.FileService;
import com.xxbb.springbootapi.service.impl.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
@Slf4j
@RequestMapping("file")
@Api(tags = "文件操作")
@RestController
public class FileController {
@Autowired
private QiniuConfig qiniuConfig;
@GetMapping("/qiniu/policy")
@ApiOperation("获取上传七牛凭证token")
public JsonResultData<?> qiniuPolicy() {
String uploadToken = qiniuConfig.auth().uploadToken(qiniuConfig.getBucket(), null, 3600, null);
return new JsonResultData<>(uploadToken).Success();
}
}
测试接口正常获取:
拿到token,后续可以直接在前端做文件上传到七牛云,当然也可以在后端做上传处理。