推特 阿里云技术文档正文

Java SDK 简介_SDK参考_内容协作平台

admin 阿里云技术文档 2020-02-11 211 0
阿里云服务器优惠

Java SDK 简介

下载地址

https://github.com/aliyun/aliyun-ccp

安装步骤

安装 Java 开发环境

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

安装 CCP Java SDK

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

CCPPath

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>ccp-baseclient</artifactId>
  4. <version>Use the version shown in the maven badge</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>ccp-client</artifactId>
  9. <version>Use the version shown in the maven badge</version>
  10. </dependency>

OSSPath

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>ccp-baseclient</artifactId>
  4. <version>Use the version shown in the maven badge</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>ccp-oss-client</artifactId>
  9. <version>Use the version shown in the maven badge</version>
  10. </dependency>

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

初始化Client

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

AK & SK 初始化客户端

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

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

AccessToken & RefreshToken 初始化客户端

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

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

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

构造请求

Account 相关 API

获取图片验证码

  • 以下代码用于获取图片验证码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取图片验证码
  2. public static void getCaptcha() throws Exception {
  3. try {
  4. GetCaptchaRequest getCaptchaRequest = new GetCaptchaRequest();
  5. getCaptchaRequest.appId = appId;
  6. Captcha captcha = client.getCaptcha(getCaptchaRequest, runtime);
  7. // 打印返回结果
  8. System.out.println(new Gson().toJson(captcha));
  9. } catch (TeaException e) {
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getCode());
  12. System.out.println(e.getData());
  13. }
  14. }
  • 返回结果
  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 = appId;
  5. sendSmsCodeRequest.phoneNumber = "132*****25";
  6. sendSmsCodeRequest.type = "login";
  7. MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime);
  8. System.out.println(new Gson().toJson(response));
  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. "requestId":"07A5175C-E124-42EC-9304-2294DC2CEFAC",
  3. "smsCodeId":"b40bba70b37d74**********************6j3IW7HP"
  4. }

验证手机号是否注册

  • 以下代码用于验证手机号是否存在,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void checkMobileIsExist() throws Exception {
  2. try {
  3. MobileCheckExistRequest checkExistRequest = new MobileCheckExistRequest();
  4. checkExistRequest.appId = appId;
  5. checkExistRequest.phoneNumber = "13****25";
  6. MobileCheckExistResponse checkExistResponse = client.checkExist(checkExistRequest, runtime);
  7. System.out.println(new Gson().toJson(checkExistResponse));
  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. "requestId":"4DB78761-20AE-4724-8960-ADD225F35A0F",
  3. "isExist":true,
  4. "phoneNumber":"13****25"
  5. }

手机号注册

  • 以下代码用于手机号注册,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void register() throws Exception {
  2. try {
  3. // get register mns Code
  4. MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest();
  5. sendSmsCodeRequest.appId = appId;
  6. sendSmsCodeRequest.phoneNumber = "13****25";
  7. sendSmsCodeRequest.type = "change_password";
  8. MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime);
  9. String changePasswordCode = response.smsCodeId;
  10. // mobile register
  11. MobileRegisterRequest registerRequest = new MobileRegisterRequest();
  12. registerRequest.appId = appId;
  13. registerRequest.phoneNumber = "132*****225";
  14. registerRequest.smsCode = "123456";
  15. registerRequest.smsCodeId = changePasswordCode;
  16. AccountAccessTokenResponse tokenResponse = client.register(registerRequest, runtime);
  17. System.out.println(new Gson().toJson(tokenResponse));
  18. } catch (TeaException e) {
  19. System.out.println(e.getCode());
  20. System.out.println(e.getMessage());
  21. System.out.println(e.getData());
  22. }
  23. }
  • 返回结果
  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. // get login mns Code
  4. MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest();
  5. sendSmsCodeRequest.appId = appId;
  6. sendSmsCodeRequest.phoneNumber = "13****25";
  7. sendSmsCodeRequest.type = "change_password";
  8. MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime);
  9. String changePasswordCode = response.smsCodeId;
  10. // mns login
  11. MobileLoginRequest mobileLoginRequest = new MobileLoginRequest();
  12. mobileLoginRequest.appId = appId;
  13. mobileLoginRequest.phoneNumber = "13297039225";
  14. mobileLoginRequest.smsCode = "123456";
  15. mobileLoginRequest.smsCodeId = changePasswordCode;
  16. AccountAccessTokenResponse tokenResponse = client.login(mobileLoginRequest, runtime);
  17. System.out.println(new Gson().toJson(tokenResponse));
  18. } catch (TeaException e) {
  19. System.out.println(e.getCode());
  20. System.out.println(e.getMessage());
  21. System.out.println(e.getData());
  22. }
  23. }

