1、什么是对象存储服务2、七牛云对象存储 Kodo 概述3、配置七牛云并进行测试4、Spring Boot集成七牛云
1、什么是对象存储服务2、七牛云对象存储 Kodo 概述对象存储服务(Object Storage Service)是用来描述解决和处理离散单元的方法的通用术语,这些离散单元被称作为对象。就像文件一样,对象包含数据,但是和文件不同的是,对象在一个层结构中不会再有层级结构。每个对象都在一个被称作存储池的扁平地址空间的同一级别里,一个对象不会属于另一个对象的下一级。
七牛云对象存储 Kodo 是七牛云提供的高可靠、强安全、低成本、可扩展的存储服务。可通过控制台、API、SDK 等方式简单快速地接入七牛存储服务,实现海量数据的存储和管理。通过 Kodo 可以进行文件的上传、下载和管理。
Kodo Browser可视化工具下载地址
3.1 注册七牛云帐号并实名认证领取免费存储空间
七牛云官网
3.2 进入控制台,选择对象存储Kodo,新建空间
3.3 上传文件进行测试
3.4 在浏览器地址栏中粘贴刚刚复制的外链,能够看到刚才上传的文件说明配置成功
官方sdk地址
4.1在pom.xml中添加maven依赖
com.qiniu qiniu-java-sdk 7.7.0
4.2 编写yml配置文件
qiniu:
kodo:
# 配置accessKey
accessKey: accessKey
# 配置secretKey
secretKey: secretKey
# 配置空间名称
bucket: bucket
# 配置域名
domain: domain
accessKey和secretKey在密钥管理中查询,空间名称如下图,域名在cdn—>域名管理中查询
4.3 新建并编写QiniuKodoUtil工具类
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.*;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@Component
public class QiniuKodoUtil {
Configuration cfg = new Configuration(Region.region2());
@Value("${qiniu.kodo.accessKey}")
String accessKey;
@Value("${qiniu.kodo.secretKey}")
String secretKey;
@Value("${qiniu.kodo.bucket}")
String bucket;
@Value("${qiniu.kodo.domain}")
String domain;
String prefix = "";
int limit = 1000;
String delimiter = "";
public void listSpaceFiles() {
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
while (fileListIterator.hasNext()) {
//处理获取的file list结果
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
System.out.println(item.key);
System.out.println(item.fsize / 1024 + "kb");
System.out.println(item.mimeType);
}
}
}
public void upload(String localFilePath) {
UploadManager uploadManager = new UploadManager(cfg);
String[] strings = localFilePath.split("\\");
String key = strings[strings.length - 1];
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}
public String getFileUrl(String fileName) throws UnsupportedEncodingException {
String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
String finalUrl = String.format("%s/%s", "http://" + domain, encodedFileName);
System.out.println(finalUrl);
return finalUrl;
}
public void deleteFile(String[] fileList) {
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucket, fileList);
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < fileList.length; i++) {
BatchStatus status = batchStatusList[i];
String key = fileList[i];
System.out.print(key + "t");
if (status.code == 200) {
System.out.println("delete success");
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
}
}
4.4 新建并编写QiniuKodoController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
@RestController
@RequestMapping("qiniukodo")
public class QiniuKodoController {
@Resource
QiniuKodoUtil qiniuKodoUtil;
@RequestMapping("upload")
public void upload(String localFilePath) {
qiniuKodoUtil.upload(localFilePath);
}
@RequestMapping("listSpaceFiles")
public void listSpaceFiles() {
qiniuKodoUtil.listSpaceFiles();
}
@RequestMapping("getFileUrl")
public void getFileUrl(String fileName) {
try {
qiniuKodoUtil.getFileUrl(fileName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@RequestMapping("deleteFile")
public void deleteFile(String[] fileList) {
qiniuKodoUtil.deleteFile(fileList);
}
}
4.5 测试接口是否可用
初始无文件
上传文件
列举空间文件列表
获取下载文件的链接
批量删除空间中的文件
为测试批量,我又上传了几个文件进行测试



