图片存储方案
图片存储方案-七牛云
新建存储空间
-
存储空间
选择如下 -
新建空间
新建空间
开发者中心
-
进入
进入开发者中心,之后选择对象存储 -
JavaSDK
JavaSDK
-
获取依赖
1
2
3
4
5<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.7.0, 7.10.99]</version>
</dependency>
鉴权
-
简介
Java SDK
的所有功能,都需要合法的授权, 授权凭证的签算需要七牛云账号下的一对有效的 Access Key
和Secret Key
密钥:https://portal.qiniu.com/user/key -
机房选择
机房 Region 华东 Region.region0()
,Region.huadong()
华北 Region.region1()
,Region.huabei()
华南 Region.region2()
,Region.huanan()
北美 Region.regionNa0()
,Region.beimei()
东南亚 Region.regionAs0()
,Region.xinjiapo()
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
55package com.coderitl.file.file;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
class FileApplicationTests {
void contextLoads() {
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "YKo8tJpMo7rj_0kPWLcNJ7ohYDU3bvzmI0TI6WFF";
String secretKey = "gpINj68_vTDxJzW6j2BnLgMuX9P0CJ0o3oLTAPXt";
String bucket = "coderitl";
//如果是 Windows 情况下,格式是 D:\\qiniu\\test.png
String localFilePath = "C:\\Users\\coderitl\\Desktop\\路线.png";
//默认不指定 key 的情况下,以文件内容的 hash 值作为文件名
String key = null;
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);
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
}
}
}
}重要参数 -
地理位置选择错误出现结果
地址位置问题 -
修改后
成功上传文件 -
删除文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void delete() {
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释
//...生成上传凭证,然后准备上传
String accessKey = "YKo8tJpMo7rj_0kPWLcNJ7ohYDU3bvzmI0TI6WFF";
String secretKey = "gpINj68_vTDxJzW6j2BnLgMuX9P0CJ0o3oLTAPXt";
String bucket = "coderitl";
String key = "Fkjk76jhorOTqDTJyfGJQ6j086f_";
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());
}
}删除文件的 id
封装工具类
-
工具类
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
74
75
76
77
78
79
80
81
82
83
84package com.coderitl.file.utils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
/**
* 七牛云工具类
*/
public class QiniuUtils {
//...生成上传凭证,然后准备上传
public static String accessKey = "YKo8tJpMo7rj_0kPWLcNJ7ohYDU3bvzmI0TI6WFF";
public static String secretKey = "gpINj68_vTDxJzW6j2BnLgMuX9P0CJ0o3oLTAPXt";
public static String bucket = "coderitl";
public static void upload2Qiniu(String filePath, String fileName) {
// 构造一个带指定 Zone 对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
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;
System.err.println(r.toString());
try {
System.out.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
}
}
// 字节
public static void upload2Qiniu(byte[] bytes, String fileName) {
Configuration cfg = new Configuration(Zone.zone2());
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);
} 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 deleteFileFromQinniu(String fileName) {
Configuration cfg = new Configuration(Zone.zone2());
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());
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 coder-itl!
评论