设置登录密码

  • 以下代码用于设置登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void setPassWord() throws Exception {
  2. try {
  3. DefaultSetPasswordRequest setPasswordRequest = new DefaultSetPasswordRequest();
  4. setPasswordRequest.appId = appId;
  5. setPasswordRequest.newPassword = "1234567";
  6. setPasswordRequest.state = "aKsdfGoeasd***ksFasds";
  7. client.setPassword(setPasswordRequest, runtime);
  8. // 此结果不返回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. public static void passwordLogin() throws Exception {
  3. try {
  4. MobileLoginRequest mobileLoginRequest = new MobileLoginRequest();
  5. mobileLoginRequest.appId = appId;
  6. mobileLoginRequest.phoneNumber = "13*****225";
  7. mobileLoginRequest.password = "*********";
  8. AccountAccessTokenResponse tokenResponse = client.login(mobileLoginRequest, runtime);
  9. // 打印返回结果
  10. System.out.println(new Gson().tojson(tokenResponse));
  11. } catch (TeaException e) {
  12. System.out.println(e.getMessage());
  13. System.out.println(e.getCode());
  14. System.out.println(e.getData());
  15. }
  16. }

修改登录密码

  • 以下代码用于修改登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void changePassWord() throws Exception {
  2. try {
  3. // get change passowrd mns Code
  4. MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest();
  5. sendSmsCodeRequest.appId = appId;
  6. sendSmsCodeRequest.phoneNumber = "13****25";
  7. sendSmsCodeRequest.type = "change_password";
  8. MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime);
  9. String changePasswordCode = response.smsCodeId;
  10. // chang password
  11. DefaultChangePasswordRequest changePasswordRequest = new DefaultChangePasswordRequest();
  12. changePasswordRequest.appId = appId;
  13. changePasswordRequest.phoneNumber = "13****25";
  14. changePasswordRequest.newPassword = "1234567";
  15. changePasswordRequest.smsCode = "13124353";
  16. changePasswordRequest.smsCodeId = changePasswordCode;
  17. client.changePassword(changePasswordRequest, runtime);
  18. //此接口不返回body
  19. } catch (TeaException e) {
  20. System.out.println(e.getCode());
  21. System.out.println(e.getMessage());
  22. System.out.println(e.getData());
  23. }
  24. }

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

  • 以下代码用于刷新token,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void getTokenByRefreshToken() throws Exception {
  2. try {
  3. TokenRequest tokenRequest = new TokenRequest();
  4. tokenRequest.appId = appId;
  5. tokenRequest.grantType = "refresh_token";
  6. tokenRequest.refreshToken = "Dln*******Tcpz";
  7. AccountAccessTokenResponse tokenResponse = client.token(tokenRequest, runtime);
  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 = "13297039225";
  6. getAccessTokenByLinkInfoRequest.type = "mobile";
  7. AccountAccessTokenResponse tokenResponse = client.getAccessTokenByLinkInfo(getAccessTokenByLinkInfoRequest, runtime);
  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. 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 = "6c23c********5211";
  6. LinkInfoListResponse listResponse = client.getLinkInfoByUserId(linkInfoByUserIDRequest, runtime);
  7. System.out.println(new Gson().toJson(listResponse));
  8. } catch (TeaException e) {
  9. System.out.println(e.getCode());
  10. System.out.println(e.getMessage());
  11. System.out.println(e.getData());
  12. }
  • 返回结果
  1. {
  2. "requestId":"F19338F6-55B6-45D4-9D29-3B950125B175",
  3. "items":[
  4. {
  5. "authenticationType":"mobile",
  6. "createdAt":1571905906341,
  7. "domainId":"daily1405",
  8. "identity":"13******225",
  9. "lastLoginTime":1571905906341,
  10. "status":"normal",
  11. "userId":"6c23c9******3f9f5211"
  12. }
  13. ]
  14. }

获取用户认证方式

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  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. LinkInfoResponse linkInfoResponse = client.getLinkInfo(getByLinkInfoRequest, runtime);
  8. System.out.println(new Gson().toJson(linkInfoResponse));
  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. "requestId":"61AC56AD-E048-4292-8898-053287220B98",
  3. "authenticationType":"ding",
  4. "createdAt":1572427460313,
  5. "domainId":"daily1405",
  6. "identity":"adsfqwrsfad",
  7. "lastLoginTime":1572427460313,
  8. "status":"wait_link",
  9. "userId":"6c23c98*****7d8b3f9f5211"
  10. }

绑定用户认证方式

  • 以下代码用于绑定用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  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 = "1eb*********08cb";
  8. linkInfo.status ="wait_link";
  9. AccountAccessTokenResponse tokenResponse = client.link(linkInfo, runtime);
  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. {
  2. "requestId":"2DB728B5-6468-4E41-9BDE-677433FFCE16",
  3. "accessToken":"eyJ*****igpwc",
  4. "avatar":"",
  5. "defaultDriveId":"",
  6. "existLink":[
  7. {
  8. "identity":"13*****225",
  9. "type":"mobile"
  10. }
  11. ],
  12. "expireTime":"",
  13. "expiresIn":300,
  14. "needLink":true,
  15. "nickName":"",
  16. "refreshToken":"",
  17. "role":"",
  18. "state":"",
  19. "tokenType":"Bearer",
  20. "userId":"",
  21. "userName":""
  22. }

