七牛云java基础版本
Posted SDingBa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了七牛云java基础版本相关的知识,希望对你有一定的参考价值。
最近用到了七牛云,挺好用的,先来讲一下官方的demo,之后会上传一个自己封装的utils.java文件
本文以 图片为例
更具体的请查看我的下一篇博客,关于七牛云的…….
g
Gradle :
compile 'com.qiniu:qiniu-java-sdk:7.x.+'
MAVEN:
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.0.0, 7.1.99]</version>
</dependency>
———-七牛云存储 是一种键值对的方式存储的形式,有点像redis 等NoSql存储
u
–如果是公开文件的话,在七牛云找到对应的url+key就能直接访问.
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = “Access_Key”;
String SECRET_KEY = “Secret_Key”;
上面的 key 都是官方对应的key,每个账号都是唯一的,最好不要告诉别人.
//要上传的空间
String bucketname = “Bucket_Name”;
上面的bucketname是上传的空间名称,需要自己在七牛云上面创建空间.
//上传到七牛后保存的文件名
String key = “my-java.png”;
上面的key表示你上传文件的名称(key值)
//上传文件的路径
String FilePath = “/…/…”;
文件上传的路径,对应本地电脑的文件路径,后续讲解怎么从浏览器的方式上传文件到服务器,然后上传到七牛云.
———-以下是官方的简单demo
简单上传
import com.qiniu.util.Auth;
import java.io.IOException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
public class UploadDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
//要上传的空间
String bucketname = "Bucket_Name";
//上传到七牛后保存的文件名
String key = "my-java.png";
//上传文件的路径
String FilePath = "/.../...";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//创建上传对象
UploadManager uploadManager = new UploadManager();
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
public String getUpToken()
return auth.uploadToken(bucketname);
public void upload() throws IOException
try
//调用put方法上传
Response res = uploadManager.put(FilePath, key, getUpToken());
//打印返回的信息
System.out.println(res.bodyString());
catch (QiniuException e)
Response r = e.response;
// 请求失败时打印的异常的信息
System.out.println(r.toString());
try
//响应的文本信息
System.out.println(r.bodyString());
catch (QiniuException e1)
//ignore
public static void main(String args[]) throws IOException
new UploadDemo().upload();
覆盖上传
import java.io.IOException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
public class UploadDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
//要上传的空间
String bucketname = "Bucket_Name";
//上传到七牛后保存的文件名
String key = "my-java.png";
//上传文件的路径
String filePath = "/.../...";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//创建上传对象
UploadManager uploadManager = new UploadManager();
// 覆盖上传
public String getUpToken()
//<bucket>:<key>,表示只允许用户上传指定key的文件。在这种格式下文件默认允许“修改”,已存在同名资源则会被本次覆盖。
return auth.uploadToken(bucketname, key);
public void upload() throws IOException
try
//调用put方法上传,这里指定的key和上传策略中的key要一致
Response res = uploadManager.put(filePath, key, getUpToken());
//打印返回的信息
System.out.println(res.bodyString());
catch (QiniuException e)
Response r = e.response;
// 请求失败时打印的异常信息
System.out.println(r.toString());
try
//响应的文本信息
System.out.println(r.bodyString());
catch (QiniuException e1)
//ignore
public static void main(String args[]) throws IOException
new UploadDemo().upload();
下载文件下载文件适合私有空间的存储
import com.qiniu.util.Auth;
public class DownloadDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//构造私有空间需要生成的下载的链接
String URL = "http://bucketdomain/key";
public void download()
//调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
String downloadRUL = auth.privateDownloadUrl(URL,3600);
System.out.println(downloadRUL);
public static void main(String args[])
new DownloadDemo().download();
管理文件 获取文件的信息
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
public class BucketManagerDemo
public static void main(String args[])
//设置需要操作的账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//要测试的空间和key,并且这个key在你空间中存在
String bucket = "Bucket_Name";
String key = "Bucket_key";
try
//调用stat()方法获取文件的信息
FileInfo info = bucketManager.stat(bucket, key);
System.out.println(info.hash);
System.out.println(info.key);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
———-移动单个文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
public class BucketManagerDemo
public static void main(String args[])
//设置需要操作的账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//要测试的空间和key,并且这个key在你空间中存在
String bucket = "Bucket_Name";
String key = "Bucket_key";
//将文件从文件key移动到文件key2, 可以在不同bucket移动,同空间移动相当于重命名
String key2 = "yourjavakey";
try
//调用move方法移动文件
bucketManager.move(bucket, key, bucket, key2);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
———-复制单个文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
public class BucketManagerDemo
public static void main(String args[])
//设置需要操作账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//要测试的空间和key,并且这个key在你空间中存在
String bucket = "Bucket_Name";
String key = "Bucket_key";
//将文件从文件key复制到文件key2,可以在不同bucket复制
String key2 = "yourjavakey";
try
//调用copy方法移动文件
bucketManager.copy(bucket, key, bucket, key2);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
———-删除单个文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
public class BucketManagerDemo
public static void main(String args[])
//设置需要操作账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//要测试的空间和key,并且这个key在你空间中存在
String bucket = "Bucket_Name";
String key = "Bucket_key";
try
//调用delete方法移动文件
bucketManager.delete(bucket, key);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
列举文件条目
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;
public class ListDemo
public static void main(String args[])
//设置需要操作账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//要列举文件的空间名
String bucket = "yourbucket";
try
//调用listFiles方法列举指定空间的指定文件
//参数一:bucket 空间名
//参数二:prefix 文件名前缀
//参数三:marker 上一次获取文件列表时返回的marker
//参数四:limit 每次迭代的长度限制,最大1000,推荐值100
//参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
FileListing fileListing = bucketManager.listFiles(bucket,null,null,10,null);
FileInfo[] items = fileListing.items;
for(FileInfo fileInfo:items)
System.out.println(fileInfo.key);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
抓取文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.util.Auth;
public class FetchDemo
public static void main(String args[])
//设置需要操作账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//文件保存的空间名和文件名
String bucket = "yourbucket";
String key = "yourkey";
//要fetch的url
String url = "url";
try
//调用fetch方法抓取文件
bucketManager.fetch(url, bucket,key);
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
批量操作文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;
public class BatchDemo
public static void main(String args[])
//设置需要操作账号的AK和SK
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
//创建Batch类型的operations对象
BucketManager.Batch operations = new BucketManager.Batch();
//第一组源空间名、原文件名,目的空间名、目的文件名
String bucketFrom1 = "yourbucket";
String keyFrom1 = "srckey1";
String bucketTo1 = "yourbucket";
String keyTo1 = "destkey1";
//第二组源空间名、原文件名,目的空间名、目的文件名
String bucketFrom2 = "yourbucket";
String keyFrom2 = "srckey2";
String bucketTo2 = "yourbucket";
String keyTo2 = "destkey2";
try
//调用批量操作的batch方法
Response res = bucketManager.batch(operations.move(bucketFrom1, keyFrom1, bucketTo1, keyTo1)
.move(bucketFrom2, keyFrom2, bucketTo2, keyTo2));
System.out.println(res.toString());
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
以上是关于七牛云java基础版本的主要内容,如果未能解决你的问题,请参考以下文章