Skip to content

Commit

Permalink
Merge pull request #362 from sxci/add_bucket_private
Browse files Browse the repository at this point in the history
 add bucketInfo, setBucketAcl, setIndexPage [ci skip]
  • Loading branch information
longbai authored Sep 18, 2018
2 parents f52e776 + 6d46407 commit adc3b4f
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class Constants {
/**
* 版本号
*/
public static final String VERSION = "7.2.16";
public static final String VERSION = "7.2.17";
/**
* 块大小,不能改变
*/
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.storage.model.*;
import com.qiniu.util.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -98,7 +96,7 @@ public String[] buckets() throws QiniuException {
return r.jsonToObject(String[].class);
}

public void createBucket(String bucketName, String region) throws Exception {
public void createBucket(String bucketName, String region) throws QiniuException {
String url = String.format("%s/mkbucketv2/%s/region/%s", configuration.rsHost(),
UrlSafeBase64.encodeToString(bucketName), region);
post(url, null).close();
Expand Down Expand Up @@ -233,14 +231,6 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
}


//存储类型
public enum StorageType {
//普通存储
COMMON,
//低频存储
INFREQUENCY
}

/**
* 修改文件的类型(普通存储或低频存储)
*
Expand Down Expand Up @@ -510,6 +500,33 @@ public Response deleteAfterDays(String bucket, String key, int days) throws Qini
return rsPost(bucket, String.format("/deleteAfterDays/%s/%d", encodedEntry(bucket, key), days), null);
}

public void setBucketAcl(String bucket, AclType acl) throws QiniuException {
String url = String.format("%s/private?bucket=%s&private=%s", configuration.ucHost(), bucket, acl.getType());
Response res = post(url, null);
res.close();
if (!res.isOK()) {
throw new QiniuException(res);
}
}

public BucketInfo getBucketInfo(String bucket) throws QiniuException {
String url = String.format("%s/v2/bucketInfo?bucket=%s", configuration.ucHost(), bucket);
Response res = post(url, null);
if (!res.isOK()) {
res.close();
throw new QiniuException(res);
}
BucketInfo info = res.jsonToObject(BucketInfo.class);
return info;
}


public void setIndexPage(String bucket, IndexPageType type) throws QiniuException {
String url = String.format("%s/noIndexPage?bucket=%s&noIndexPage=%s", configuration.ucHost(), bucket, type.getType());
Response res = post(url, null);
}


/*
* 相关请求的方法列表
* */
Expand Down Expand Up @@ -722,4 +739,5 @@ public void remove() {
throw new UnsupportedOperationException("remove");
}
}

}
13 changes: 11 additions & 2 deletions src/main/java/com/qiniu/storage/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public final class Configuration implements Cloneable {
*/
public Zone zone;
/**
* 空间相关上传管理操作是否使用 https , 默认否
* 空间相关上传管理操作是否使用 https , 默认 是
*/
public boolean useHttpsDomains = false;
public boolean useHttpsDomains = true;
/**
* 如果文件大小大于此值则使用断点上传, 否则使用Form上传
*/
Expand Down Expand Up @@ -74,6 +74,7 @@ public final class Configuration implements Cloneable {
*/
public static String defaultRsHost = "rs.qiniu.com";
public static String defaultApiHost = "api.qiniu.com";
public static String defaultUcHost = "uc.qbox.me";

public Configuration() {

Expand Down Expand Up @@ -165,4 +166,12 @@ public String rsfHost(String ak, String bucket) {
return useHttpsDomains ? zone.getRsfHttps(zoneReqInfo)
: zone.getRsfHttp(zoneReqInfo);
}

public String ucHost() {
String scheme = "http://";
if (useHttpsDomains) {
scheme = "https://";
}
return scheme + defaultUcHost;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/qiniu/storage/model/AclType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.qiniu.storage.model;

/**
* 空间类型:公开空间、私有空间
*/
public enum AclType {
/**
* 公开空间
*/
PUBLIC(0),
/**
* 私有空间
*/
PRIVATE(1);

private int type = 0;

AclType(int t) {
type = t;
}

public int getType() {
return type;
}
}
112 changes: 112 additions & 0 deletions src/main/java/com/qiniu/storage/model/BucketInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.qiniu.storage.model;

import com.google.gson.annotations.SerializedName;

import java.util.Map;

public class BucketInfo {
@SerializedName("source")
private String imageSource;
@SerializedName("host")
private String imageHost;
@SerializedName("protected")
private int _protected;
@SerializedName("private")
private int _private;
@SerializedName("no_index_page")
private int noIndexPage;
private int imgsft;
private String separator;
private Map<String, String> styles;
private String zone;
private String region;
private boolean global;

public String getImageSource() {
return imageSource;
}

public void setImageSource(String imageSource) {
this.imageSource = imageSource;
}

public String getImageHost() {
return imageHost;
}

public void setImageHost(String imageHost) {
this.imageHost = imageHost;
}

public int getProtected() {
return _protected;
}

public void setProtected(int _protected) {
this._protected = _protected;
}

public int getPrivate() {
return _private;
}

public void setPrivate(int _private) {
this._private = _private;
}

public int getNoIndexPage() {
return noIndexPage;
}

public void setNoIndexPage(int noIndexPage) {
this.noIndexPage = noIndexPage;
}

public int getImgsft() {
return imgsft;
}

public void setImgsft(int imgsft) {
this.imgsft = imgsft;
}

public String getSeparator() {
return separator;
}

public void setSeparator(String separator) {
this.separator = separator;
}

public Map<String, String> getStyles() {
return styles;
}

public void setStyles(Map<String, String> styles) {
this.styles = styles;
}

public String getZone() {
return zone;
}

public void setZone(String zone) {
this.zone = zone;
}

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public boolean isGlobal() {
return global;
}

public void setGlobal(boolean global) {
this.global = global;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/qiniu/storage/model/IndexPageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.qiniu.storage.model;

public enum IndexPageType {
HAS(0),
NO(1);

private int type = 0;

IndexPageType(int t) {
type = t;
}

public int getType() {
return type;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/qiniu/storage/model/StorageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.qiniu.storage.model;

/**
* 存储类型
*/
public enum StorageType {
/**
* 普通存储
*/
COMMON,
/**
* 低频存储
*/
INFREQUENCY
}
20 changes: 10 additions & 10 deletions src/test/java/test/com/qiniu/DnsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public void setUp() throws UnknownHostException {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
InetAddress[] ips;
try{
Domain domain = new Domain(hostname, true, true);
ips = dnsClient.queryInetAddress(domain);
} catch (IOException e) {
e.printStackTrace();
throw new UnknownHostException(e.getMessage());
}
try {
Domain domain = new Domain(hostname, true, true);
ips = dnsClient.queryInetAddress(domain);
} catch (IOException e) {
e.printStackTrace();
throw new UnknownHostException(e.getMessage());
}
if (ips == null) {
throw new UnknownHostException(hostname + " resolve failed.");
}
List<InetAddress> l = new ArrayList<>(ips.length);
throw new UnknownHostException(hostname + " resolve failed.");
}
List<InetAddress> l = new ArrayList<>(ips.length);
Collections.addAll(l, ips);
return l;
}
Expand Down
Loading

0 comments on commit adc3b4f

Please sign in to comment.