全部产品

Java SDK 简介

更新时间:2020-08-31 15:33:02

下载地址

https://github.com/aliyun/alibabacloud-pds-sdk

安装步骤

安装 Java 开发环境

目前,PDS Java SDK 支持 J2SE 6.0 及以上的 Java 运行环境,您可以从 Java 官方网站 下载并按说明安装 Java 开发环境。

安装PDS Java SDK

安装完 Java 开发环境后,您需要安装PDS SDK,将下面的依赖加入 pom.xml 。

StandardMode

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-sdk-pds</artifactId>
  4. <version>RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>credentials-java</artifactId>
  9. <version>0.1.0</version>
  10. </dependency>

注意:请关注 Git Hub提供的最新版本,查看相关功能并选择使用。

初始化Client

使用前提是您已经开通PDS服务, 并且在控制台创建了域实例。参见快速搭建云盘服务。之后您可以按需初始化StandardModeClient 或者 HostingModeClient。

AK & SK 初始化客户端

AK, SK 的获取详见 密钥管理页面。选择一对用于 SDK 的访问密钥对。如果没有,请创建一对新访问密钥,且保证它处于启用状态。有关如何创建访问密钥,参见 创建访问密钥

  1. import com.aliyun.pds.client.Client;
  2. import com.aliyun.pds.client.models.*;
  3. public class Demo {
  4. private static Client client;
  5. public static void createClient() throws IllegalAccessException {
  6. Config config = new Config();
  7. config.domainId = "your domainId";
  8. config.protocol = "https";
  9. config.accessKeyId = System.getenv("accessKeyId");
  10. config.accessKeySecret = System.getenv("accessKeySecret");
  11. client = new Client(config);
  12. }
  13. }

AccessToken & RefreshToken 初始化客户端

clientId, clientSecret 的获取详见应用接入指南

  1. import com.aliyun.pds.client.Client;
  2. import com.aliyun.pds.client.models.*;
  3. public class Demo {
  4. private static Client client;
  5. public static void createClient() throws IllegalAccessException {
  6. Config config = new Config();
  7. config.domainId = "your domainId";
  8. config.protocol = "https";
  9. config.clientId = System.getenv("clientId");
  10. config.clientSecret = System.getenv("clientSecret");
  11. config.accessToken = System.getenv("accessToken");
  12. config.refreshToken = System.getenv("refreshToken");
  13. config.expireTime = System.getenv("expireTime");
  14. client = new Client(config);
  15. }
  16. }

注意:AK & SK 模式 和 Access Token & Refresh Token 模式 同时只能存在一种

构造请求

Account 相关 API

获取图片验证码

  • 以下代码用于获取图片验证码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public static void getCaptcha() throws Exception {
  2. try {
  3. GetCaptchaRequest getCaptchaRequest = new GetCaptchaRequest();
  4. getCaptchaRequest.appId = "***********";
  5. GetCaptchaModel captcha = client.getCaptcha(getCaptchaRequest);
  6. // 打印结果
  7. System.out.println(new Gson().toJson(captcha.body.captcha));
  8. } catch (TeaException e) {
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getCode());
  11. System.out.println(e.getData());
  12. }
  13. }
  • 返回结果
  1. {
  2. "captcha":"iVBORw0KGgoAAAANSUhEUgAAAFAAAAAaCAIAAACvsEzwAAABFUlEQVR42u3YwQ7CIAwGYOKDaLya7OBL+BDePRoT4/vPJSSEjFJ+2oEIW3oy0PHRDnVmHuwyO3gH93J9Xm85+PA4+REOmKYLGbGE5/vVBn/f2/O4hMZs9FrSjIMdtVEwKSSLTG7Bb8HZLc30MALmB7QLlmkHAiPa3sDIsE7AYHkbAtvvaw0YHNlQhRezZeee0nh5BWC/EkVamjHHtqAoWGbOe4ZtqZFfWrmHcxhIhYuD3Z2S2gpgmVn450G2u8i5mOzn1aFlV7LKwCQ0FRaqTOWcsSPazvUzZIPJbStaahCMT48lNIjHD0GpkQzKftkMzK9e8/yHecLPwZ0ihZuBK7yFKTq9uXdamp7/M7Df22JPcsxw4C/GgzEzOWpc1QAAAABJRU5ErkJggg==",
  3. "captchaFormat":"png",
  4. "captchaId":"f644d0b2a7d21a3caddb17377c90c28e8DPXYUIfvs3"
  5. }

获取短信验证码

  • 以下代码用于获取短信验证码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getMNSCode() throws Exception {
  2. try {
  3. MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest();
  4. sendSmsCodeRequest.appId = "your app id";
  5. sendSmsCodeRequest.phoneNumber = "13**********5";
  6. sendSmsCodeRequest.type = "change_password";
  7. MobileSendSmsCodeModel response = client.mobileSendSmsCode(sendSmsCodeRequest);
  8. System.out.println(new Gson().toJson(response.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getCode());
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getData());
  13. }
  14. }
  • 返回结果
  1. {
  2. "smsCodeId":"b40bba70b37d74**********************6j3IW7HP"
  3. }

验证手机号是否注册

  • 以下代码用于验证手机号是否存在,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void checkMobileIsExist() throws Exception {
  2. try {
  3. MobileCheckExistRequest checkExistRequest = new MobileCheckExistRequest();
  4. checkExistRequest.appId = "your app id";
  5. checkExistRequest.phoneNumber = "13*****25";
  6. CheckExistModel checkExistResponse = client.checkExist(checkExistRequest);
  7. System.out.println(new Gson().toJson(checkExistResponse.body));
  8. } catch (TeaException e) {
  9. System.out.println(e.getCode());
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getData());
  12. }
  13. }
  • 返回结果
  1. {
  2. "isExist":true,
  3. "phoneNumber":"13****25"
  4. }

