七牛云使用Demo

前言

微服务中采用分布式架构,假设用户访问app时,服务可能通过集群配分配到海底或者山洞里的某个幸运服务器,这时图片单一部署在服务器需要采用统一URI,于是云存储图片服务获得URI进行部署即可解决问题。本次采用的是七牛云作为Demo测试(优惠)省。

七牛云前置工作环境搭建

1.注册登录

2.构建存储空间

<1>选择对象存储

新建存储空间

<2>进入新建空间,存储空间名称即入口,请依照规范标准命名;存储区域根据自己地理位置进行选择;访问控制公开

image-20220924154507736

<3>进入构建空间,获取系统分配的域名前缀以便程序中使用

<4>官方SDK工具中找到对应编程语言得SDK配置文档,嫌麻烦就直接找客服

<5>点击个人中心->密钥管理获取AccessKey/SecretKey

java案例

SDK工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class QiniuyunUtils {
// AccessKey
public static String accessKey = "ftn79KS0pDDPoGqtd9wNyNICRuMq_fqo9Jt3Gjj1";
// SecretKey
public static String secretKey = "pKUaRo5vxUKtZVSrCpy5J5kayUtpcvsu9ykbm1mh";
// 云空间名称
public static String bucket = "CloudDemo";

public static void upload2Qiniu(String filePath,String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone1());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(filePath, fileName, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
} catch (QiniuException ex) {
Response r = ex.response;
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}

//上传文件
public static void upload2Qiniu(byte[] bytes, String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone1());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);

//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(bytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}

//删除文件
public static void deleteFileFromQiniu(String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone1());
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
//如果遇到异常,说明删除失败
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class QinNiuTest {

@Test
public void testUpload(){
String filePath = "C:\\Users\\0\\Desktop\\avatar.jpg";
String fileName = "123.jpg";
QiniuyunUtils.upload2Qiniu(filePath,fileName);
}

@Test
public void testDelete(){
String fileName = "123.jpg";
QiniuyunUtils.deleteFileFromQiniu(fileName);
}
}


七牛云使用Demo
https://github.com/i-xiaoxin/2022/09/21/七牛云使用Demo/
作者
xiaoxinlore
发布于
2022年9月21日
许可协议