概述:
有存储对应的就会有删除,将很久不再访问或不在需要的资源文件进行清理,节省存储空间和存储的费用,同时也方便文件的管理,所以删除是很必要的; Tips:七牛不提供删除后,恢复数据的服务,所以,删除前请确认数据确实没有价值后,再进行该操作。
操作方式:
通过七牛的SDK提供的delete方法;通过七牛提供的delete接口;
tips:通过sdk调用delete方法,本质上其实还是调用七牛的delete接口来实现的,不过是通过sdk封装了一下,具体可以从github上查看七牛的sdk源码。 Qiniu-Java-Sdk 源码地址如下: https://github.com/qiniu/java-sdk/tree/v7.2.1
具体实现:
通过七牛sdk实现delete操作,代码示例如下:
package com.qiniu.kodo.manager;
import com.qiniu.base.AccountMgr;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
/**
* 删除文件
* @author xuhuanchao
*
*/
public class DeleteFile {
/**
* main method
* @param args
*/
public static void main(String[] args) {
Auth auth = Auth.create(AccountMgr.ACCESS_KEY, AccountMgr.SECRET_KEY);
Configuration config =
new Configuration(Zone.autoZone());
BucketManager bucketMgr =
new BucketManager(auth, config);
String bucketName =
"java-bucket";
String key =
"blob_11_9_01.png";
String key2 =
"blob_11_9_02.png";
try {
bucketMgr.delete(bucketName, key);
}
catch (QiniuException e) {
e.printStackTrace();
}
}
}
通过调用七牛的delete接口来实现,代码示例如下:
package
com.qiniu.kodo.manager
import java
.io.IOException
import
com.qiniu.base.AccountMgr
import
com.qiniu.common.Zone
import
com.qiniu.storage.BucketManager
import
com.qiniu.storage.Configuration
import
com.qiniu.util.Auth
import
com.qiniu.util.UrlSafeBase64
import okhttp3
.OkHttpClient
import okhttp3
.Request
import okhttp3
.Response
public class DeleteFileByInterface {
public static void main(String[] args) {
//获取Auth对象
Auth auth = Auth
.create(AccountMgr
.ACCESS_KEY, AccountMgr
.SECRET_KEY)
//指定需要删除的空间和文件,格式为: <bucket>:<key>
String entry =
"java-bucket:blob_11_9.png"
//通过安全base64编码方式进行编码处理
String encodedEntryURI = UrlSafeBase64
.encodeToString(entry)
//指定接口
String target =
"/delete/" + encodedEntryURI +
"\n"
//获取token,即操作凭证
String access_token = auth
.sign(target)
//指定好请求的delete接口地址
String url =
"http://rs.qiniu.com/delete/" + encodedEntryURI
System
.out.println(access_token)
//通过Okhttp jar 包封装的对象 发起网络请求
OkHttpClient client = new OkHttpClient()
Request request = new Request
.Builder()
.url(url)
.addHeader(
"Content-Type",
"application/x-www-form-urlencoded")
.addHeader(
"Authorization",
"QBox " + access_token)
.build()
Response re = null
try {
re = client
.newCall(request)
.execute()
if (re
.isSuccessful() == true) {
System
.out.println(re
.code())
} else {
System
.out.println(re
.code())
}
} catch (IOException e) {
e
.printStackTrace()
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-678091.html