Skip to content

Commit

Permalink
Merge pull request #36 from JoshuaYin/master
Browse files Browse the repository at this point in the history
## 2.4.1

* ObjectProfileApi新增CreateTime字段
* 所有Object的API均添加response headers Map<String,String>
* ObjectProfileSample新增批量调用demo
* 生成下载链接的API加入配置Content-Disposition的filename
  • Loading branch information
joshua.yin authored Jan 7, 2020
2 parents 45f5b7c + f637c68 commit fadd08a
Show file tree
Hide file tree
Showing 51 changed files with 647 additions and 184 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.4.0</version>
<version>2.4.1</version>
</dependency>
```

Expand All @@ -36,7 +36,7 @@
/*
* your other dependencies
*/
implementation 'cn.ucloud.ufile:ufile-client-java:2.4.0'
implementation 'cn.ucloud.ufile:ufile-client-java:2.4.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion ufile-sample-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.4.0</version>
<version>2.4.1</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import cn.ucloud.ufile.util.MimeTypeUtil;
import okhttp3.Request;


/**
* @author: joshua
* @E-mail: [email protected]
Expand All @@ -25,8 +25,8 @@ public class AppendObjectSample {

public static void main(String[] args) {
byte[] appendData = new byte[]{0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
String mimeType = "";
String keyName = "";
String mimeType = MimeTypeUtil.getMimeType(keyName);
String bucketName = "";
long posistion = 0;
appendObject(appendData, mimeType, bucketName, keyName, posistion);
Expand Down Expand Up @@ -59,7 +59,7 @@ public void onProgress(long bytesWritten, long contentLength) {
}
})
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
JLog.D(TAG, String.format("[res]: %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.bean.base.BaseObjectResponseBean;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
Expand All @@ -30,7 +30,7 @@ public static void main(String[] args) {

public static void execute(String keyName, String bucketName) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.deleteObject(keyName, bucketName)
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
Expand All @@ -44,10 +44,10 @@ public static void execute(String keyName, String bucketName) {
public static void executeAsync(String keyName, String bucketName) {
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.deleteObject(keyName, bucketName)
.executeAsync(new UfileCallback<BaseResponseBean>() {
.executeAsync(new UfileCallback<BaseObjectResponseBean>() {

@Override
public void onResponse(BaseResponseBean response) {
public void onResponse(BaseObjectResponseBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public static void main(String[] args) {
try {
String url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
/**
* 使用Content-Disposition: attachment,并且默认文件名为KeyName
*/
// .withAttachment()
/**
* 使用Content-Disposition: attachment,并且配置文件名
*/
// .withAttachment("filename")
.createUrl();
getStream(url, localDir, saveName);
} catch (UfileParamException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import cn.ucloud.ufile.util.JLog;
import okhttp3.Request;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
* @author: joshua
* @E-mail: [email protected]
Expand All @@ -25,15 +30,108 @@ public static void main(String[] args) {
String keyName = "";
String bucketName = "";

execute(keyName, bucketName);
// execute(keyName, bucketName);
List<Info> infos = new ArrayList<>();
infos.add(new Info(bucketName).setKeyName(""));
infos.add(new Info(bucketName).setKeyName(""));
infos.add(new Info(bucketName).setKeyName(""));
infos.add(new Info(bucketName).setKeyName(""));
infos.add(new Info(bucketName).setKeyName(""));
batch(infos);
}

private static class Info {
private String bucket;
private String keyName;

public Info(String bucket) {
this.bucket = bucket;
}

public String getBucket() {
return bucket;
}

public Info setBucket(String bucket) {
this.bucket = bucket;
return this;
}

public String getKeyName() {
return keyName;
}

public Info setKeyName(String keyName) {
this.keyName = keyName;
return this;
}
}

public static void batch(List<Info> infos) {
if (infos == null || infos.isEmpty())
return;

ExecutorService threadPool = new ThreadPoolExecutor(5, 10,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());

List<ObjectProfileCallable> callables = new ArrayList<>();
for (Info info : infos) {
callables.add(new ObjectProfileCallable(info));
}

try {
List<Future<ObjectProfile>> futures = threadPool.invokeAll(callables);
for (Future<ObjectProfile> future : futures) {
try {
JLog.D(TAG, "=====================================================================\n");
ObjectProfile objectProfile = future.get();
JLog.D(TAG, String.format("[res]: %s", (objectProfile == null ? "null" : objectProfile.toString())));
Map<String, String> headers = objectProfile.getHeaders();
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
JLog.D(TAG, "\t\t[key]:" + entry.getKey() + "\t[val]:" + entry.getValue());
}
}
} catch (ExecutionException e) {
JLog.D(TAG, String.format("[err]: %s", e.getMessage()));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
threadPool.shutdownNow();
}
}

private static class ObjectProfileCallable implements Callable<ObjectProfile> {
private Info info;

public ObjectProfileCallable(Info info) {
this.info = info;
}

@Override
public ObjectProfile call() throws Exception {
Thread.sleep(5000);
return UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectProfile(info.getKeyName(), info.getBucket())
.execute();
}
}

public static void execute(String keyName, String bucketName) {
try {
ObjectProfile objectProfile = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectProfile(keyName, bucketName)
.execute();
JLog.D(TAG, String.format("[res] = %s", (objectProfile == null ? "null" : objectProfile.toString())));
JLog.D(TAG, String.format("[res]: %s", (objectProfile == null ? "null" : objectProfile.toString())));
Map<String, String> headers = objectProfile.getHeaders();
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
JLog.D(TAG, "[key]:" + entry.getKey() + " [val]:" + entry.getValue());
}
}
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
Expand All @@ -49,6 +147,12 @@ public static void executeAsync(String keyName, String bucketName) {
@Override
public void onResponse(ObjectProfile response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
Map<String, String> headers = response.getHeaders();
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
JLog.D(TAG, "[key]:" + entry.getKey() + " [val]:" + entry.getValue());
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.bean.base.BaseObjectResponseBean;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;

/**
* @author: joshua
Expand All @@ -12,16 +13,16 @@
*/
public class ObjectRestoreSample {
private static final String TAG = "ObjectRestoreSample";
private static ObjectConfig config = new ObjectConfig("cn-gd", "ufileos.com");
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
String keyName = "a.jpg";
String bucketName = "ufile-test-gd";
String keyName = "";
String bucketName = "";
try {
BaseResponseBean a = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectRestore(keyName, bucketName)
.execute(); //同步调用,如果要用异步调用,请用 executeAsync(...)

JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.bean.base.BaseObjectResponseBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.UfileCallback;
Expand Down Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) {

public static void renameObject(String bucketName, String keyName, String newKeyName, boolean isForceToCover) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.renameObject(bucketName, keyName)
.isRenamedTo(newKeyName)
/**
Expand All @@ -56,10 +56,10 @@ public static void renameObjectAsync(String bucketName, String keyName, String n
* 如果已存在同名文件,值为true则覆盖,否则操作失败;请求中若不携带该参数,默认不覆盖
*/
// .isForcedToCover(isForceToCover)
.executeAsync(new UfileCallback<BaseResponseBean>() {
.executeAsync(new UfileCallback<BaseObjectResponseBean>() {

@Override
public void onResponse(BaseResponseBean response) {
public void onResponse(BaseObjectResponseBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.bean.base.BaseObjectResponseBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.UfileCallback;
Expand Down Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {

public static void switchStorageType(String bucketName, String keyName, String storageType) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.switchStorageType(bucketName, keyName)
.turnTypeTo(storageType)
.execute();
Expand All @@ -50,10 +50,10 @@ public static void switchStorageTypeAsync(String bucketName, String keyName, Str
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.switchStorageType(bucketName, keyName)
.turnTypeTo(storageType)
.executeAsync(new UfileCallback<BaseResponseBean>() {
.executeAsync(new UfileCallback<BaseObjectResponseBean>() {

@Override
public void onResponse(BaseResponseBean response) {
public void onResponse(BaseObjectResponseBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.bean.base.BaseObjectResponseBean;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
Expand Down Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {

public static void uploadHitFile(File file, String nameAs, String toBucket) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.uploadHit(file)
.nameAs(nameAs)
.toBucket(toBucket)
Expand All @@ -53,10 +53,10 @@ public static void uploadHitFileAsync(File file, String nameAs, String toBucket)
.uploadHit(file)
.nameAs(nameAs)
.toBucket(toBucket)
.executeAsync(new UfileCallback<BaseResponseBean>() {
.executeAsync(new UfileCallback<BaseObjectResponseBean>() {

@Override
public void onResponse(BaseResponseBean response) {
public void onResponse(BaseObjectResponseBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}

Expand All @@ -71,7 +71,7 @@ public void onError(Request request, ApiError error, UfileErrorBean response) {

public static void uploadHitStream(InputStream stream, String nameAs, String toBucket) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
BaseObjectResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.uploadHit(stream)
.nameAs(nameAs)
.toBucket(toBucket)
Expand All @@ -89,10 +89,10 @@ public static void uploadHitStreamAsync(InputStream stream, String nameAs, Strin
.uploadHit(stream)
.nameAs(nameAs)
.toBucket(toBucket)
.executeAsync(new UfileCallback<BaseResponseBean>() {
.executeAsync(new UfileCallback<BaseObjectResponseBean>() {

@Override
public void onResponse(BaseResponseBean response) {
public void onResponse(BaseObjectResponseBean response) {
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
}

Expand Down
Loading

0 comments on commit fadd08a

Please sign in to comment.