手机号注册

  • 以下代码用于手机号注册,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void register() throws Exception {
  2. try {
  3. MobileRegisterRequest registerRequest = new MobileRegisterRequest();
  4. registerRequest.appId = "your app id";
  5. registerRequest.phoneNumber = "132****225";
  6. registerRequest.smsCode = "0****0";
  7. registerRequest.smsCodeId = "674e************JRuoQgRu0R";
  8. RegisterModel tokenResponse = client.register(registerRequest);
  9. System.out.println(new Gson().toJson(tokenResponse.body));
  10. } catch (TeaException e) {
  11. System.out.println(e.getCode());
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getData());
  14. }
  15. }
  • 返回结果
  1. {
  2. "access_token": "eyJhbGc***************iOiJSUzI1NiI",
  3. "refresh_token": "fvw1FLZ************tGjXxJl",
  4. "expires_in": 7200,
  5. "token_type": "Bearer",
  6. "user_id": "6c23c98d3***************3f9f5211",
  7. "user_name": "132******225",
  8. "avatar": "",
  9. "nick_name": "1329*****225",
  10. "default_drive_id": "",
  11. "role": "admin",
  12. "expire_time": "2019-10-30T11:44:24Z",
  13. "state": "",
  14. "exist_link": [],
  15. "need_link": false,
  16. "user_data": {}
  17. }

手机号短信登录

  • 以下代码用于短信登录,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void mnsLogin() throws Exception {
  2. try {
  3. // mns login
  4. MobileLoginRequest mobileLoginRequest = new MobileLoginRequest();
  5. mobileLoginRequest.appId = "your app id";
  6. mobileLoginRequest.phoneNumber = "132**********#=&";
  7. mobileLoginRequest.smsCode = "123456";
  8. mobileLoginRequest.smsCodeId = "your sms code";
  9. LoginModel tokenResponse = client.login(mobileLoginRequest);
  10. System.out.println(new Gson().toJson(tokenResponse));
  11. } catch (TeaException e) {
  12. System.out.println(e.getCode());
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getData());
  15. }
  16. }

设置登录密码

  • 以下代码用于设置登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void setPassWord() throws Exception {
  2. try {
  3. DefaultSetPasswordRequest setPasswordRequest = new DefaultSetPasswordRequest();
  4. setPasswordRequest.appId = "your app id";
  5. setPasswordRequest.newPassword = "1234567";
  6. setPasswordRequest.state = "";
  7. client.setPassword(setPasswordRequest);
  8. } catch (TeaException e) {
  9. System.out.println(e.getCode());
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getData());
  12. }
  13. }

密码登录

  • 以下代码用于密码登录,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 密码登录
  2. public static void passwordLogin() throws Exception {
  3. try {
  4. MobileLoginRequest mobileLoginRequest = new MobileLoginRequest();
  5. mobileLoginRequest.appId = "your app id";
  6. mobileLoginRequest.phoneNumber = "132******25";
  7. mobileLoginRequest.password = "123********456";
  8. LoginModel tokenResponse = client.login(mobileLoginRequest);
  9. System.out.println(new Gson().toJson(tokenResponse.body));
  10. } catch (TeaException e) {
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getCode());
  13. System.out.println(e.getData());
  14. }
  15. }

修改登录密码

  • 以下代码用于修改登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void changePassWord() throws Exception {
  2. try {
  3. DefaultChangePasswordRequest changePasswordRequest = new DefaultChangePasswordRequest();
  4. changePasswordRequest.appId = "your app id";
  5. changePasswordRequest.phoneNumber = "13*****25";
  6. changePasswordRequest.newPassword = "12*****9";
  7. changePasswordRequest.state = "6*****73";
  8. changePasswordRequest.encryptedKey = "Your EncryptedKey";
  9. client.changePassword(changePasswordRequest);
  10. } catch (TeaException e) {
  11. System.out.println(e.getCode());
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getData());
  14. }
  15. }

通过刷新令牌获取访问令牌

  • 以下代码用于刷新token,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getTokenByRefreshToken() throws Exception {
  2. try {
  3. TokenRequest tokenRequest = new TokenRequest();
  4. tokenRequest.appId = "your app id";
  5. tokenRequest.grantType = "refresh_token";
  6. tokenRequest.refreshToken = "CzuJktQK*********TAVTJorJa";
  7. AccountTokenModel tokenResponse = client.accountToken(tokenRequest);
  8. System.out.println(new Gson().toJson(tokenResponse));
  9. } catch (TeaException e) {
  10. System.out.println(e.getCode());
  11. System.out.println(e.getMessage());
  12. }
  13. }

通过账号获取访问令牌

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getAccessTokenByLinkInfo() throws Exception {
  2. try {
  3. // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
  4. GetAccessTokenByLinkInfoRequest getAccessTokenByLinkInfoRequest = new GetAccessTokenByLinkInfoRequest();
  5. getAccessTokenByLinkInfoRequest.identity = "132********25";
  6. getAccessTokenByLinkInfoRequest.type = "mobile";
  7. GetAccessTokenByLinkInfoModel tokenResponse = client.getAccessTokenByLinkInfo(getAccessTokenByLinkInfoRequest);
  8. System.out.println(new Gson().toJson(tokenResponse.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getCode());
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getData());
  13. }
  14. }

获取用户绑定信息

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getLinkInfoByUserId() throws Exception {
  2. try {
  3. // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
  4. GetLinkInfoByUserIDRequest linkInfoByUserIDRequest = new GetLinkInfoByUserIDRequest();
  5. linkInfoByUserIDRequest.userId = "6c23*********5211";
  6. GetLinkInfoByUserIdModel listResponse = client.getLinkInfoByUserId(linkInfoByUserIDRequest);
  7. System.out.println(new Gson().toJson(listResponse.body));
  8. } catch (TeaException e) {
  9. System.out.println(e.getCode());
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getData());
  12. }
  13. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "authenticationType":"mobile",
  5. "createdAt":1571905906341,
  6. "domainId":"daily1405",
  7. "identity":"13******225",
  8. "lastLoginTime":1571905906341,
  9. "status":"normal",
  10. "userId":"6c23c9******3f9f5211"
  11. }
  12. ]
  13. }

