Skip to content

Commit

Permalink
Merge pull request #33 from JoshuaYin/master
Browse files Browse the repository at this point in the history
2.3.0
  • Loading branch information
joshua.yin authored Oct 12, 2019
2 parents 5e833ce + a6d7bbf commit e4063b9
Show file tree
Hide file tree
Showing 40 changed files with 1,419 additions and 146 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.2.1</version>
<version>2.3.0</version>
</dependency>
```

- JCenter
- Gradle

``` java
dependencies {
/*
* your other dependencies
*/
implementation 'cn.ucloud.ufile:ufile-client-java:2.2.1'
implementation 'cn.ucloud.ufile:ufile-client-java:2.3.0'
}
```

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.2.1</version>
<version>2.3.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Constants {
* 请修改下面的公私钥
*/
public static final ObjectAuthorization OBJECT_AUTHORIZER = new UfileObjectLocalAuthorization(
"YourBucket_PublicKey_Or_TokenPublicKey",
"YourBucket_PrivateKey");
System.getenv("UcloudPublicKey"),
System.getenv("UcloudPrivateKey"));

/**
* 远程Object相关API的签名器
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cn.ucloud.ufile.sample.object;

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.CopyObjectResultBean;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import okhttp3.Request;


/**
* @author: joshua
* @E-mail: [email protected]
* @date: 2018-12-11 14:32
*/
public class CopyObjectSample {
private static final String TAG = "CopyObjectSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
String srcBucket = "";
String srcKeyName = "";
String dstBucket = "";
String dstKeyName = "";
copyObjectAsync(srcBucket, srcKeyName, dstBucket, dstKeyName);
}

public static void copyObject(String srcBucket, String srcKeyName, String dstBucket, String dstKeyName) {
try {
CopyObjectResultBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.copyObject(srcBucket, srcKeyName)
.copyTo(dstBucket, dstKeyName)
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}

public static void copyObjectAsync(String srcBucket, String srcKeyName, String dstBucket, String dstKeyName) {
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.copyObject(srcBucket, srcKeyName)
.copyTo(dstBucket, dstKeyName)
.executeAsync(new UfileCallback<CopyObjectResultBean>() {

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

@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class ObjectListSample {
private static final String TAG = "ObjectListSample";
private static ObjectConfig config = new ObjectConfig("cn-bj", "ufileos.com");
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
String bucketName = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package cn.ucloud.ufile.sample.object;

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.ApiError;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.*;
import cn.ucloud.ufile.bean.ObjectListWithDirFormatBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import okhttp3.Request;

import java.util.ArrayList;
import java.util.List;

/**
* @author: joshua
* @E-mail: [email protected]
* @date: 2018-12-11 14:32
*/
public class ObjectListWithDirFormatSample {
private static final String TAG = "ObjectListWithDirFormatSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
String bucketName = "";

execute_list_all(bucketName);
}

//拉取一页列表
public static void execute(String bucketName) {
try {
ObjectListWithDirFormatBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectListWithDirFormat(bucketName)
/**
* 过滤前缀
*/
// .withPrefix("")
/**
* 分页标记
*/
// .withMarker("") //如果要拉下一页,withMarker 里要把 response.getNextMarker() 填进去,就可以拉下一页;
//如果 response.getNextMarker() 为"" 表示列表已经拉完了
//参考:https://github.com/ufilesdk-dev/elasticsearch-repository-ufile/blob/dev/src/main/java/org/elasticsearch/repository/ufile/UfileBlobStore.java,函数 listBlobsByPrefix
/**
* 分页数据上限,Default = 20
*/
// .dataLimit(10)
/**
* 目录分隔符,Default = '/',当前只支持是'/'
*/
.withDelimiter("/")
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}

//拉取全部文件列表
/*关于分页拉列表:
第一次拉: nextMarker 填 "", 返回值里,会有 nextMarker 的值 "xxxx", 返回 0-99 的结果
第二次拉: nextMarker 填 "xxxx", 就可以拉 100-199 的结果,通过会返回新的 nextMarker "yyyy"
第三次拉: nextMarker 填 "yyyy", 就可以拉 200-299 的结果,通过会返回新的 nextMarker "zzzz"
。。。。
直到, 如果拉到列表尾部了, nextMarker 会返回长度为0的串, 表示到达尾部; 就拉完了, 不需要继续拉列表
https://docs.ucloud.cn/api/ufile-api/prefix_file_list
*/
public static void execute_list_all(String bucketName) {
try {
String nextMarker = "";
List<CommonPrefix> directories = new ArrayList<>();
String prefix = "";
do {
ObjectListWithDirFormatBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectListWithDirFormat(bucketName)
.withMarker(nextMarker)
.dataLimit(100)
.withPrefix(prefix)
/**
* 目录分隔符,Default = '/',当前只支持是'/'
*/
.withDelimiter("/")
.execute();
//遍历结果
if (response == null)
break;

for (ObjectContentBean content : response.getObjectContents()) {
JLog.D(TAG, String.format("keyname: %s", content.getKey()));
}
for (CommonPrefix commonPrefix : response.getCommonPrefixes()) {
JLog.D(TAG, String.format("directory: %s", commonPrefix.getPrefix()));
}
//获取下一页
nextMarker = response.getNextMarker();
if (response.getCommonPrefixes() != null)
directories.addAll(response.getCommonPrefixes());
if (directories.isEmpty())
prefix = null;
if ((nextMarker == null || nextMarker.isEmpty()) && !directories.isEmpty()) {
nextMarker = "";
prefix = directories.remove(0).getPrefix();
}
} while ((nextMarker != null && nextMarker.length() > 0) || (prefix != null && prefix.length() > 0));

} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}

public static void executeAsync(String bucketName) {
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectListWithDirFormat(bucketName)
/**
* 过滤前缀
*/
// .withPrefix("")
/**
* 分页标记
*/
// .withMarker("")
/**
* 分页数据上限,Default = 20
*/
// .dataLimit(10)
/**
* 目录分隔符,Default = '/',当前只支持是'/'
*/
.withDelimiter("/")
.executeAsync(new UfileCallback<ObjectListWithDirFormatBean>() {

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

@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.bean.ObjectRestoreBean;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.sample.Constants;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cn.ucloud.ufile.sample.object;

import cn.ucloud.ufile.UfileClient;
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.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import okhttp3.Request;


/**
* @author: joshua
* @E-mail: [email protected]
* @date: 2018-12-11 14:32
*/
public class RenameObjectSample {
private static final String TAG = "RenameObjectSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
String bucketName = "";
String keyName = "";
String newKeyName = "";
boolean isForceToCover = false;
renameObject(bucketName, keyName, newKeyName, isForceToCover);
}

public static void renameObject(String bucketName, String keyName, String newKeyName, boolean isForceToCover) {
try {
BaseResponseBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.renameObject(bucketName, keyName)
.isRenamedTo(newKeyName)
/**
* 如果已存在同名文件,值为true则覆盖,否则操作失败;请求中若不携带该参数,默认不覆盖
*/
// .isForcedToCover(isForceToCover)
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}

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

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

@Override
public void onError(Request request, ApiError error, UfileErrorBean response) {
JLog.D(TAG, String.format("[error] = %s\n[info] = %s",
(error == null ? "null" : error.toString()),
(response == null ? "null" : response.toString())));
}
});
}

}
Loading

0 comments on commit e4063b9

Please sign in to comment.