1.注册七牛云账号
七牛云官网网址:https://www.qiniu.com/
七牛云注册网址:https://portal.qiniu.com/signup
根据网址一步步注册即可。
2.新建存储空间
(1)点击立即添加
(2)逐步填写点击确定即可
(3)点击空间管理查看
3.操作七牛云
七牛云开发者中心学习如何操作七牛云:
(1)跳转到对象存储开发页面,网址是:https://developer.qiniu.com/kodo
(2)找到java SDK开始学习:实例中只给出文件上传和删除其他可以自行实践哦!!!!
(3)使用Java SDK操作七牛云
(3.1)导入maven坐标
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.0</version>
</dependency>
(3.2)鉴权
Java SDK的所有的功能,都需要合法的授权。授权凭证的签算需要七牛账号下的一对有效的Access Key和Secret Key,这对密钥可以在七牛云管理控制台的个人中心获得,网址为:https://portal.qiniu.com/user/key
如下图所示。AK和SK十分重要,记得保存。
(3.3)上传/删除文件
以下代码中:
String accessKey = “你的accessKey”;
String secretKey = “你的secretKey”;
String bucket = “你的存储空间名字”;
localFilePath我是以桌面上的一个胡歌的图片为例,根据要上传的文件的地址修改即可。
删除文件时的 String key = “xxx”;这个key要去空间管理中查找
public class QiNiuTest {
// @Test
public void uploadFile(){
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "xxx";
String secretKey = "xxx";
String bucket = "xxx";
//如果是Windows情况下,格式是 D:\\qiniu\\test.png
String localFilePath = "C:\\Users\\lenovo\\Desktop\\51.jpg";
//默认不指定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
}
}
}
//@Test
public void deleteFile(){
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "xxx";
String secretKey = "xxx";
String bucket = "xxx";
//如果是Windows情况下,格式是 D:\\qiniu\\test.png
String key = "xxx";
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());
}
}
}
新增效果:其中红线圈起的就是这个文件的key,在删除时我们要删除哪一个就把对应的key给java代码即可
点击详情可以看到上传文件的详细信息:
删除效果:
(3.4)为了方便操作七牛云存储服务,我们可以将官方提供的案例简单改造成一个工具类
/**
* 七牛云工具类
*/
public class QiniuUtils {
public static String accessKey = "xxx";
public static String secretKey = "xxx";
public static String bucket = "xxx";
public static void upload2Qiniu(String filePath,String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
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.zone0());
//...其他参数参考类注释
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.zone0());
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());
}
}
}