取消绑定关系

  • 以下代码用于取消绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public void cancelLink() throws Exception {
  2. try {
  3. CancelLinkRequest cancelLinkRequest = new CancelLinkRequest();
  4. cancelLinkRequest.temporaryToken = "einR5cCI6IkpX****asfsdfdf";
  5. AccountAccessTokenResponse tokenResponse = client.cancelLink(cancelLinkRequest, runtime);
  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. }

确定绑定关系

  • 以下代码用于确定绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  • 此接口只支持Access Token,不要使用AK,SK初始化客户端。
  1. public void confirmlLink() throws Exception {
  2. try {
  3. CancelLinkRequest cancelLinkRequest = new CancelLinkRequest();
  4. cancelLinkRequest.temporaryToken = "einR5cCI6IkpX****asfsdfdf";
  5. AccountAccessTokenResponse tokenResponse = client.cancelLink(cancelLinkRequest, runtime);
  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.userName = "test_user";
  8. CreateUserResponse createUserResponse = client.createUser(createUserRequest, runtime);
  9. // 打印返回结果
  10. System.out.println(new Gson().tojson(createUserResponse));
  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. "requestId":"44AF1DB3-119D-4A2E-B9F1-6155C613C9A9",
  3. "avatar":"",
  4. "createdAt":1572225460185,
  5. "defaultDriveId":"",
  6. "description":"",
  7. "domainId":"daily1405",
  8. "email":"",
  9. "nickName":"",
  10. "phone":"",
  11. "role":"user",
  12. "status":"enabled",
  13. "updatedAt":0,
  14. "userId":"test_user",
  15. "userName":"test_user"
  16. }

获取User

  • 以下代码用于获取User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. //获取User
  2. public static void getUser() throws Exception {
  3. try {
  4. GetUserRequest getUserRequest = new GetUserRequest();
  5. getUserRequest.userId = userId;
  6. GetUserResponse getUserResponse = client.getUser(getUserRequest, runtime);
  7. // 打印返回结果
  8. System.out.println(new Gson().tojson(getUserResponse));
  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. "requestId":"2E4824D1-AF73-4F3B-B8C0-C9F446DE4198",
  3. "avatar":"",
  4. "createdAt":1572226149810,
  5. "defaultDriveId":"",
  6. "description":"",
  7. "domainId":"daily1405",
  8. "email":"",
  9. "nickName":"",
  10. "phone":"",user
  11. "role":"user",
  12. "status":"enabled",
  13. "updatedAt":0,
  14. "userId":"test_user",
  15. "userName":"test_user"
  16. }

列举User

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

更新User

  • 以下代码用于更新User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. //更新User
  2. public static void updateUser() throws Exception {
  3. try {
  4. UpdateUserRequest updateUserRequest = new UpdateUserRequest();
  5. updateUserRequest.description = "test_user";
  6. // userId为之前创建的user
  7. updateUserRequest.userId = userId;
  8. UpdateUserResponse updateUserResponse = client.updateUser(updateUserRequest, runtime);
  9. // 打印返回结果
  10. System.out.println(new Gson().tojson(updateUserResponse));
  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. "requestId":"56FEFF5B-9E88-403B-AED4-92B051BE4E74",
  3. "avatar":"",
  4. "createdAt":1572226835585,
  5. "defaultDriveId":"",
  6. "description":"test_user",
  7. "domainId":"daily1405",
  8. "email":"",
  9. "nickName":"",
  10. "phone":"",
  11. "role":"user",
  12. "status":"enabled",
  13. "updatedAt":1572226880276,
  14. "userId":"test_user",
  15. "userName":"test_user"
  16. }

搜索User

  • 以下代码用于搜索User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // search user
  2. public static void searchUser() throws Exception {
  3. try {
  4. SearchUserRequest searchUserRequest = new SearchUserRequest();
  5. searchUserRequest.userName = "刘";
  6. ListUserResponse listUserResponse = client.searchUser(searchUserRequest,runtimeOptions);
  7. // 打印返回结果
  8. System.out.println(new Gson().toJson(listUserResponse));
  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. "requestId":"0CCADE95-61CC-41BC-9047-39145CC50054",
  3. "items":[
  4. {
  5. "avatar":"",
  6. "createdAt":1571915575499,
  7. "defaultDriveId":"",
  8. "description":"",
  9. "domainId":"daily1405",
  10. "email":"",
  11. "nickName":"刘***",
  12. "phone":"",
  13. "role":"user",
  14. "status":"enabled",
  15. "updatedAt":1571915575499,
  16. "userId":"5190******************2edbf5",
  17. "userName":"刘***"
  18. }
  19. ],
  20. "nextMarker":""
  21. }

删除User

  • 以下代码用于删除User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除User
  2. public static void deleteUser() throws Exception {
  3. try {
  4. DeleteUserRequest deleteUserRequest = new DeleteUserRequest();
  5. deleteUserRequest.userId = userId;
  6. client.deleteUser(deleteUserRequest, runtimeOptions);
  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. }

OSSPath Drive 相关API

说明

  • OSSPath 创建drive 需要先拿到store_id,需要先调用/v2/domain/list_stores的接口。

创建drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. public static void createDrive() throws Exception {
  2. try {
  3. // 获取store_id
  4. ListStoresRequest listStoresRequest = new ListStoresRequest();
  5. listStoresRequest.domainId = "daily1405";
  6. ListStoresResponse listStoresResponse = client.listStores(listStoresRequest, runtime);
  7. //打印结果
  8. System.out.println(new Gson().tojson(listStoresResponse));
  9. // 根据自己的需求从返回的结果中选择store_id
  10. String storeId = listStoresResponse.items[0].storeIdstore_id;
  11. //创建drive
  12. CreateDriveRequest createDriveRequest = new CreateDriveRequest();
  13. createDriveRequest.totalSize = 1000000L;
  14. createDriveRequest.driveName = "test_drive";
  15. createDriveRequest.description = "test_drive";
  16. createDriveRequest.driveType = "normal";
  17. createDriveRequest.relativePath = "/test_drive/";
  18. createDriveRequest.storeId = storeId;
  19. createDriveRequest.owner = userId;
  20. CreateDriveResponse createDriveResponse = client.createDrive(createDriveRequest, runtime);
  21. // 打印结果
  22. System.out.println(new Gson().toJson(createDriveResponse));
  23. } catch (TeaException e) {
  24. System.out.println(e.getMessage());
  25. System.out.println(e.getCode());
  26. System.out.println(new Gson().toJson(e.getData()));
  27. }
  28. }
  • 返回结果
  1. // list_stores response
  2. {
  3. "requestId":"84BDA281-9B83-477B-B2CC-41D47C8FD982",
  4. "items":[
  5. {
  6. "accelerateEndpoint":"",
  7. "basePath":"",
  8. "bucket":"ccp-daily-test",
  9. "customizedEndpoint":"",
  10. "endpoint":"https://oss-cn-hangzhou.aliyuncs.com",
  11. "internalEndpoint":"",
  12. "ownership":"custom",
  13. "policy":"",
  14. "roleArn":"",
  15. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  16. "type":"oss"
  17. }
  18. ]
  19. }
  20. // create drive response
  21. {
  22. "requestId":"E72DDD97-E99F-437D-B0B8-7C752C4E9548",
  23. "domainId":"daily1405",
  24. "driveId":"1902"
  25. }

列举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 = userId;
  7. ListDriveResponse listDriveResponse = client.listDrives(listDriveRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(listDriveResponse));
  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. "requestId":"871949D9-12A4-4220-B078-D6E937F90699",
  3. "items":[
  4. {
  5. "creator":"6c23c*************211",
  6. "description":"drive",
  7. "domainId":"daily1405",
  8. "driveId":"1902",
  9. "driveName":"test_drive",
  10. "driveType":"normal",
  11. "owner":"6c23c*************211",
  12. "relativePath":"/test_drive/",
  13. "status":"enabled",
  14. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  15. "totalSize":1000000,
  16. "usedSize":0
  17. }
  18. ],
  19. "nextMarker":""
  20. }

查询drive

  • 以下代码用于查询Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询 Drive
  2. public static void getDrive() throws Exception {
  3. try {
  4. GetDriveRequest getDriveRequest = new GetDriveRequest();
  5. getDriveRequest.driveId = driveId;
  6. GetDriveResponse getDriveResponse = client.getDrive(getDriveRequest, runtime);
  7. //打印结果
  8. System.out.println(new Gson().toJson(getDriveResponse));
  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. "requestId":"C50006A6-D23B-43AA-B634-3656CB23D542",
  3. "creator":"6c23c9*************f5211",
  4. "description":"test_drive",
  5. "domainId":"daily1405",
  6. "driveId":"1902",
  7. "driveName":"test_drive",
  8. "driveType":"normal",
  9. "owner":"6c23c9***************f5211",
  10. "relativePath":"/test_drive/",
  11. "status":"enabled",
  12. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  13. "totalSize":1000000,
  14. "usedSize":0
  15. }

列举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 = userId;
  7. ListDriveResponse listDriveResponse = client.listDrives(listDriveRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(listDriveResponse));
  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. "requestId":"51C9C618-25CE-487B-8A28-33827AF6DC99",
  3. "items":[
  4. {
  5. "creator":"",
  6. "description":"",
  7. "domainId":"daily1405",
  8. "driveId":"603",
  9. "driveName":"test_drive",
  10. "driveType":"normal",
  11. "owner":"****",
  12. "relativePath":"/test_drive/",
  13. "status":"enabled",
  14. "storeId":"55ff********904",
  15. "totalSize":100000,
  16. "usedSize":0
  17. }
  18. ],
  19. "nextMarker":""
  20. }

更新drive

  • 以下代码用于更新Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 更新 Drive
  2. public static void updateDrive() throws Exception {
  3. try {
  4. UpdateDriveRequest updateDriveRequest = new UpdateDriveRequest();
  5. updateDriveRequest.driveId = driveId;
  6. updateDriveRequest.description = "changed_drive";
  7. updateDriveRequest.totalSize = 1000000L;
  8. UpdateDriveResponse updateDriveResponse = client.updateDrive(updateDriveRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(updateDriveResponse));
  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. "requestId":"D88BC954-0BE8-4282-BDF3-6F964FC113DF",
  3. "creator":"6c23c**************9f5211",
  4. "description":"changed_drive",
  5. "domainId":"daily1405",
  6. "driveId":"1902",
  7. "driveName":"test_drive",
  8. "driveType":"normal",
  9. "owner":"6c23c9************5211",
  10. "relativePath":"/test_drive/",
  11. "status":"enabled",
  12. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  13. "totalSize":1000000,
  14. "usedSize":0
  15. }

删除drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 Drive
  2. public static void deleteDrive() throws Exception {
  3. try {
  4. DeleteDriveRequest deleteDriveRequest = new DeleteDriveRequest();
  5. deleteDriveRequest.driveId = driveId;
  6. client.deleteDrive(deleteDriveRequest, runtime);
  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. }

OSSPath Share 相关 API

创建Share

  • 以下代码用于创建Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 格式化过期时间
  2. public static String getISOTime() {
  3. long currentTime = System.currentTimeMillis();
  4. currentTime += 30 * 60 * 1000;
  5. Date date = new Date(currentTime);
  6. TimeZone timeZone = TimeZone.getTimeZone("UTC");
  7. DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  8. dateFormat.setTimeZone(timeZone);
  9. String nowAsISO = dateFormat.format(date);
  10. return nowAsISO;
  11. }
  12. // 创建 Share
  13. public static void createShare() throws Exception {
  14. try {
  15. String nowAsISO = getISOTime();
  16. CreateShareRequest createShareRequest = new CreateShareRequest();
  17. createShareRequest.shareFilePath = "/test_share/";
  18. createShareRequest.owner = userId;
  19. createShareRequest.shareName = "test_share";
  20. createShareRequest.permissions = new String[]{"FILE.LIST"};
  21. createShareRequest.expiration = nowAsISO;
  22. createShareRequest.driveId = driveId;
  23. CreateShareResponse createShareResponse = client.createShare(createShareRequest, runtime);
  24. // 打印结果
  25. System.out.println(new Gson().toJson(createShareResponse));
  26. } catch (TeaException e) {
  27. System.out.println(e.getMessage());
  28. System.out.println(e.getCode());
  29. System.out.println(new Gson().toJson(e.getData()));
  30. }
  31. }
  • 返回结果
  1. {
  2. "requestId":"CD01D212-D12A-4250-A773-8296E01B226A",
  3. "domainId":"daily1405",
  4. "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7"
  5. }

列举Share

  • 以下代码用于列举Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 列举 Share
  2. public static void listShare() throws Exception {
  3. try {
  4. ListShareRequest listShareRequest = new ListShareRequest();
  5. listShareRequest.owner = userId;
  6. ListShareResponse listShareResponse = client.listShare(listShareRequest, runtime);
  7. // 打印结果
  8. System.out.println(new Gson().toJson(listShareResponse));
  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. "requestId":"4A5EF007-CA9B-49E2-A2A9-68108E34F7F7",
  3. "items":[
  4. {
  5. "createdAt":"2019-10-28T06:22:21.163Z",
  6. "creator":"6c23c*************9f5211",
  7. "description":"changed_share",
  8. "domainId":"daily1405",
  9. "driveId":"1902",
  10. "expiration":"2019-10-28T06:52:21.139Z",
  11. "expired":false,
  12. "owner":"6c23c*****************3f9f5211",
  13. "permissions":[
  14. ],
  15. "shareFilePath":"/test_share/",
  16. "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
  17. "shareName":"test_share",
  18. "status":"enabled",
  19. "updatedAt":"2019-10-28T06:22:21.163Z"
  20. }
  21. ],
  22. "nextMarker":""
  23. }

查询Share

  • 以下代码用于查询Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询Share
  2. public static void getShare() throws Exception {
  3. try {
  4. GetShareRequest getShareRequest = new GetShareRequest();
  5. getShareRequest.shareId = shareID;
  6. GetShareResponse getShareResponse = client.getShare(getShareRequest, runtime);
  7. // 打印结果
  8. System.out.println(new Gson().toJson(getShareResponse));
  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. "requestId":"3DC7F2B1-68D4-4B1D-BF12-3F3814C22278",
  3. "createdAt":"2019-10-28T06:22:21.163Z",
  4. "creator":"6c23*************9f5211",
  5. "description":"",
  6. "domainId":"daily1405",
  7. "driveId":"1902",
  8. "expiration":"2019-10-28T06:52:21.139Z",
  9. "expired":false,
  10. "owner":"6c23c*************9f5211",
  11. "permissions":[
  12. "FILE.LIST"
  13. ],
  14. "shareFilePath":"/test_share/",
  15. "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
  16. "shareName":"test_share",
  17. "status":"enabled",
  18. "updatedAt":"2019-10-28T06:22:21.163Z"
  19. }

更新Share

  • 以下代码用于更新Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 更新 Share
  2. public static void updateShare() throws Exception {
  3. try {
  4. UpdateShareRequest updateShareRequest = new UpdateShareRequest();
  5. updateShareRequest.shareId = shareID;
  6. updateShareRequest.description = "changed_share";
  7. UpdateShareResponse updateShareResponse = client.updateShare(updateShareRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(updateShareResponse));
  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. "requestId":"F693F9EB-863E-49AF-AE0D-AD158B3613D5",
  3. "createdAt":"2019-10-28T06:22:21.163Z",
  4. "creator":"6c23c98********************f5211",
  5. "description":"changed_share",
  6. "domainId":"daily1405",
  7. "driveId":"1902",
  8. "expiration":"2019-10-28T06:52:21.139Z",
  9. "expired":false,
  10. "owner":"6c23c98********************f5211",
  11. "permissions":[
  12. "FILE.LIST"
  13. ],
  14. "shareFilePath":"/test_share/",
  15. "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
  16. "shareName":"test_share",
  17. "status":"enabled",
  18. "updatedAt":"2019-10-28T06:22:21.163Z"
  19. }

删除Share

  • 以下代码用于删除Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 Share
  2. public static void deleteShare() throws Exception {
  3. try {
  4. DeleteShareRequest deleteShareRequest = new DeleteShareRequest();
  5. deleteShareRequest.shareId = shareID;
  6. client.deleteShare(deleteShareRequest,runtime);
  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. }

OSSPath File相关 API

创建File

  • 以下代码用于创建File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  • 说明: 此处创建File 还需要上传文件到OSS, 具体操作详见最佳实践。
  1. // 创建文件
  2. public static void createFile() throws Exception {
  3. try {
  4. OSSCreateFileRequest ossCreateFileRequest = new OSSCreateFileRequest();
  5. ossCreateFileRequest.type = "file";
  6. ossCreateFileRequest.name = "a.txt";
  7. ossCreateFileRequest.driveId = driveId;
  8. ossCreateFileRequest.parentFilePath = "/";
  9. ossCreateFileRequest.contentType = "text/plain";
  10. OSSCreateFileResponse ossCreateFileResponse = client.createFile(ossCreateFileRequest, runtime);
  11. // 打印结果
  12. System.out.println(new Gson().toJson(ossCreateFileResponse));
  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. }
  • 返回结果
  1. // 此处url省略部分,实际值详见接口调用返回的结果。
  2. {
  3. "requestId":"E30F4EC3-0655-44DD-8A8A-8D71848CCC44",
  4. "domainId":"daily1405",
  5. "driveId":"1902",
  6. "filePath":"/a.txt",
  7. "partInfoList":[
  8. {
  9. "partNumber":1,
  10. "uploadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
  11. }
  12. ],
  13. "type":"file",
  14. "uploadId":"872F52602EB343D0ADCE3E75D008FB16"
  15. }

列举File

  • 以下代码用于列举File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 列举文件
  2. public static void listFile() throws Exception {
  3. try {
  4. OSSCreateFileRequest ossCreateFileRequest = new OSSCreateFileRequest();
  5. ossCreateFileRequest.type = "file";
  6. ossCreateFileRequest.name = "a.txt";
  7. ossCreateFileRequest.driveId = driveId;
  8. ossCreateFileRequest.parentFilePath = "/";
  9. ossCreateFileRequest.contentType = "text/plain";
  10. OSSCreateFileResponse ossCreateFileResponse = client.createFile(ossCreateFileRequest, runtime);
  11. // 打印结果
  12. System.out.println(new Gson().toJson(ossCreateFileResponse));
  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. }
  • 返回结果
  1. // 此处url省略部分,实际值详见接口调用返回的结果。
  2. {
  3. "requestId":"67AF911A-F8F8-4FD4-994C-797642468AFF",
  4. "items":[
  5. {
  6. "contentType":"",
  7. "domainId":"daily1405",
  8. "downloadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16",
  9. "driveId":"1902",
  10. "fileExtension":"txt",
  11. "filePath":"/test_folder/a.txt",
  12. "name":"a.txt",
  13. "parentFilePath":"/test_folder/",
  14. "size":8,
  15. "status":"available",
  16. "type":"file",
  17. "updatedAt":"2019-10-28T06:22:23Z",
  18. "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
  19. }
  20. ],
  21. "nextMarker":""
  22. }

查询File

  • 以下代码用于查询File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询File
  2. public static void getFile() throws Exception {
  3. try {
  4. OSSGetFileRequest ossGetFileRequest = new OSSGetFileRequest();
  5. ossGetFileRequest.driveId = driveId;
  6. ossGetFileRequest.filePath = filePath;
  7. OSSGetFileResponse ossGetFileResponse = client.getFile(ossGetFileRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(ossGetFileResponse));
  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. "requestId":"0AC2633C-A0F1-402B-9CCD-EE98FAD573B7",
  3. "contentType":"text/plain",
  4. "domainId":"daily1405",
  5. "downloadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16",
  6. "driveId":"1902",
  7. "fileExtension":"",
  8. "filePath":"/a.txt",
  9. "name":"a.txt",
  10. "parentFilePath":"/",
  11. "size":8,
  12. "status":"available",
  13. "type":"file",
  14. "updatedAt":"2019-10-28T06:22:23Z",
  15. "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
  16. }

移动File

  • 以下代码用于移动File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 移动 File
  2. public static void moveFile() throws Exception {
  3. try {
  4. OSSMoveFileRequest ossMoveFileRequest = new OSSMoveFileRequest();
  5. ossMoveFileRequest.driveId = driveId;
  6. ossMoveFileRequest.filePath = filePath;
  7. ossMoveFileRequest.toParentFilePath = folderPath;
  8. OSSMoveFileResponse ossMoveFileResponse = client.moveFile(ossMoveFileRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ossMoveFileResponse));
  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. "requestId":"757C1547-9354-444B-92D2-127A83AEA470",
  3. "domainId":"daily1405",
  4. "driveId":"1902",
  5. "filePath":"/test_folder/a.txt"
  6. }

复制File

  • 以下代码用于复制File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 复制 File
  2. public static void copyFile() throws Exception {
  3. try {
  4. OSSCopyFileRequest ossCopyFileRequest = new OSSCopyFileRequest();
  5. ossCopyFileRequest.filePath = movedFilePath;
  6. ossCopyFileRequest.driveId = driveId;
  7. ossCopyFileRequest.toParentFilePath = "/";
  8. OSSCopyFileResponse ossCopyFileResponse = client.copyFile(ossCopyFileRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ossCopyFileResponse));
  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. "requestId":"24C7E91D-9715-4AE7-BDA1-4A082D7D3AA3",
  3. "domainId":"daily1405",
  4. "driveId":"1902",
  5. "filePath":"/a.txt"
  6. }

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File上传地址
  2. public static void getFileUploadUrl() throws Exception {
  3. try {
  4. UploadPartInfo uploadPartInfo = new UploadPartInfo();
  5. uploadPartInfo.partNumber = 1L;
  6. OSSGetUploadUrlRequest ossGetUploadUrlRequest = new OSSGetUploadUrlRequest();
  7. ossGetUploadUrlRequest.driveId = driveId;
  8. ossGetUploadUrlRequest.filePath = filePath;
  9. ossGetUploadUrlRequest.uploadId = uploadId;
  10. ossGetUploadUrlRequest.partInfoList = new UploadPartInfo[]{uploadPartInfo};
  11. OSSGetUploadUrlResponse ossGetUploadUrlResponse = client.getUploadUrl(ossGetUploadUrlRequest, runtime);
  12. // 打印结果
  13. System.out.println(new Gson().toJson(ossGetUploadUrlResponse));
  14. } catch (TeaException e) {
  15. System.out.println(e.getMessage());
  16. System.out.println(e.getCode());
  17. System.out.println(new Gson().toJson(e.getData()));
  18. }
  19. }
  • 返回结果
  1. {
  2. "requestId":"64062AF5-7ADF-4F7D-9878-9E2FA6AABE53",
  3. "createAt":"2019-10-28T06:22:22.231Z",
  4. "domainId":"daily1405",
  5. "driveId":"1902",
  6. "filePath":"/a.txt",
  7. "partInfoList":[
  8. {
  9. "partNumber":1,
  10. "uploadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
  11. }
  12. ],
  13. "uploadId":"872F52602EB343D0ADCE3E75D008FB16"
  14. }

获取File下载地址

  • 以下代码用于获取File下载地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File下载地址
  2. public static void getFileDownloadUrl() throws Exception {
  3. try {
  4. OSSGetDownloadUrlRequest ossGetDownloadUrlRequest = new OSSGetDownloadUrlRequest();
  5. ossGetDownloadUrlRequest.driveId = driveId;
  6. ossGetDownloadUrlRequest.filePath = filePath;
  7. ossGetDownloadUrlRequest.expireSec = 3600L;
  8. OSSGetDownloadUrlResponse ossGetDownloadUrlResponse = client.getDownloadUrl(ossGetDownloadUrlRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ossGetDownloadUrlResponse));
  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. "requestId":"6760BC8F-AA33-4862-AA26-0F8FA57EAF39",
  3. "expiration":"2019-10-28T07:22:24.044Z",
  4. "method":"GET",
  5. "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
  6. }

Complete File

  • 以下代码用于创建Complete File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  • 说明: 此处请求参数中的ETag是请求uploadUrl上传完成后从返回的Response Headers中获取的,详见最佳实践。
  1. // 合并File分片
  2. public static void completeFile() throws Exception {
  3. try {
  4. UploadPartInfo uploadPartInfo1 = new UploadPartInfo();
  5. uploadPartInfo1.etag = etag;
  6. uploadPartInfo1.partNumber = 1L;
  7. OSSCompleteFileRequest ossCompleteFileRequest = new OSSCompleteFileRequest();
  8. ossCompleteFileRequest.driveId = driveId;
  9. ossCompleteFileRequest.filePath = filePath;
  10. ossCompleteFileRequest.uploadId = newUploadId;
  11. ossCompleteFileRequest.partInfoList = new UploadPartInfo[]{uploadPartInfo1};
  12. OSSCompleteFileResponse ossCompleteFileResponse = client.completeFile(ossCompleteFileRequest, runtime);
  13. // 打印结果
  14. System.out.println(new Gson().toJson(ossCompleteFileResponse));
  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. }
  • 返回结果
  1. {
  2. "requestId":"B05D6ABD-AF30-4E10-B1FA-4168941EE750",
  3. "contentType":"text/plain",
  4. "crc64Hash":"6668564720710875145",
  5. "domainId":"daily1405",
  6. "driveId":"1902",
  7. "fileExtension":"",
  8. "filePath":"/a.txt",
  9. "name":"a.txt",
  10. "parentFilePath":"/",
  11. "size":0,
  12. "status":"uploading",
  13. "type":"file",
  14. "uploadId":"872F52602EB343D0ADCE3E75D008FB16",
  15. "crc":"6668564720710875145"
  16. }

删除File

  • 以下代码用于删除File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 file
  2. public static void deleteFile() throws Exception {
  3. try {
  4. OSSDeleteFileRequest ossDeleteFileRequest = new OSSDeleteFileRequest();
  5. ossDeleteFileRequest.driveId = driveId;
  6. ossDeleteFileRequest.filePath = copiedFilePath;
  7. client.deleteFile(ossDeleteFileRequest, runtime);
  8. // 此接口不返回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. }

列举Stores File

  • 以下代码用于列举Stores File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档.
  1. // 列举 Stores File
  2. public static void listStoreFiles() throws Exception {
  3. try {
  4. // list Stores
  5. ListStorageRequest listStoresRequest = new ListStorageRequest();
  6. listStoresRequest.domainId = "daily1405";
  7. ListStorageResponse listStoresResponse = client.listStorage(listStoresRequest, runtime);
  8. System.out.println(new Gson().toJson(listStoresResponse));
  9. String storeId = listStoresResponse.items[0].storeId;
  10. ListStorageFileRequest listStorageFileRequest = new ListStorageFileRequest();
  11. listStorageFileRequest.storeId = storeId;
  12. listStorageFileRequest.limit = 10L;
  13. // list store_file
  14. ListStorageFileResponse listStorageFileResponse = client.listStoragefile(listStorageFileRequest,runtime);
  15. // 打印结果
  16. System.out.println(new Gson().toJson(listStorageFileResponse));
  17. } catch (TeaException e) {
  18. System.out.println(e.getMessage());
  19. System.out.println(e.getCode());
  20. System.out.println(new Gson().toJson(e.getData()));
  21. }
  22. }
  • 返回结果
  1. {
  2. "requestId":"DB0A660A-C4B6-4620-BF83-B6CFCA31162C",
  3. "items":[
  4. {
  5. "domainId":"",
  6. "name":"***y",
  7. "parentFilePath":"/",
  8. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  9. "type":"folder"
  10. },
  11. {
  12. "domainId":"",
  13. "name":"superadmin",
  14. "parentFilePath":"/",
  15. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  16. "type":"folder"
  17. },
  18. {
  19. "domainId":"",
  20. "name":"test",
  21. "parentFilePath":"/",
  22. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  23. "type":"folder"
  24. },
  25. {
  26. "domainId":"",
  27. "name":"test_drive",
  28. "parentFilePath":"/",
  29. "storeId":"90fba27e9c40452d91d83b204aee1d9b",
  30. "type":"folder"
  31. }
  32. ],
  33. "nextMarker":""
  34. }

CCPPath 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 = userId ;
  9. CreateDriveResponse createDriveResponse = client.createDrive(createDriveRequest, runtime);
  10. // 打印结果
  11. System.out.println(new Gson().toJson(createDriveResponse));
  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. "requestId":"6B556975-1A8D-4517-9227-D1CED662A24B",
  3. "domainId":"daily1404",
  4. "driveId":"603"
  5. }

列举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 = userId;
  7. ListDriveResponse listDriveResponse = client.listDrives(listDriveRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(listDriveResponse));
  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. "requestId":"51C9C618-25CE-487B-8A28-33827AF6DC99",
  3. "items":[
  4. {
  5. "creator":"",
  6. "description":"",
  7. "domainId":"daily1404",
  8. "driveId":"603",
  9. "driveName":"test_drive",
  10. "driveType":"normal",
  11. "owner":"ldh",
  12. "relativePath":"",
  13. "status":"enabled",
  14. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  15. "totalSize":100000,
  16. "usedSize":0
  17. }
  18. ],
  19. "nextMarker":""
  20. }

查询drive

  • 以下代码用于查询drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询 Drive
  2. public static void getDrive() throws Exception {
  3. try {
  4. GetDriveRequest getDriveRequest = new GetDriveRequest();
  5. getDriveRequest.driveId = driveId;
  6. GetDriveResponse getDriveResponse = client.getDrive(getDriveRequest, runtime);
  7. //打印结果
  8. System.out.println(new Gson().toJson(getDriveResponse));
  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. "requestId":"2A361340-F298-4685-8C8B-B34C12EE2F41",
  3. "creator":"",
  4. "description":"",
  5. "domainId":"daily1404",
  6. "driveId":"603",
  7. "driveName":"test_drive",
  8. "driveType":"normal",
  9. "owner":"ldh",
  10. "relativePath":"",
  11. "status":"enabled",
  12. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  13. "totalSize":100000,
  14. "usedSize":0
  15. }

更新drive

  • 以下代码用于更新drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 更新 Drive
  2. public static void updateDrive() throws Exception {
  3. try {
  4. UpdateDriveRequest updateDriveRequest = new UpdateDriveRequest();
  5. updateDriveRequest.driveId = driveId;
  6. updateDriveRequest.description = "changed_drive";
  7. updateDriveRequest.totalSize = 1000000L;
  8. UpdateDriveResponse updateDriveResponse = client.updateDrive(updateDriveRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(updateDriveResponse));
  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. "requestId":"501B210E-0D4C-46E9-9D35-F1E86074A1D8",
  3. "creator":"",
  4. "description":"changed_drive",
  5. "domainId":"daily1404",
  6. "driveId":"603",
  7. "driveName":"test_drive",
  8. "driveType":"normal",
  9. "owner":"ldh",
  10. "relativePath":"",
  11. "status":"enabled",
  12. "storeId":"55ff60f575b24a8c97378f1e0a946904",
  13. "totalSize":1000000,
  14. "usedSize":0
  15. }

删除drive

  • 以下代码用于删除drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 Drive
  2. public static void deleteDrive() throws Exception {
  3. try {
  4. DeleteDriveRequest deleteDriveRequest = new DeleteDriveRequest();
  5. deleteDriveRequest.driveId = driveId;
  6. client.deleteDrive(deleteDriveRequest, runtime);
  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. }

CCPPath File 相关 API

创建File

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 创建文件
  2. public static void createFile() throws Exception {
  3. try {
  4. CCPCreateFileRequest ccpCreateFileRequest = new CCPCreateFileRequest();
  5. ccpCreateFileRequest.type = "file";
  6. ccpCreateFileRequest.name = "a.txt";
  7. ccpCreateFileRequest.driveId = driveId;
  8. ccpCreateFileRequest.parentFileId = "root";
  9. ccpCreateFileRequest.contentType = "text/plain";
  10. CCPCreateFileResponse ccpCreateFileResponse = client.createFile(ccpCreateFileRequest, runtime);
  11. // 打印结果
  12. System.out.println(new Gson().toJson(ccpCreateFileResponse));
  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. }
  • 返回结果
  1. {
  2. "requestId":"70BC9CC0-A4E9-49A1-9384-6C9CEF286981",
  3. "domainId":"daily1404",
  4. "driveId":"603",
  5. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  6. "parentFileId":"root",
  7. "partInfoList":[
  8. {
  9. "partNumber":1,
  10. "uploadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
  11. }
  12. ],
  13. "rapidUpload":false,
  14. "type":"file",
  15. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
  16. }

列举File

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

查询File

  • 以下代码用于查询File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 查询File
  2. public static void getFile() throws Exception {
  3. try {
  4. CCPGetFileRequest ccpGetFileRequest = new CCPGetFileRequest();
  5. ccpGetFileRequest.driveId = driveId;
  6. ccpGetFileRequest.fileId = fileId;
  7. CCPGetFileResponse ccpGetFileResponse = client.getFile(ccpGetFileRequest, runtime);
  8. // 打印结果
  9. System.out.println(new Gson().toJson(ccpGetFileResponse));
  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. "requestId":"C32B9339-C65E-4AB6-86C9-AC4F172A0C63",
  3. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  4. "contentHashName":"sha1",
  5. "contentType":"text/plain",
  6. "crc64Hash":"318318745347147982",
  7. "createdAt":"2019-10-28T08:40:54.398Z",
  8. "domainId":"daily1404",
  9. "downloadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943.txt",
  10. "driveId":"603",
  11. "fileExtension":"txt",
  12. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  13. "hidden":false,
  14. "name":"a.txt",
  15. "parentFileId":"root",
  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/*****F943"
  23. }

移动File

  • 以下代码用于移动File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 移动 File
  2. public static void moveFile() throws Exception {
  3. try {
  4. CCPMoveFileRequest ccpMoveFileRequest = new CCPMoveFileRequest();
  5. ccpMoveFileRequest.driveId = driveId;
  6. ccpMoveFileRequest.fileId = fileId;
  7. ccpMoveFileRequest.toParentFileId = folderId;
  8. CCPMoveFileResponse ccpMoveFileResponse = client.moveFile(ccpMoveFileRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ccpMoveFileResponse));
  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. "requestId":"B6E69EDE-D3C9-410E-B4D1-059355A38F34",
  3. "domainId":"daily1404",
  4. "driveId":"603",
  5. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f"
  6. }

复制File

  • 以下代码用于复制File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 复制 File
  2. public static void copyFile() throws Exception {
  3. try {
  4. CCPCopyFileRequest ccpCopyFileRequest = new CCPCopyFileRequest();
  5. ccpCopyFileRequest.fileId = fileId;
  6. ccpCopyFileRequest.driveId = driveId;
  7. ccpCopyFileRequest.toParentFileId = "root";
  8. CCPCopyFileResponse ccpCopyFileResponse = client.copyFile(ccpCopyFileRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ccpCopyFileResponse));
  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. "requestId":"F001CD18-8BD8-481E-B1F1-8B804709A865",
  3. "domainId":"daily1404",
  4. "driveId":"603",
  5. "fileId":"5db6a997ce31eb635f2e4f4c9163ec3bd10af459"
  6. }

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File上传地址
  2. public static void getFileUploadUrl() throws Exception {
  3. try {
  4. UploadPartInfo uploadPartInfo = new UploadPartInfo();
  5. uploadPartInfo.partNumber = 1L;
  6. CCPGetUploadUrlRequest ccpGetUploadUrlRequest = new CCPGetUploadUrlRequest();
  7. ccpGetUploadUrlRequest.driveId = driveId;
  8. ccpGetUploadUrlRequest.fileId = fileId;
  9. ccpGetUploadUrlRequest.uploadId = uploadId;
  10. ccpGetUploadUrlRequest.partInfoList = new UploadPartInfo[]{uploadPartInfo};
  11. CCPGetUploadUrlResponse ccpGetUploadUrlResponse = client.getUploadUrl(ccpGetUploadUrlRequest, runtime);
  12. // 打印结果
  13. System.out.println(new Gson().toJson(ccpGetUploadUrlResponse));
  14. } catch (TeaException e) {
  15. System.out.println(e.getMessage());
  16. System.out.println(e.getCode());
  17. System.out.println(new Gson().toJson(e.getData()));
  18. }
  19. }
  • 返回结果
  1. {
  2. "requestId":"8E80078C-0749-4C03-A3AB-F0274D652114",
  3. "createAt":"2019-10-28T08:40:54.519Z",
  4. "domainId":"daily1404",
  5. "driveId":"603",
  6. "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
  7. "partInfoList":[
  8. {
  9. "partNumber":1,
  10. "uploadUrl":"https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db6a996aa1292d7563644f0bc4847107171ed7f%2F5db6a996e8f9ecd76fae49d196a93eeaf7009b09?Expires=1572252114&OSSAccessKeyId=LTAIi5kx6X0mwR8k&Signature=TOMPiGXx0733JZZEzSCPoDMUk%2FE%3D&partNumber=1&uploadId=DEB65A38FCCA410BAC6DD23A8A11F943"
  11. }
  12. ],
  13. "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
  14. }

获取File下载地址

  • 以下代码用于获取File下载地,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取File下载地址
  2. public static void getFileDownloadUrl() throws Exception {
  3. try {
  4. CCPGetDownloadUrlRequest ccpGetDownloadUrlRequest = new CCPGetDownloadUrlRequest();
  5. ccpGetDownloadUrlRequest.driveId = driveId;
  6. ccpGetDownloadUrlRequest.fileId = fileId;
  7. ccpGetDownloadUrlRequest.expireSec = 3600L;
  8. CCPGetDownloadUrlResponse ccpGetDownloadUrlResponse = client.getDownloadUrl(ccpGetDownloadUrlRequest, runtime);
  9. // 打印结果
  10. System.out.println(new Gson().toJson(ccpGetDownloadUrlResponse));
  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. "requestId":"29B59FA4-C6BD-44F6-809F-BF788F435145",
  3. "expiration":"2019-10-28T09:40:55.716Z",
  4. "method":"GET",
  5. "url":"https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db5910348946ee1835e4c139165301333e5f743%2F5db59103967263443aef47799253e5e63970d7ce?Expires=1572252955&OSSAccessKeyId=LTAIi5kx6X0mwR8k&Signature=urIb%2BP03nvZg0%2Birshzeow5u6ow%3D&response-content-disposition=attachment%3Bfilename%3Da.txt"
  6. }

Complete File

  • 以下代码用于Complete File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 合并File分片
  2. public static void completeFile() throws Exception {
  3. try {
  4. UploadPartInfo uploadPartInfo1 = new UploadPartInfo();
  5. uploadPartInfo1.etag = etag;
  6. uploadPartInfo1.partNumber = 1L;
  7. CCPCompleteFileRequest ccpCompleteFileRequest = new CCPCompleteFileRequest();
  8. ccpCompleteFileRequest.driveId = driveId;
  9. ccpCompleteFileRequest.fileId = fileId;
  10. ccpCompleteFileRequest.uploadId = newUploadId;
  11. ccpCompleteFileRequest.partInfoList = new UploadPartInfo[]{uploadPartInfo1};
  12. CCPCompleteFileResponse ccpCompleteFileResponse = client.completeFile(ccpCompleteFileRequest, runtime);
  13. // 打印结果
  14. System.out.println(new Gson().toJson(ccpCompleteFileResponse));
  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. }
  • 返回结果
  1. {
  2. "requestId":"6E4FCA06-8554-425A-8447-1A3826B064CA",
  3. "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
  4. "contentHashName":"sha1",
  5. "contentType":"text/plain",
  6. "crc64Hash":"318318745347147982",
  7. "createdAt":"2019-10-28T08:40:54.398Z",
  8. "domainId":"daily1404",
  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. "crc":""
  22. }

更新File

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

搜索File

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

删除File

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 删除 file
  2. public static void deleteFile() throws Exception {
  3. try {
  4. CCPDeleteFileRequest ccpDeleteFileRequest1 = new CCPDeleteFileRequest();
  5. ccpDeleteFileRequest1.driveId = driveId;
  6. ccpDeleteFileRequest1.fileId = copiedFileId;
  7. client.deleteFile(ccpDeleteFileRequest1, runtime);
  8. // 此接口不返回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. }

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

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

创建APP(获取ClientID, ClientSecret)

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

CCPPath 上传文件

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

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----