获取用户认证方式

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getLinkInfo() throws Exception {
  2. try {
  3. // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
  4. GetByLinkInfoRequest getByLinkInfoRequest = new GetByLinkInfoRequest();
  5. getByLinkInfoRequest.identity = "adsfqwrsfad";
  6. getByLinkInfoRequest.type = "ding";
  7. GetLinkInfoModel linkInfoResponse = client.getLinkInfo(getByLinkInfoRequest);
  8. System.out.println(new Gson().toJson(linkInfoResponse.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getCode());
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getData());
  13. }
  14. }
  • 返回结果
  1. {
  2. "authenticationType":"ding",
  3. "createdAt":1572427460313,
  4. "domainId":"daily1405",
  5. "identity":"adsfqwrsfad",
  6. "lastLoginTime":1572427460313,
  7. "status":"wait_link",
  8. "userId":"6c23c98*****7d8b3f9f5211"
  9. }

绑定用户认证方式

  • 以下代码用于绑定用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void userLinkInfo() throws Exception {
  2. try {
  3. // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
  4. AccountLinkRequest linkInfo = new AccountLinkRequest();
  5. linkInfo.type = "taobao";
  6. linkInfo.identity = "1234";
  7. linkInfo.userId = "1eb15*************eff97708cb";
  8. linkInfo.status ="wait_link";
  9. LinkModel tokenResponse = client.link(linkInfo);
  10. System.out.println(new Gson().toJson(tokenResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getCode());
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getData());
  15. }
  16. }
  • 返回结果
  1. {
  2. "avatar":"",
  3. "defaultDriveId":"",
  4. "existLink":[
  5. {
  6. "identity":"13*****225",
  7. "type":"mobile"
  8. }
  9. ],
  10. "expireTime":"",
  11. "expiresIn":300,
  12. "needLink":true,
  13. "nickName":"",
  14. "refreshToken":"",
  15. "role":"",
  16. "state":"",
  17. "tokenType":"Bearer",
  18. "userId":"",
  19. "userName":""
  20. }

取消绑定关系

  • 以下代码用于取消绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void cancelLink() throws Exception {
  2. try {
  3. CancelLinkRequest cancelLinkRequest = new CancelLinkRequest();
  4. cancelLinkRequest.temporaryToken = "eyJhbGciOiJSUzI1N*****pwc";
  5. CancelLinkModel tokenResponse = client.cancelLink(cancelLinkRequest);
  6. System.out.println(new Gson().toJson(tokenResponse.body));;
  7. } catch (TeaException e) {
  8. System.out.println(e.getCode());
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getData());
  11. }
  12. }

确定绑定关系

  • 以下代码用于确定绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  • 此接口只支持Access Token,不要使用AK,SK初始化客户端。
  1. public void confirmLink() throws Exception {
  2. try {
  3. ConfirmLinkRequest confirmLinkRequest = new ConfirmLinkRequest();
  4. confirmLinkRequest.temporaryToken = "eyJhbGciOiJSUzI1NiIsI(***qE";
  5. ConfirmLinkModel tokenResponse = client.confirmLink(confirmLinkRequest);
  6. System.out.println(new Gson().toJson(tokenResponse));
  7. } catch (TeaException e) {
  8. System.out.println(e.getCode());
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getData());
  11. }
  12. }

User 相关 API

创建User

  • 以下代码用于创建User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 创建User
  2. public static void createUser() throws Exception {
  3. try {
  4. CreateUserRequest createUserRequest = new CreateUserRequest();
  5. createUserRequest.userId = "test_user";
  6. createUserRequest.role = "user";
  7. createUserRequest.description = "123";
  8. CreateUserModel createUserResponse = client.createUser(createUserRequest);
  9. System.out.println(new Gson().toJson(createUserResponse.body));
  10. } catch (TeaException e) {
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getCode());
  13. System.out.println(new Gson().toJson(e.getData()));
  14. }
  15. }
  • 返回结果
  1. {
  2. "avatar":"",
  3. "createdAt":1572225460185,
  4. "defaultDriveId":"",
  5. "description":"",
  6. "domainId":"daily1405",
  7. "email":"",
  8. "nickName":"",
  9. "phone":"",
  10. "role":"user",
  11. "status":"enabled",
  12. "updatedAt":0,
  13. "userId":"test_user",
  14. "userName":"test_user"
  15. }

获取User

  • 以下代码用于获取User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. //获取User
  2. public static void getUser() throws Exception {
  3. try {
  4. GetUserRequest getUserRequest = new GetUserRequest();
  5. getUserRequest.userId = "test_user";
  6. GetUserModel getUserResponse = client.getUser(getUserRequest);
  7. System.out.println(new Gson().toJson(getUserResponse.body));
  8. } catch (TeaException e) {
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getCode());
  11. System.out.println(new Gson().toJson(e.getData()));
  12. }
  13. }
  • 返回结果
  1. {
  2. "avatar":"",
  3. "createdAt":1572226149810,
  4. "defaultDriveId":"",
  5. "description":"",
  6. "domainId":"daily1405",
  7. "email":"",
  8. "nickName":"",
  9. "phone":"",user
  10. "role":"user",
  11. "status":"enabled",
  12. "updatedAt":0,
  13. "userId":"test_user",
  14. "userName":"test_user"
  15. }

