1、首先登陆七牛云(注册省略)
2、点击左上角按钮
3、选择对象存储Kodo
4、选择空间管理
5、如果没有创建空间先创建空间
7、域名选择七牛云给的测试域名
8、点击空间名称
9、后端代码
首先导入配置文件(选择自己要导入的版本)
java sdk 1.6
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[6.1.7, 6.999]</version>
</dependency>
java sdk 1.8及以上
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.13.0, 7.13.99]</version>
</dependency>
yml中加入配置信息
#自定义的七牛云配置信息
config:
ACCESS_KEY: 这里填写你的AccessKey
SECRET_KEY: 这里填写你的SecretKey
bucketName: 这里填写你的空间名称
endpoint: http://这里填写你的测试域名/
注意填写格式!!!!
或者properties中加入配置信息
#自定义的七牛云配置信息
config.ACCESS_KEY = 这里填写你的AccessKey
config.SECRET_KEY = 这里填写你的SecretKey
config.bucketName = 这里填写你的空间名称
config.endpoint = http://这里填写你的测试域名/
密钥查看的方式
创建bean对象 通过配置自动注入配置信息 (我这用的yml配置文件)
@ConfigurationProperties(prefix = "config") //设置配置属性,用于设置解析操作
@Component
@Data
public class QiNiuProperties {
private String ACCESS_KEY; //AccessKey密钥
private String SECRET_KEY; //SecretKey
private String endpoint; //测试域名
private String bucketName; //空间名称
}
java sdk 1.6
工具类
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.qiniu.api.auth.digest.Mac;
import java.io.InputStream;
import java.util.UUID;
@Component
public class QiNiuUtils {
@Autowired
private QiNiuProperties qiNiuProperties; //自动注入
public String upload(MultipartFile file) throws Exception {
String accessKey = qiNiuProperties.getACCESS_KEY(); //获取access密钥
String secretKey = qiNiuProperties.getSECRET_KEY(); //获取secret密钥
String bucketName = qiNiuProperties.getBucketName(); //获取空间名称
String endpoint = qiNiuProperties.getEndpoint(); //获取域名
Mac mac = new Mac(accessKey, secretKey);
// 请确保该bucket已经存在
PutPolicy putPolicy = new PutPolicy(bucketName);
String uptoken = putPolicy.token(mac);
//PutExtra extra = new PutExtra(); 可以为null
// 获取上传的文件的输入流
InputStream fileInputStream = file.getInputStream();
//将上传的文件名设置为唯一的 这样不会上传到云端时文件被覆盖
String filename = UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//IoApi.Put调用官方提供的工具上传文件 这里生成上传文件的名称 如***.jpg
PutRet ret = IoApi.Put(uptoken, filename, fileInputStream, null);
//根据上传的文件名称拼接前面域名
return endpoint + ret.getKey();
}
}
java sdk 1.8及以上的工具类
@Component
public class QiNiuUtilsNew {
@Autowired
private QiNiuProperties qiNiuProperties;
public String upload(MultipartFile file) throws IOException {
//构造一个带指定 Region 对象的配置类
//注意 Region.huanan() 选择你的存储区域,这里选的是华东-浙江
Configuration cfg = new Configuration(Region.huadong());
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = qiNiuProperties.getACCESS_KEY();
String secretKey = qiNiuProperties.getSECRET_KEY();
String bucket = qiNiuProperties.getBucketName();
String endpoint = qiNiuProperties.getEndpoint();
//生成文件名 使用 fileName替换掉key
String fileName = UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
InputStream inputStream = file.getInputStream();
//默认不指定key的情况下,以文件内容的hash值作为文件名
// String key = "";
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
//文件上传
// Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
Response response = uploadManager.put(inputStream, fileName, upToken, null, null);
if (response.statusCode == 200) {
//上传成功,将文件的链接返回
return endpoint + fileName;
}
} catch (QiniuException ex) {
ex.printStackTrace();
if (ex.response != null) {
System.err.println(ex.response);
try {
String body = ex.response.toString();
System.err.println(body);
} catch (Exception ignored) {
}
}
}
return "失败";
}
}
控制层
@RestController
@Slf4j
public class UploadController {
@Autowired
private QiNiuUtils qiNiuUtils;
/**
* 七牛云上传
* @param image
* @return
* @throws Exception
*/
@PostMapping("/upload")
public Result upload(MultipartFile image) throws Exception {
log.info("文件上传,文件名:{}",image.getOriginalFilename());
String url = qiNiuUtils.upload(image); //上传文件
return Result.success(url);
}
}