|
| 1 | +package com.qiniu.storage; |
| 2 | + |
| 3 | +import com.qiniu.common.Zone; |
| 4 | +import com.qiniu.http.Response; |
| 5 | +import com.qiniu.storage.Configuration; |
| 6 | +import com.qiniu.storage.UploadManager; |
| 7 | +import com.qiniu.util.Auth; |
| 8 | + |
| 9 | +/** |
| 10 | + * 华东 1 区域(杭州)和华北 2 区域(北京)的云主机 通过内网上传资源到七牛同区域的对象存储空间, |
| 11 | + * 并且避免绕行公网带来的网络质量不稳定问题,也可以免去数据在传输过程中被窃取的风险, |
| 12 | + * 参考文档:https://developer.qiniu.com/qvm/manual/4269/qvm-kodo |
| 13 | + */ |
| 14 | +public class QvmUploadDemo { |
| 15 | + // 获取凭证 |
| 16 | + public static void main(String[] args) { |
| 17 | + //设置账号的AK,SK |
| 18 | + String ACCESS_KEY = "Access_Key"; |
| 19 | + String SECRET_KEY = "Secret_Key"; |
| 20 | + //要上传的空间 |
| 21 | + String bucketname = "Bucket_Name"; |
| 22 | + //上传到七牛后保存的文件名 |
| 23 | + String key = "my-java.png"; |
| 24 | + //上传文件的路径 |
| 25 | + String FilePath = "/.../..."; |
| 26 | + // 构造一个带指定的zone对象的配置类,华东1区域的云主机可以选择Zone.qvmZone0(),华北2区域(北京)的云主机可以选择Zone.qvmZone1(),目前其他存储区域是不支持 |
| 27 | + Configuration configuration = new Configuration(Zone.qvmZone0()); |
| 28 | + //创建上传对象 |
| 29 | + UploadManager uploadManager = new UploadManager(configuration); |
| 30 | + //秘钥鉴权 |
| 31 | + Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); |
| 32 | + //简单上传,使用默认策略,只需要设置上传的空间名就可以了 |
| 33 | + String upToken = auth.uploadToken(bucketname); |
| 34 | + Response res = null; |
| 35 | + try { |
| 36 | + //调用上传方法 |
| 37 | + res = uploadManager.put(FilePath, key, upToken); |
| 38 | + } catch (QiniuException e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + //打印返回的信息 |
| 42 | + System.out.println(res.statusCode); |
| 43 | + System.out.println(res.toString()); |
| 44 | + } |
| 45 | +} |
0 commit comments