列举User

  • 以下代码用于列举User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. //列举User
  2. public static void listUsers() throws Exception {
  3. try {
  4. ListUserRequest listUserRequest = new ListUserRequest();
  5. listUserRequest.limit = 10;
  6. ListUsersModel listUserResponse = client.listUsers(listUserRequest);
  7. System.out.println(new Gson().toJson(listUserResponse.body));
  8. } catch (TeaException e) {
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getCode());
  11. System.out.println(new Gson().toJson(e.getData()));
  12. }
  13. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "avatar":"",
  5. "createdAt":1571903980532,
  6. "defaultDriveId":"",
  7. "description":"",
  8. "domainId":"daily1405",
  9. "email":"",
  10. "nickName":"xxxxxx",
  11. "phone":"",
  12. "role":"user",
  13. "status":"enabled",
  14. "updatedAt":1571903980532,
  15. "userId":"1eb15a*****************8cb",
  16. "userName":"xxxxxx"
  17. },
  18. {
  19. "avatar":"",
  20. "createdAt":1571915575499,
  21. "defaultDriveId":"",
  22. "description":"",
  23. "domainId":"daily1405",
  24. "email":"",
  25. "nickName":"xxxxxx",
  26. "phone":"",
  27. "role":"user",
  28. "status":"enabled",
  29. "updatedAt":1571915575499,
  30. "userId":"51901a4************dbf5",
  31. "userName":"xxxxxx"
  32. },
  33. {
  34. "avatar":"",
  35. "createdAt":1571903776751,
  36. "defaultDriveId":"",
  37. "description":"",
  38. "domainId":"daily1405",
  39. "email":"",
  40. "nickName":"xxxxxx",
  41. "phone":"",
  42. "role":"user",
  43. "status":"enabled",
  44. "updatedAt":1571903776751,
  45. "userId":"621a3c***************8ecd",
  46. "userName":"xxxxxx"
  47. },
  48. {
  49. "avatar":"",
  50. "createdAt":1571905906346,
  51. "defaultDriveId":"",
  52. "description":"",
  53. "domainId":"daily1405",
  54. "email":"",
  55. "nickName":"1329***25",
  56. "phone":"132****25",
  57. "role":"admin",
  58. "status":"enabled",
  59. "updatedAt":1571907859554,
  60. "userId":"6c23c98****************f5211",
  61. "userName":"132**********5"
  62. },
  63. {
  64. "avatar":"",
  65. "createdAt":1572226835585,
  66. "defaultDriveId":"",
  67. "description":"",
  68. "domainId":"daily1405",
  69. "email":"",
  70. "nickName":"",
  71. "phone":"",
  72. "role":"admin",
  73. "status":"enabled",
  74. "updatedAt":0,
  75. "userId":"xxxxxx",
  76. "userName":"xxxxxx"
  77. },
  78. {
  79. "avatar":"",
  80. "createdAt":1571887988846,
  81. "defaultDriveId":"",
  82. "description":"",
  83. "domainId":"daily1405",
  84. "email":"",
  85. "nickName":"superadmin",
  86. "phone":"",
  87. "role":"superadmin",
  88. "status":"enabled",
  89. "updatedAt":0,
  90. "userId":"superadmin",
  91. "userName":"superadmin"
  92. }
  93. ],
  94. "nextMarker":""
  95. }

更新User

  • 以下代码用于更新User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. //更新User
  2. public static void updateUser() throws Exception {
  3. try {
  4. UpdateUserRequest updateUserRequest = new UpdateUserRequest();
  5. updateUserRequest.description = "changed_user";
  6. updateUserRequest.userId = "test_user";
  7. UpdateUserModel updateUserResponse = client.updateUser(updateUserRequest);
  8. System.out.println(new Gson().toJson(updateUserResponse.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getCode());
  12. System.out.println(new Gson().toJson(e.getData()));
  13. }
  14. }
  • 返回结果
  1. {
  2. "avatar":"",
  3. "createdAt":1572226835585,
  4. "defaultDriveId":"",
  5. "description":"test_user",
  6. "domainId":"daily1405",
  7. "email":"",
  8. "nickName":"",
  9. "phone":"",
  10. "role":"user",
  11. "status":"enabled",
  12. "updatedAt":1572226880276,
  13. "userId":"test_user",
  14. "userName":"test_user"
  15. }

搜索User

  • 以下代码用于搜索User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // search user
  2. public static void searchUser() throws Exception {
  3. try {
  4. SearchUserRequest searchUserRequest = new SearchUserRequest();
  5. SearchUserModel listUserResponse1 = client.searchUser(searchUserRequest);
  6. System.out.println(new Gson().toJson(listUserResponse1.body));
  7. } catch (TeaException e) {
  8. System.out.println(e.getMessage());
  9. System.out.println(e.getCode());
  10. System.out.println(new Gson().toJson(e.getData()));
  11. }
  12. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "avatar":"",
  5. "createdAt":1571915575499,
  6. "defaultDriveId":"",
  7. "description":"",
  8. "domainId":"daily1405",
  9. "email":"",
  10. "nickName":"刘***",
  11. "phone":"",
  12. "role":"user",
  13. "status":"enabled",
  14. "updatedAt":1571915575499,
  15. "userId":"5190******************2edbf5",
  16. "userName":"刘***"
  17. }
  18. ],
  19. "nextMarker":""
  20. }

删除User

  • 以下代码用于删除User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除User
  2. public static void deleteUser() throws Exception {
  3. try {
  4. DeleteUserRequest deleteUserRequest = new DeleteUserRequest();
  5. deleteUserRequest.userId = "test_user";
  6. DeleteUserModel deleteUserResponse = client.deleteUser(deleteUserRequest);
  7. //此接口没有返回body
  8. } catch (TeaException e) {
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getCode());
  11. System.out.println(new Gson().toJson(e.getData()));
  12. }
  13. }

StandardMode Drive 相关API

创建drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 创建 Drive
  2. public static void createDrive() throws Exception {
  3. try {
  4. CreateDriveRequest createDriveRequest = new CreateDriveRequest();
  5. createDriveRequest.totalSize = 100000L;
  6. createDriveRequest.driveName = "test_drive";
  7. createDriveRequest.driveType = "normal";
  8. createDriveRequest.owner = "superadmin";
  9. CreateDriveModel createDriveResponse = client.createDrive(createDriveRequest);
  10. // 打印结果
  11. System.out.println(new Gson().toJson(createDriveResponse.body));
  12. } catch (TeaException e) {
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getCode());
  15. System.out.println(new Gson().toJson(e.getData()));
  16. }
  17. }
  • 返回结果
  1. {
  2. "domainId":"daily1404",
  3. "driveId":"603"
  4. }

