Java七牛云创建bucket新建空间
一、首先查看七牛云api文档可以得到请求方式
二、查看七牛云SDK菜单java SDK文档可以发现
1、gradle引用如下:
复制代码
1compile 'com.qiniu:qiniu-java-sdk:7.2.+'
2、maven引用如下:
复制代码
1
2
3
4
5<dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.2.0, 7.2.99]</version> </dependency>
三、需要注意七牛云在新建的时候需要验证个人中心里面的AccessKey和SeceretKey
四、java代码,使用的是maven演示
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62package com.spirits.bucket; import java.io.IOException; import com.qiniu.util.Auth; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * 七牛云新建bucket * * @author spirits * */ public class CreateBucket { /** * * @param accessKey 七牛云个人中心的key * @param secretKey 个人中心密码 * @param bucketName 空间名称 * @param storageArea 存储区域 */ public void createBucket(String accessKey,String secretKey,String bucketName,String storageArea) { Auth auth = Auth.create(accessKey,secretKey); String path = "/mkbucketv2/" + encode(bucketName.getBytes()) + "/region/"+ storageArea +"n"; String access_token = auth.sign(path); System.out.println(access_token); String url = "http://rs.qiniu.com/mkbucketv2/" + encode(bucketName.getBytes()) + "/region/" + storageArea; 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()); System.out.println(re.toString()); } else { System.out.println(re.code()); } } catch (IOException e) { e.printStackTrace(); } } /** * 编码 * * @param bstr * @return String */ public static String encode(byte[] bstr) { return new sun.misc.BASE64Encoder().encode(bstr); } /** * 测试 * * @param args */ public static void main(String[] args) { new CreateBucket().createBucket("xxxxxx", "xxxxxx","code_crate_bucket","z1"); } }
程序中bucket空间名需要转换为base64编码,通过re.code()可以知道创建是否成功,若返回200,则说明创建成功,此时恭喜你的七牛云上已经创建好空间了,可以打开七牛云对象存储验证。
最后
以上就是斯文猎豹最近收集整理的关于Java七牛云创建bucket新建空间的全部内容,更多相关Java七牛云创建bucket新建空间内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复