七牛云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();
    }

}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值