列举drive

  • 以下代码用于列举drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 列举 Drive
  2. public static void listDrive() throws Exception {
  3. try {
  4. ListDriveRequest listDriveRequest = new ListDriveRequest();
  5. listDriveRequest.limit = 1;
  6. listDriveRequest.owner = "";
  7. ListDrivesModel listDriveResponse = client.listDrives(listDriveRequest);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(listDriveResponse.body));
  10. } catch (TeaException e) {
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getCode());
  13. System.out.println(new Gson().toJson(e.getData()));
  14. }
  15. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "creator":"",
  5. "description":"",
  6. "domainId":"daily1404",
  7. "driveId":"603",
  8. "driveName":"test_drive",
  9. "driveType":"normal",
  10. "owner":"ldh",
  11. "relativePath":"",
  12. "status":"enabled",
  13. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  14. "totalSize":100000,
  15. "usedSize":0
  16. }
  17. ],
  18. "nextMarker":""
  19. }

查询drive

  • 以下代码用于查询drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询 Drive
  2. public static void getDrive() throws Exception {
  3. try {
  4. GetDriveRequest getDriveRequest = new GetDriveRequest();
  5. getDriveRequest.driveId = "1";
  6. GetDriveModel getDriveResponse = client.getDrive(getDriveRequest);
  7. //打印结果
  8. System.out.println(new Gson().toJson(getDriveResponse.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getCode());
  12. System.out.println(new Gson().toJson(e.getData()));
  13. }
  14. }
  • 返回结果
  1. {
  2. "creator":"",
  3. "description":"",
  4. "domainId":"daily1404",
  5. "driveId":"603",
  6. "driveName":"test_drive",
  7. "driveType":"normal",
  8. "owner":"ldh",
  9. "relativePath":"",
  10. "status":"enabled",
  11. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  12. "totalSize":100000,
  13. "usedSize":0
  14. }

更新drive

  • 以下代码用于更新drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 更新 Drive
  2. public static void updateDrive() throws Exception {
  3. try {
  4. UpdateDriveRequest updateDriveRequest = new UpdateDriveRequest();
  5. updateDriveRequest.driveId = "401";
  6. updateDriveRequest.description = "changed_drive";
  7. updateDriveRequest.totalSize = 1000000L;
  8. UpdateDriveModel updateDriveResponse = client.updateDrive(updateDriveRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(updateDriveResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. }
  • 返回结果
  1. {
  2. "creator":"",
  3. "description":"changed_drive",
  4. "domainId":"daily1404",
  5. "driveId":"603",
  6. "driveName":"test_drive",
  7. "driveType":"normal",
  8. "owner":"ldh",
  9. "relativePath":"",
  10. "status":"enabled",
  11. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  12. "totalSize":1000000,
  13. "usedSize":0
  14. }

删除drive

  • 以下代码用于删除drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 Drive
  2. public static void deleteDrive() throws Exception {
  3. try {
  4. DeleteDriveRequest deleteDriveRequest = new DeleteDriveRequest();
  5. deleteDriveRequest.driveId = "";
  6. DeleteDriveModel deleteDriveResponse = client.deleteDrive(deleteDriveRequest);
  7. // 此接口不返回body
  8. } catch (TeaException e) {
  9. System.out.println(e.getMessage());
  10. System.out.println(e.getCode());
  11. System.out.println(new Gson().toJson(e.getData()));
  12. }
  13. }

StandardMode File 相关 API

创建File

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 创建文件
  2. public static void createFile() throws Exception {
  3. try {
  4. CreateFileRequest CreateFileRequest = new CreateFileRequest();
  5. CreateFileRequest.type = "folder";
  6. CreateFileRequest.name = "test_folder";
  7. CreateFileRequest.driveId = "1";
  8. CreateFileRequest.parentFileId = "root";
  9. CreateFileModel CreateFileResponse = client.createFile(CreateFileRequest);
  10. // 打印结果
  11. System.out.println(new Gson().toJson(CreateFileResponse.body));
  12. } catch (TeaException e) {
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getCode());
  15. System.out.println(new Gson().toJson(e.getData()));
  16. }
  17. catch (Exception e){
  18. System.out.println(e.getMessage());
  19. }
  20. }
  • 返回结果
  1. {
  2. "domainId":"daily1404",
  3. "driveId":"603",
  4. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  5. "parentFileId":"root",
  6. "partInfoList":[
  7. {
  8. "partNumber":1,
  9. "uploadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
  10. }
  11. ],
  12. "rapidUpload":false,
  13. "type":"file",
  14. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
  15. }

列举File

  • 以下代码用于列举File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 列举文件
  2. public static void listFile() throws Exception {
  3. try {
  4. ListFileRequest ListFileRequest = new ListFileRequest();
  5. ListFileRequest.driveId = "1";
  6. ListFileRequest.parentFileId = "root";
  7. ListFileRequest.limit = 1L;
  8. ListFileModel ListFileResponse = client.listFile(ListFileRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ListFileResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. catch (Exception e){
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  5. "contentHashName":"sha1",
  6. "contentType":"",
  7. "createdAt":"2019-10-28T08:40:54.398Z",
  8. "domainId":"daily1404",
  9. "downloadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****.txt",
  10. "driveId":"603",
  11. "fileExtension":"txt",
  12. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  13. "hidden":false,
  14. "name":"a.txt",
  15. "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
  16. "size":6,
  17. "starred":false,
  18. "status":"available",
  19. "type":"file",
  20. "updatedAt":"2019-10-28T08:40:55.398Z",
  21. "url":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
  22. }
  23. ],
  24. "nextMarker":""
  25. }

查询File

  • 以下代码用于查询File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询File
  2. public static void getFile() throws Exception {
  3. try {
  4. GetFileRequest GetFileRequest = new GetFileRequest();
  5. GetFileRequest.driveId = "1";
  6. GetFileRequest.fileId = "5f3b54672d4cd1b97fcb4e658385061b98d5863f";
  7. GetFileModel GetFileResponse = client.getFile(GetFileRequest);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(GetFileResponse.body));
  10. } catch (TeaException e) {
  11. System.out.println(e.getMessage());
  12. System.out.println(e.getCode());
  13. System.out.println(new Gson().toJson(e.getData()));
  14. }
  15. catch (Exception e){
  16. System.out.println(e.getMessage());
  17. }
  18. }
  • 返回结果
  1. {
  2. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  3. "contentHashName":"sha1",
  4. "contentType":"text/plain",
  5. "crc64Hash":"318318745347147982",
  6. "createdAt":"2019-10-28T08:40:54.398Z",
  7. "domainId":"daily1404",
  8. "downloadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943.txt",
  9. "driveId":"603",
  10. "fileExtension":"txt",
  11. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  12. "hidden":false,
  13. "name":"a.txt",
  14. "parentFileId":"root",
  15. "size":6,
  16. "starred":false,
  17. "status":"available",
  18. "type":"file",
  19. "updatedAt":"2019-10-28T08:40:55.398Z",
  20. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
  21. "url":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
  22. }

移动File

  • 以下代码用于移动File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 移动 File
  2. public static void moveFile() throws Exception {
  3. try {
  4. MoveFileRequest MoveFileRequest = new MoveFileRequest();
  5. MoveFileRequest.driveId = "1";
  6. MoveFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
  7. MoveFileRequest.toParentFileId = "5f3b54672d4cd1b97fcb4e658385061b98d5863f";
  8. MoveFileModel MoveFileResponse = client.moveFile(MoveFileRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(MoveFileResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. catch (Exception e){
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "domainId":"daily1404",
  3. "driveId":"603",
  4. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f"
  5. }

复制File

  • 以下代码用于复制File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 复制 File
  2. public static void copyFile() throws Exception {
  3. try {
  4. CopyFileRequest CopyFileRequest = new CopyFileRequest();
  5. CopyFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
  6. CopyFileRequest.driveId = "1";
  7. CopyFileRequest.toParentFileId = "root";
  8. CopyFileModel CopyFileResponse = client.copyFile(CopyFileRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(CopyFileResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. catch (Exception e){
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "domainId":"daily1404",
  3. "driveId":"603",
  4. "fileId":"5db6a997ce31eb635f2e4f4c9163ec3bd10af459"
  5. }

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File上传地址
  2. public static void getFileUploadUrl() throws Exception {
  3. try {
  4. UploadPartInfo uploadPartInfo = new UploadPartInfo();
  5. uploadPartInfo.partNumber = 1L;
  6. GetUploadUrlRequest GetUploadUrlRequest = new GetUploadUrlRequest();
  7. GetUploadUrlRequest.driveId = "";
  8. GetUploadUrlRequest.fileId = "";
  9. GetUploadUrlRequest.uploadId = "";
  10. GetUploadUrlRequest.partInfoList = new ArrayList<UploadPartInfo>();
  11. GetUploadUrlRequest.partInfoList.add(uploadPartInfo);
  12. GetUploadUrlModel GetUploadUrlResponse = client.getUploadUrl(GetUploadUrlRequest);
  13. // 打印结果
  14. System.out.println(new Gson().toJson(GetUploadUrlResponse.body));
  15. } catch (TeaException e) {
  16. System.out.println(e.getMessage());
  17. System.out.println(e.getCode());
  18. System.out.println(new Gson().toJson(e.getData()));
  19. }
  20. catch (Exception e){
  21. System.out.println(e.getMessage());
  22. }
  23. }
  • 返回结果
  1. {
  2. "createAt":"2019-10-28T08:40:54.519Z",
  3. "domainId":"daily1404",
  4. "driveId":"603",
  5. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  6. "partInfoList":[
  7. {
  8. "partNumber":1,
  9. "uploadUrl":"url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
  10. }
  11. ],
  12. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
  13. }

获取File下载地址

  • 以下代码用于获取File下载地,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File下载地址
  2. public static void getFileDownloadUrl() throws Exception {
  3. try {
  4. GetDownloadUrlRequest GetDownloadUrlRequest = new GetDownloadUrlRequest();
  5. GetDownloadUrlRequest.driveId = "1";
  6. GetDownloadUrlRequest.fileId = "5f3f2abeb28ec23886d844e397e75fa30f5339af";
  7. GetDownloadUrlRequest.expireSec = 3600L;
  8. GetDownloadUrlModel GetDownloadUrlResponse = client.getDownloadUrl(GetDownloadUrlRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(GetDownloadUrlResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. catch (Exception e){
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "expiration":"2019-10-28T09:40:55.716Z",
  3. "method":"GET",
  4. "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
  5. }

Complete File

  • 以下代码用于Complete File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 合并File分片
  2. public static void completeFile() throws Exception {
  3. try {
  4. CompleteFileRequest CompleteFileRequest = new CompleteFileRequest();
  5. UploadPartInfo uploadPartInfo = new UploadPartInfo();
  6. CompleteFileRequest.driveId = "";
  7. CompleteFileRequest.fileId = "";
  8. CompleteFileRequest.uploadId = "";
  9. CompleteFileRequest.partInfoList = new ArrayList<UploadPartInfo>(Arrays.asList(uploadPartInfo));
  10. CompleteFileModel CompleteFileResponse = client.completeFile(CompleteFileRequest);
  11. // 打印结果
  12. System.out.println(new Gson().toJson(CompleteFileResponse.body));
  13. } catch (TeaException e) {
  14. System.out.println(e.getMessage());
  15. System.out.println(e.getCode());
  16. System.out.println(new Gson().toJson(e.getData()));
  17. }
  18. catch (Exception e){
  19. System.out.println(e.getMessage());
  20. }
  21. }
  • 返回结果
  1. {
  2. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  3. "contentHashName":"sha1",
  4. "contentType":"text/plain",
  5. "crc64Hash":"318318745347147982",
  6. "createdAt":"2019-10-28T08:40:54.398Z",
  7. "domainId":"daily1404",
  8. "driveId":"603",
  9. "fileExtension":"txt",
  10. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  11. "hidden":false,
  12. "name":"a.txt",
  13. "parentFileId":"root",
  14. "size":6,
  15. "starred":false,
  16. "status":"available",
  17. "type":"file",
  18. "updatedAt":"2019-10-28T08:40:55.398Z",
  19. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
  20. "crc":""
  21. }

更新File

  • 以下代码用于更新File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 更新 file
  2. public static void updateFile() throws Exception {
  3. try {
  4. UpdateFileMetaRequest UpdateFileMetaRequest = new UpdateFileMetaRequest();
  5. UpdateFileMetaRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
  6. UpdateFileMetaRequest.description = "changed_file";
  7. UpdateFileMetaRequest.driveId = "1";
  8. UpdateFileModel UpdateFileMetaResponse = client.updateFile(UpdateFileMetaRequest);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(UpdateFileMetaResponse.body));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(new Gson().toJson(e.getData()));
  15. }
  16. catch (Exception e){
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  3. "contentHashName":"sha1",
  4. "contentType":"text/plain",
  5. "crc64Hash":"318318745347147982",
  6. "createdAt":"2019-10-28T08:40:54.398Z",
  7. "description":"changed_file",
  8. "domainId":"daily1404",
  9. "downloadUrl":"https://******.oss-cn-hangzhou.aliyuncs.com/5****a.txt",
  10. "driveId":"603",
  11. "fileExtension":"txt",
  12. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  13. "hidden":false,
  14. "name":"a.txt",
  15. "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
  16. "size":6,
  17. "starred":false,
  18. "status":"available",
  19. "type":"file",
  20. "updatedAt":"2019-10-28T08:40:55.398Z",
  21. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
  22. "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
  23. }

搜索File

  • 以下代码用于搜索File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 搜索 file
  2. public static void searchFile() throws Exception {
  3. try {
  4. SearchFileRequest SearchFileRequest = new SearchFileRequest();
  5. SearchFileRequest.driveId = "1";
  6. SearchFileRequest.limit = 3;
  7. SearchFileRequest.orderBy = "type DESC";
  8. SearchFileRequest.query = "file_extension in [\"txt\"]";
  9. SearchFileModel SearchFileResponse = client.searchFile(SearchFileRequest);
  10. // 打印结果
  11. System.out.println(new Gson().toJson(SearchFileResponse.body));
  12. } catch (TeaException e) {
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getCode());
  15. System.out.println(new Gson().toJson(e.getData()));
  16. }
  17. catch (Exception e){
  18. System.out.println(e.getMessage());
  19. }
  20. }
  • 返回结果
  1. {
  2. "items":[
  3. {
  4. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  5. "contentHashName":"sha1",
  6. "contentType":"text/plain",
  7. "crc64Hash":"318318745347147982",
  8. "createdAt":"2019-10-28T08:40:54.398Z",
  9. "description":"changed_file",
  10. "domainId":"daily1404",
  11. "downloadUrl":"https://************.oss-cn-hangzhou.aliyuncs.com/***a.txt",
  12. "driveId":"603",
  13. "fileExtension":"txt",
  14. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  15. "hidden":false,
  16. "name":"a.txt",
  17. "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
  18. "size":6,
  19. "starred":false,
  20. "status":"available",
  21. "type":"file",
  22. "updatedAt":"2019-10-28T08:40:55.398Z",
  23. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
  24. "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****ZM%3D"
  25. }
  26. ],
  27. "nextMarker":""
  28. }

删除File

  • 以下代码用于删除file,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 file
  2. public static void deleteFile() throws Exception {
  3. try {
  4. DeleteFileRequest DeleteFileRequest = new DeleteFileRequest();
  5. DeleteFileRequest.driveId = "1";
  6. DeleteFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
  7. DeleteFileModel deleteFileResponse = client.deleteFile(DeleteFileRequest);
  8. System.out.println(new Gson().toJson(deleteFileResponse.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getCode());
  12. System.out.println(new Gson().toJson(e.getData()));
  13. }
  14. catch (Exception e){
  15. System.out.println(e.getMessage());
  16. }
  17. }

批量操作

  • 以下代码用于批量操作,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public static void batch() throws Exception {
  2. try {
  3. BatchRequest BatchRequest = new BatchRequest();
  4. BatchRequest.resource = "file";
  5. BatchSubRequest batchSubRequest = new BatchSubRequest();
  6. Map<String, Object> bodyMap = new HashMap<String, Object>();
  7. bodyMap.put("drive_id", "1");
  8. bodyMap.put("file_id", "5f3b578fdea43919eb3d4a3bac88b614a1754f52");
  9. batchSubRequest.body = bodyMap;
  10. batchSubRequest.url = "/file/get";
  11. batchSubRequest.method = "POST";
  12. batchSubRequest.id = "uuid";
  13. BatchRequest.requests = new ArrayList<BatchSubRequest>(Arrays.asList(batchSubRequest));
  14. BatchOperationModel response = client.batchOperation(BatchRequest);
  15. System.out.println(new Gson().toJson(response.body));
  16. } catch (TeaException e) {
  17. System.out.println(e.getMessage());
  18. }
  19. }
  • 返回结果
  1. {
  2. "responses":[
  3. {
  4. "status":200,
  5. "body":{
  6. "file_extension":"txt",
  7. "updated_at":"2020-03-30T08:15:58.267Z",
  8. "content_hash":"FAA12FD40AAC1F492082C90C2CD6C03B9ABDB826",
  9. "domain_id":"hz22",
  10. "size":36,
  11. "category":"doc",
  12. "content_hash_name":"sha1",
  13. "download_url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
  14. "crc64_hash":"13138712399852734283",
  15. "drive_id":"1",
  16. "hidden":false,
  17. "type":"file",
  18. "parent_file_id":"root",
  19. "status":"available",
  20. "description":"changed_file",
  21. "encrypt_mode":"none",
  22. "file_id":"5e81aabdae****d9836b36",
  23. "content_type":"application/oct-stream",
  24. "name":"testDJw8oWE6ef9464f66e5034f69aafd57cb2879170b.txt",
  25. "url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
  26. "created_at":"2020-03-30T08:15:57.361Z",
  27. "upload_id":"DFD1DA39317F45EC8534FEA57AE420D1",
  28. "starred":false
  29. },
  30. "id":"624830fa2c2347be8c0d8afc76faa04d"
  31. }
  32. ]
  33. }

获取file已经上传的分片

  • 以下代码用于获取file已经上传的分片,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void listUploadedPart() throws Exception {
  2. try {
  3. ListUploadedPartRequest ListUploadedPartRequest = new ListUploadedPartRequest();
  4. ListUploadedPartRequest.driveId = "1";
  5. ListUploadedPartRequest.fileId = "****";
  6. ListUploadedPartRequest.uploadId = "****";
  7. ListUploadedPartsModel response = client.listUploadedParts(ListUploadedPartRequest);
  8. System.out.println(new Gson().toJson(response.body));
  9. } catch (TeaException e) {
  10. System.out.println(e.getMessage());
  11. }
  12. }
  • 返回结果
  1. {
  2. "drive_id" : "1",
  3. "file_id" : "5d5b846942cf94fa72324c14a4bda34e81da635d",
  4. "limit" : 1,
  5. "part_number_marker" : 1,
  6. "upload_id" : "00668396C0814D818D90F0A92B04B355"
  7. }

获取异步信息

  • 以下代码用于获取异步信息,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getAsyncTask() throws Exception {
  2. try {
  3. GetAsyncTaskRequest getAsyncTaskRequest = new GetAsyncTaskRequest();
  4. getAsyncTaskRequest.asyncTaskId = "***";
  5. GetAsyncTaskInfoModel response = client.getAsyncTaskInfo(getAsyncTaskRequest);
  6. System.out.println(new Gson().toJson(response.headers));
  7. } catch (TeaException e) {
  8. System.out.println(e.getMessage());
  9. }
  10. }
  • 返回结果
  1. {
  2. "async_task_id" : "000e89fb-cf8f-11e9-8ab4-b6e980803a3b",
  3. "message" : "task is running",
  4. "state" : "success"
  5. }

创建RAM子用户(获取AK,SK)

  1. 注册阿里云账号,详见阿里云账号注册流程
  2. 开启访问控制服务,详见RAM访问控制,并根据提示操作。
  3. 创建RAM子用户,并获取AK,SK。详见服务端调用接口接入。SK要注意保密不要泄露。

创建APP(获取ClientID, ClientSecret)

  1. 首先,您需要开通相册与网盘(PDS)服务。如果没有开通,请到产品详情页面开通 。
  2. 您需要在PDS控制台创建一个实例 。详见创建StandardMode实例和创建HostingMode实例
  3. 创建APP,选择类型为”Web服务应用”。确定APP的访问Scope: 支持的Scope列表, 这个Scope要在用户授权页面展示。创建完成,可以得到APP ID(ClientID) 和 Secret(ClientSecret)。这个是授权认证的凭证,Secret要注意保密不要泄露。

StandardMode 上传文件 Demo

  1. import com.aliyun.pds.client.Client;
  2. import com.aliyun.pds.client.models.*;
  3. import com.aliyun.tea.TeaException;
  4. import com.google.gson.Gson;
  5. import okhttp3.*;
  6. import java.util.*;
  7. public class Demo {
  8. private static Client client;
  9. private static RuntimeOptions runtime;
  10. public static void main(String[] args) throws Exception{
  11. createClient();
  12. createFile();
  13. }
  14. public static void createClient() throws Exception {
  15. Config config = new Config();
  16. config.domainId= "your domain id";
  17. config.protocol = "https";
  18. config.accessKeyId = "your accessKeyId";
  19. config.accessKeySecret = "your accessKeySecret";
  20. client = new Client(config);
  21. runtime = new RuntimeOptions();
  22. }
  23. public static void createFullFile() throws Exception{
  24. try{
  25. System.out.println("-------------create file----------------");
  26. CreateFileRequest CreateFileRequest = new CreateFileRequest();
  27. CreateFileRequest.type = "file";
  28. CreateFileRequest.name = "a.txt";
  29. CreateFileRequest.driveId = "1";
  30. CreateFileRequest.parentFileId = "root";
  31. CreateFileRequest.contentType = "text/plain";
  32. CreateFileModel CreateFileResponse = client.createFile(CreateFileRequest);
  33. System.out.println(new Gson().toJson(CreateFileResponse.body));
  34. String uploadId = CreateFileResponse.body.uploadId;
  35. String fileId = CreateFileResponse.body.fileId;
  36. String uploadUrl = CreateFileResponse.body.partInfoList.get(0).uploadUrl;
  37. // upload file
  38. System.out.println("-------------upload file----------------");
  39. Request.Builder requestBuilder = new Request.Builder();
  40. RequestBody body = RequestBody.create(MediaType.parse(""), "123456");
  41. requestBuilder.url(uploadUrl);
  42. requestBuilder.put(body);
  43. Request request = requestBuilder.build();
  44. OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
  45. Response response = okHttpClient.newCall(request).execute();
  46. String etag = response.headers().get("ETag");
  47. System.out.println();
  48. // complete file
  49. System.out.println("-------------complete file----------------");
  50. UploadPartInfo uploadPartInfo1 = new UploadPartInfo();
  51. uploadPartInfo1.etag = etag;
  52. uploadPartInfo1.partNumber = 1L;
  53. CompleteFileRequest CompleteFileRequest = new CompleteFileRequest();
  54. CompleteFileRequest.driveId = "1";
  55. CompleteFileRequest.fileId = fileId;
  56. CompleteFileRequest.uploadId = uploadId;
  57. CompleteFileRequest.partInfoList = new ArrayList<UploadPartInfo>(Arrays.asList(uploadPartInfo1));
  58. CompleteFileModel CompleteFileResponse = client.completeFile(CompleteFileRequest);
  59. System.out.println(new Gson().toJson(CompleteFileResponse.body));
  60. }catch (TeaException e) {
  61. System.out.println(e.getMessage());
  62. System.out.println(e.getCode());
  63. System.out.println(new Gson().toJson(e.getData()));
  64. }
  65. }
  66. }