七牛云Java API使用
Posted 慧强杨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了七牛云Java API使用相关的知识,希望对你有一定的参考价值。
1、引入jar
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.3</version>
</dependency>
2、上传文件
package com.hui.qiniu;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import java.io.IOException;
import java.util.UUID;
/**
* 上传到七牛返回 hash 和 key
*/
public class UploadDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
//要上传的空间
private static final String BUCKET_NAME = "myhui";
public static void main(String args[]) throws IOException
new UploadDemo().upload();
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
public String getUpToken()
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
return auth.uploadToken(BUCKET_NAME);
public void upload() throws IOException
//第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
//创建上传对象
UploadManager uploadManager = new UploadManager(c);
//上传到七牛后保存的文件名
String key = "image/test/" + UUID.randomUUID() + ".png";
//上传文件的路径
String FilePath = "D://1.jpg";
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.bodyString());
3、列出文件
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;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
/**
* 文件列表
*/
public class ListDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
public static void main(String args[])
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
Zone z = Zone.zone0();
Configuration c = new Configuration(z);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth, c);
//要列举文件的空间名
String bucket = "myhui";
try
//调用listFiles方法列举指定空间的指定文件
//参数一:bucket 空间名
//参数二:prefix 文件名前缀
//参数三:marker 上一次获取文件列表时返回的 marker
//参数四:limit 每次迭代的长度限制,最大1000,推荐值 100
//参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
FileListing fileListing = bucketManager.listFiles(bucket, null, null, 1000, 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());
4、获取文件信息
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Json;
/**
* 获取文件的hash 和key 实际上是一个没有什么卵用的方法
*/
public class FetchDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
public static void main(String args[])
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
Zone z = Zone.zone0();
Configuration c = new Configuration(z);
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth, c);
//文件保存的空间名和文件名
String bucket = "myhui";
String key = "ce81d939-437e-45ec-8580-13913cd1264c.png";
//要fetch的url
String url = "http://ojg32ej8x.bkt.clouddn.com/ce81d939-437e-45ec-8580-13913cd1264c.png";
try
//调用fetch方法抓取文件
DefaultPutRet fetch = bucketManager.fetch(url, bucket, key);
System.out.println("fetch = " + Json.encode(fetch));
catch (QiniuException e)
//捕获异常信息
Response r = e.response;
System.out.println(r.toString());
5、上传成功之后,回调方法
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;
import java.io.IOException;
import java.util.UUID;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
public class UploadCallBackDemo
//设置好账号的ACCESS_KEY和SECRET_KEY
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
//要上传的空间
private static final String BUCKET_NAME = "myhui";
//上传到七牛后保存的文件名
String key = "image/test/" + UUID.randomUUID() + ".png";
//上传文件的路径
String FilePath = "D://1.jpg";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
//创建上传对象
UploadManager uploadManager = new UploadManager(c);
//设置callbackUrl以及callbackBody,七牛将文件名和文件大小回调给业务服务器
public String getUpToken()
return auth.uploadToken(BUCKET_NAME, null, 3600, new StringMap()
.put("callbackUrl", "http://your.domain.com/callback")
.put("callbackBody", "filename=$(fname)&filesize=$(fsize)"));
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.bodyString());
public static void main(String args[]) throws IOException
new UploadCallBackDemo().upload();
以上是关于七牛云Java API使用的主要内容,如果未能解决你的问题,请参考以下文章