Java SDK 简介

Java SDK 简介

说明

推荐您试用新版本SDK

下载地址

说明

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

安装步骤

安装 Java 开发环境

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

安装PDS Java SDK

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

StandardMode

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-pds</artifactId>
    <version>Use the version shown in the maven badge</version>
</dependency>

HostingMode

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-pds-hosting-mode</artifactId>
    <version>Use the version shown in the maven badge</version>
</dependency>

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

初始化Client

说明

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

AK & SK 初始化客户端

说明

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

import com.aliyun.pds.client.Client;
import com.aliyun.pds.client.models.*;

public class Demo {
    private static Client client;

    public static void createClient() throws IllegalAccessException {
        Config config = new Config();
        config.domainId = "your domainId";
        config.protocol = "https";
        config.accessKeyId = System.getenv("accessKeyId");
        config.accessKeySecret = System.getenv("accessKeySecret");
        client = new Client(config);
    }
}

AccessToken & RefreshToken 初始化客户端

import com.aliyun.pds.client.Client;
import com.aliyun.pds.client.models.*;

public class Demo {
    private static Client client;
    // clientId 和 clientSecret 可以在控制台创建应用时获取
    public static void createClient() throws IllegalAccessException {
        Config config = new Config();
        config.domainId = "your domainId";
        config.protocol = "https";
        config.clientId = System.getenv("clientId");
        config.clientSecret = System.getenv("clientSecret");
        config.accessToken = System.getenv("accessToken");
        config.refreshToken =  System.getenv("refreshToken");
        config.expireTime =   System.getenv("expireTime");
        client = new Client(config);
    }
}

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

构造请求

Account 相关 API

通过账号获取访问令牌

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void getAccessTokenByLinkInfo() throws Exception {
  try {
    // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
    GetAccessTokenByLinkInfoRequest getAccessTokenByLinkInfoRequest = new GetAccessTokenByLinkInfoRequest();
    getAccessTokenByLinkInfoRequest.identity = "132********25";
    getAccessTokenByLinkInfoRequest.type = "mobile";

    GetAccessTokenByLinkInfoModel tokenResponse = client.getAccessTokenByLinkInfo(getAccessTokenByLinkInfoRequest);
    System.out.println(new Gson().toJson(tokenResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果参见手机号注册的返回结果

获取用户绑定信息

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void getLinkInfoByUserId() throws Exception {
  try {
    // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
    GetLinkInfoByUserIDRequest linkInfoByUserIDRequest = new GetLinkInfoByUserIDRequest();
    linkInfoByUserIDRequest.userId = "6c23*********5211";

    GetLinkInfoByUserIdModel listResponse = client.getLinkInfoByUserId(linkInfoByUserIDRequest);
    System.out.println(new Gson().toJson(listResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果

{
    "items":[
        {
            "authenticationType":"mobile",
            "createdAt":1571905906341,
            "domainId":"daily1405",
            "identity":"13******225",
            "lastLoginTime":1571905906341,
            "status":"normal",
            "userId":"6c23c9******3f9f5211"
        }
    ]
}

获取用户认证方式

  • 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void getLinkInfo() throws Exception {
  try {
    // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
    GetByLinkInfoRequest getByLinkInfoRequest = new GetByLinkInfoRequest();
    getByLinkInfoRequest.identity = "adsfqwrsfad";
    getByLinkInfoRequest.type = "ding";

    GetLinkInfoModel linkInfoResponse = client.getLinkInfo(getByLinkInfoRequest);
    System.out.println(new Gson().toJson(linkInfoResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果

{
    "authenticationType":"ding",
    "createdAt":1572427460313,
    "domainId":"daily1405",
    "identity":"adsfqwrsfad",
    "lastLoginTime":1572427460313,
    "status":"wait_link",
    "userId":"6c23c98*****7d8b3f9f5211"
}

绑定用户认证方式

  • 以下代码用于绑定用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void userLinkInfo() throws Exception {
  try {
    // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端
    AccountLinkRequest linkInfo = new AccountLinkRequest();
    linkInfo.type = "taobao";
    linkInfo.identity = "1234";
    linkInfo.userId = "1eb15*************eff97708cb";
    linkInfo.status ="wait_link";

    LinkModel tokenResponse = client.link(linkInfo);
    System.out.println(new Gson().toJson(tokenResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果

{
    "avatar":"",
    "defaultDriveId":"",
    "existLink":[
        {
            "identity":"13*****225",
            "type":"mobile"
        }
    ],
    "expireTime":"",
    "expiresIn":300,
    "needLink":true,
    "nickName":"",
    "refreshToken":"",
    "role":"",
    "state":"",
    "tokenType":"Bearer",
    "userId":"",
    "userName":""
}

取消绑定关系

  • 以下代码用于取消绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void cancelLink() throws Exception {
  try {
    CancelLinkRequest cancelLinkRequest = new CancelLinkRequest();
    cancelLinkRequest.temporaryToken = "eyJhbGciOiJSUzI1N*****pwc";

    CancelLinkModel tokenResponse = client.cancelLink(cancelLinkRequest);

    System.out.println(new Gson().toJson(tokenResponse.body));;
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果参见手机号注册的返回结果

确定绑定关系

  • 以下代码用于确定绑定关系,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

  • 此接口只支持Access Token,不要使用AK,SK初始化客户端。

public void confirmLink() throws Exception {
  try {
    ConfirmLinkRequest confirmLinkRequest = new ConfirmLinkRequest();
    confirmLinkRequest.temporaryToken = "eyJhbGciOiJSUzI1NiIsI(***qE";

    ConfirmLinkModel tokenResponse = client.confirmLink(confirmLinkRequest);

    System.out.println(new Gson().toJson(tokenResponse));
  } catch (TeaException e) {
    System.out.println(e.getCode());
    System.out.println(e.getMessage());
    System.out.println(e.getData());
  }

}
  • 返回结果参见手机号注册的返回结果

User 相关 API

创建User

  • 以下代码用于创建User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 创建User
public static void createUser() throws Exception {
  try {
    CreateUserRequest createUserRequest = new CreateUserRequest();
    createUserRequest.userId = "test_user";
    createUserRequest.role = "user";
    createUserRequest.description = "123";
    CreateUserModel createUserResponse = client.createUser(createUserRequest);
    System.out.println(new Gson().toJson(createUserResponse.body));


  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "avatar":"",
    "createdAt":1572225460185,
    "defaultDriveId":"",
    "description":"",
    "domainId":"daily1405",
    "email":"",
    "nickName":"",
    "phone":"",
    "role":"user",
    "status":"enabled",
    "updatedAt":0,
    "userId":"test_user",
    "userName":"test_user"
}

获取User

  • 以下代码用于获取User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

//获取User
public static void getUser() throws Exception {
  try {
    GetUserRequest getUserRequest = new GetUserRequest();
    getUserRequest.userId = "test_user";
    GetUserModel getUserResponse = client.getUser(getUserRequest);
    System.out.println(new Gson().toJson(getUserResponse.body));


  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "avatar":"",
    "createdAt":1572226149810,
    "defaultDriveId":"",
    "description":"",
    "domainId":"daily1405",
    "email":"",
    "nickName":"",
    "phone":"",user
    "role":"user",
    "status":"enabled",
    "updatedAt":0,
    "userId":"test_user",
    "userName":"test_user"
}

列举User

  • 以下代码用于列举User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

//列举User
public static void listUsers() throws Exception {
  try {
    ListUserRequest listUserRequest = new ListUserRequest();
    listUserRequest.limit = 10;
    ListUsersModel listUserResponse = client.listUsers(listUserRequest);

    System.out.println(new Gson().toJson(listUserResponse.body));

  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "items":[
        {
            "avatar":"",
            "createdAt":1571903980532,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"xxxxxx",
            "phone":"",
            "role":"user",
            "status":"enabled",
            "updatedAt":1571903980532,
            "userId":"1eb15a*****************8cb",
            "userName":"xxxxxx"
        },
        {
            "avatar":"",
            "createdAt":1571915575499,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"xxxxxx",
            "phone":"",
            "role":"user",
            "status":"enabled",
            "updatedAt":1571915575499,
            "userId":"51901a4************dbf5",
            "userName":"xxxxxx"
        },
        {
            "avatar":"",
            "createdAt":1571903776751,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"xxxxxx",
            "phone":"",
            "role":"user",
            "status":"enabled",
            "updatedAt":1571903776751,
            "userId":"621a3c***************8ecd",
            "userName":"xxxxxx"
        },
        {
            "avatar":"",
            "createdAt":1571905906346,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"1329***25",
            "phone":"132****25",
            "role":"admin",
            "status":"enabled",
            "updatedAt":1571907859554,
            "userId":"6c23c98****************f5211",
            "userName":"132**********5"
        },
        {
            "avatar":"",
            "createdAt":1572226835585,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"",
            "phone":"",
            "role":"admin",
            "status":"enabled",
            "updatedAt":0,
            "userId":"xxxxxx",
            "userName":"xxxxxx"
        },
        {
            "avatar":"",
            "createdAt":1571887988846,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"superadmin",
            "phone":"",
            "role":"superadmin",
            "status":"enabled",
            "updatedAt":0,
            "userId":"superadmin",
            "userName":"superadmin"
        }
    ],
    "nextMarker":""
}

更新User

  • 以下代码用于更新User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

//更新User
public static void updateUser() throws Exception {
  try {
    UpdateUserRequest updateUserRequest = new UpdateUserRequest();
    updateUserRequest.description = "changed_user";
    updateUserRequest.userId = "test_user";
    UpdateUserModel updateUserResponse = client.updateUser(updateUserRequest);

    System.out.println(new Gson().toJson(updateUserResponse.body));

  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "avatar":"",
    "createdAt":1572226835585,
    "defaultDriveId":"",
    "description":"test_user",
    "domainId":"daily1405",
    "email":"",
    "nickName":"",
    "phone":"",
    "role":"user",
    "status":"enabled",
    "updatedAt":1572226880276,
    "userId":"test_user",
    "userName":"test_user"
}

搜索User

  • 以下代码用于搜索User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// search user
public static void searchUser() throws Exception {
  try {
    SearchUserRequest searchUserRequest = new SearchUserRequest();

    SearchUserModel listUserResponse1 = client.searchUser(searchUserRequest);

    System.out.println(new Gson().toJson(listUserResponse1.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "items":[
        {
            "avatar":"",
            "createdAt":1571915575499,
            "defaultDriveId":"",
            "description":"",
            "domainId":"daily1405",
            "email":"",
            "nickName":"刘***",
            "phone":"",
            "role":"user",
            "status":"enabled",
            "updatedAt":1571915575499,
            "userId":"5190******************2edbf5",
            "userName":"刘***"
        }
    ],
    "nextMarker":""
}

删除User

  • 以下代码用于删除User,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 删除User
public static void deleteUser() throws Exception {
  try {
    DeleteUserRequest deleteUserRequest = new DeleteUserRequest();
    deleteUserRequest.userId = "test_user";

    DeleteUserModel deleteUserResponse = client.deleteUser(deleteUserRequest);

    //此接口没有返回body
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}

HostingMode Drive 相关API

说明

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

创建drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static String adminListStores() throws Exception {
  try {
    AdminListStoresRequest adminListStoresRequest = new AdminListStoresRequest();
    AdminListStoresModel adminListStoresResponse = tokenClient.adminListStores(adminListStoresRequest);

    System.out.println(new Gson().toJson(adminListStoresResponse.body));
    return adminListStoresResponse.body.items.get(0).storeId;
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    throw new Exception("failed to list stores");
  }
}

public static String createDrive() throws Exception {
  try {
    String storeId = adminListStores();
    CreateDriveRequest createDriveRequest = new CreateDriveRequest();
    createDriveRequest.totalSize = 100000L;
    createDriveRequest.driveName = "test_drive";
    createDriveRequest.driveType = "normal";
    createDriveRequest.owner = "superadmin";
    createDriveRequest.relativePath = "/hosting_test/";
    createDriveRequest.storeId = storeId;
    CreateDriveModel createDriveResponse = client.createDrive(createDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(createDriveResponse.body));
    return createDriveResponse.body.driveId;
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
    throw new Exception("failed to create drive");
  }
}
  • 返回结果

// list_stores response
{
    "items":[
        {
            "accelerateEndpoint":"",
            "basePath":"",
            "bucket":"ccp-daily-test",
            "customizedEndpoint":"",
            "endpoint":"https://oss-cn-hangzhou.aliyuncs.com",
            "internalEndpoint":"",
            "ownership":"custom",
            "policy":"",
            "roleArn":"",
            "storeId":"90fba27e9c40452d91d83b204aee1d9b",
            "type":"oss"
        }
    ]
}

// create drive response
{
    "requestId":"E72DDD97-E99F-437D-B0B8-7C752C4E9548",
    "domainId":"daily1405",
    "driveId":"1902"
}

列举drive

  • 以下代码用于列举Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void listDrive() throws Exception {
  try {
    ListDriveRequest listDriveRequest = new ListDriveRequest();
    listDriveRequest.limit = 1;
    listDriveRequest.owner = "";
    ListDrivesModel listDriveResponse = client.listDrives(listDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(listDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "items":[
        {
            "creator":"6c23c*************211",
            "description":"drive",
            "domainId":"daily1405",
            "driveId":"1902",
            "driveName":"test_drive",
            "driveType":"normal",
            "owner":"6c23c*************211",
            "relativePath":"/test_drive/",
            "status":"enabled",
            "storeId":"90fba27e9c40452d91d83b204aee1d9b",
            "totalSize":1000000,
            "usedSize":0
        }
    ],
    "nextMarker":""
}

查询drive

  • 以下代码用于查询Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void getDrive(String driveId) throws Exception {
  try {
    GetDriveRequest getDriveRequest = new GetDriveRequest();
    getDriveRequest.driveId = driveId;
    GetDriveModel getDriveResponse = client.getDrive(getDriveRequest);
    //打印结果
    System.out.println(new Gson().toJson(getDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "creator":"6c23c9*************f5211",
    "description":"test_drive",
    "domainId":"daily1405",
    "driveId":"1902",
    "driveName":"test_drive",
    "driveType":"normal",
    "owner":"6c23c9***************f5211",
    "relativePath":"/test_drive/",
    "status":"enabled",
    "storeId":"90fba27e9c40452d91d83b204aee1d9b",
    "totalSize":1000000,
    "usedSize":0
}

列举drive

  • 以下代码用于更新Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void listDrive() throws Exception {
  try {
    ListDriveRequest listDriveRequest = new ListDriveRequest();
    listDriveRequest.limit = 1;
    listDriveRequest.owner = "";
    ListDrivesModel listDriveResponse = client.listDrives(listDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(listDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "items":[
        {
            "creator":"",
            "description":"",
            "domainId":"daily1405",
            "driveId":"603",
            "driveName":"test_drive",
            "driveType":"normal",
            "owner":"****",
            "relativePath":"/test_drive/",
            "status":"enabled",
            "storeId":"55ff********904",
            "totalSize":100000,
            "usedSize":0
        }
    ],
    "nextMarker":""
}

更新drive

  • 以下代码用于更新Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void updateDrive(String driveId) throws Exception {
  try {
    UpdateDriveRequest updateDriveRequest = new UpdateDriveRequest();
    updateDriveRequest.driveId = driveId;
    updateDriveRequest.description = "changed_drive";
    updateDriveRequest.totalSize = 1000000L;
    UpdateDriveModel updateDriveResponse = client.updateDrive(updateDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(updateDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println();
  }
}
  • 返回结果

{
    "requestId":"D88BC954-0BE8-4282-BDF3-6F964FC113DF",
    "creator":"6c23c**************9f5211",
    "description":"changed_drive",
    "domainId":"daily1405",
    "driveId":"1902",
    "driveName":"test_drive",
    "driveType":"normal",
    "owner":"6c23c9************5211",
    "relativePath":"/test_drive/",
    "status":"enabled",
    "storeId":"90fba27e9c40452d91d83b204aee1d9b",
    "totalSize":1000000,
    "usedSize":0
}

删除drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void deleteDrive(String driveId) throws Exception {
  try {
    DeleteDriveRequest deleteDriveRequest = new DeleteDriveRequest();
    deleteDriveRequest.driveId = driveId;

    DeleteDriveModel deleteDriveResponse = client.deleteDrive(deleteDriveRequest);
    // 此接口不返回body
    System.out.println(new Gson().toJson(deleteDriveResponse.headers));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}

列举Stores

​ 说明:当前接口只支持accessToken调用, 请使用Accesstoken初始化客户端

  • 以下代码用于列举Stores,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void listStores() throws Exception {
    try {
        AdminListStoresRequest listStoresRequest = new AdminListStoresRequest();

        AdminListStoresModel listStoresResponse = client.adminListStores(listStoresRequest);
        System.out.println(new Gson().toJson(listStoresResponse.body));
    } catch (TeaException e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCode());
        System.out.println(new Gson().toJson(e.getData()));
    }
}
  • 返回结果

{
    "items":[
        {
            "accelerateEndpoint":"",
            "basePath":"",
            "bucket":"ccp-daily-test",
            "customizedEndpoint":"",
            "endpoint":"https://oss-cn-hangzhou.aliyuncs.com",
            "internalEndpoint":"",
            "ownership":"custom",
            "policy":"",
            "roleArn":"",
            "storeId":"90fba27e9c40452d91d83b204aee1d9b",
            "type":"oss"
        }
    ]
}

HostingMode Share 相关 API

创建Share

  • 以下代码用于创建Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static String getISOTime() {
  long currentTime = System.currentTimeMillis();
  currentTime += 30 * 60 * 1000;
  Date date = new Date(currentTime);
  TimeZone timeZone = TimeZone.getTimeZone("UTC");
  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  dateFormat.setTimeZone(timeZone);

  return dateFormat.format(date);
}

public static String createShare() throws Exception {
  try {
    String nowAsISO = getISOTime();
    CreateShareRequest createShareRequest = new CreateShareRequest();
    createShareRequest.shareFilePath = "/ccp_sdk/";
    createShareRequest.owner = "superadmin";
    createShareRequest.shareName = "test_share";
    createShareRequest.permissions = Arrays.asList("FILE.LIST");
    createShareRequest.expiration = nowAsISO;
    createShareRequest.driveId = "1";
    CreateShareModel createShareResponse = tokenClient.createShare(createShareRequest);

    // 打印结果
    System.out.println(new Gson().toJson(createShareResponse.body));
    return createShareResponse.body.shareId;
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
    throw new Exception("failed to create share");
  }
}
  • 返回结果

{
    "domainId":"daily1405",
    "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7"
}

列举Share

  • 以下代码用于列举Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 列举 Share
public static void listShare() throws Exception {
  try {
    ListShareRequest listShareRequest = new ListShareRequest();
    listShareRequest.owner = "superadmin";
    ListShareModel listShareResponse = tokenClient.listShare(listShareRequest);

    // 打印结果
    System.out.println(new Gson().toJson(listShareResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "items":[
        {
            "createdAt":"2019-10-28T06:22:21.163Z",
            "creator":"6c23c*************9f5211",
            "description":"changed_share",
            "domainId":"daily1405",
            "driveId":"1902",
            "expiration":"2019-10-28T06:52:21.139Z",
            "expired":false,
            "owner":"6c23c*****************3f9f5211",
            "permissions":[

            ],
            "shareFilePath":"/test_share/",
            "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
            "shareName":"test_share",
            "status":"enabled",
            "updatedAt":"2019-10-28T06:22:21.163Z"
        }
    ],
    "nextMarker":""
}

查询Share

  • 以下代码用于查询Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 查询Share
public static void getShare(String shareID) throws Exception {
  try {
    GetShareRequest getShareRequest = new GetShareRequest();
    getShareRequest.shareId = shareID;
    GetShareModel getShareResponse = tokenClient.getShare(getShareRequest);


    // 打印结果
    System.out.println(new Gson().toJson(getShareResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "createdAt":"2019-10-28T06:22:21.163Z",
    "creator":"6c23*************9f5211",
    "description":"",
    "domainId":"daily1405",
    "driveId":"1902",
    "expiration":"2019-10-28T06:52:21.139Z",
    "expired":false,
    "owner":"6c23c*************9f5211",
    "permissions":[
        "FILE.LIST"
    ],
    "shareFilePath":"/test_share/",
    "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
    "shareName":"test_share",
    "status":"enabled",
    "updatedAt":"2019-10-28T06:22:21.163Z"
}

更新Share

  • 以下代码用于更新Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 更新 Share
public static void updateShare(String shareID) throws Exception {
  try {
    UpdateShareRequest updateShareRequest = new UpdateShareRequest();
    updateShareRequest.shareId = shareID;
    updateShareRequest.description = "changed_share";
    UpdateShareModel updateShareResponse = tokenClient.updateShare(updateShareRequest);

    // 打印结果
    System.out.println(new Gson().toJson(updateShareResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "createdAt":"2019-10-28T06:22:21.163Z",
    "creator":"6c23c98********************f5211",
    "description":"changed_share",
    "domainId":"daily1405",
    "driveId":"1902",
    "expiration":"2019-10-28T06:52:21.139Z",
    "expired":false,
    "owner":"6c23c98********************f5211",
    "permissions":[
        "FILE.LIST"
    ],
    "shareFilePath":"/test_share/",
    "shareId":"47057f7b-8182-4d2d-ba1a-09ede78782d7",
    "shareName":"test_share",
    "status":"enabled",
    "updatedAt":"2019-10-28T06:22:21.163Z"
}

删除Share

  • 以下代码用于删除Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 删除 Share
public static void deleteShare(String shareID) throws Exception {
  try {
    DeleteShareRequest deleteShareRequest = new DeleteShareRequest();
    deleteShareRequest.shareId = shareID;

    DeleteShareModel deleteShareResponses = tokenClient.deleteShare(deleteShareRequest);

    //此接口不返回body
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}

HostingMode File相关 API

创建File

  • 以下代码用于创建File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

  • 说明: 此处创建File 还需要上传文件到OSS, 具体操作详见最佳实践。

public static void createFile() throws Exception {
  try {
    HostingCreateFileRequest CreateFileRequest = new HostingCreateFileRequest();
    CreateFileRequest.type = "folder";
    CreateFileRequest.name = "test_folder";
    CreateFileRequest.driveId = "1";
    CreateFileRequest.parentFilePath = "/";
    CreateFileModel CreateFileResponse = client.createFile(CreateFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CreateFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

// 此处url省略部分,实际值详见接口调用返回的结果。
{
    "domainId":"daily1405",
    "driveId":"1902",
    "filePath":"/a.txt",
    "partInfoList":[
        {
            "partNumber":1,
            "uploadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
        }
    ],
    "type":"file",
    "uploadId":"872F52602EB343D0ADCE3E75D008FB16"
}

列举File

  • 以下代码用于列举File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void listFile() throws Exception {
  try {
    HostingListFileRequest ListFileRequest = new HostingListFileRequest();
    ListFileRequest.driveId = "1";
    ListFileRequest.parentFilePath = "/";
    ListFileRequest.limit = 1L;
    ListFileModel ListFileResponse = client.listFile(ListFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(ListFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

// 此处url省略部分,实际值详见接口调用返回的结果。
{
    "items":[
        {
            "contentType":"",
            "domainId":"daily1405",
            "downloadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16",
            "driveId":"1902",
            "fileExtension":"txt",
            "filePath":"/test_folder/a.txt",
            "name":"a.txt",
            "parentFilePath":"/test_folder/",
            "size":8,
            "status":"available",
            "type":"file",
            "updatedAt":"2019-10-28T06:22:23Z",
            "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
        }
    ],
    "nextMarker":""
}

查询File

  • 以下代码用于查询File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void getFile() throws Exception {
  try {
    HostingGetFileRequest GetFileRequest = new HostingGetFileRequest();
    GetFileRequest.driveId = "1";
    GetFileRequest.filePath = "/java.zip";
    GetFileModel GetFileResponse = client.getFile(GetFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "contentType":"text/plain",
    "domainId":"daily1405",
    "downloadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16",
    "driveId":"1902",
    "fileExtension":"",
    "filePath":"/a.txt",
    "name":"a.txt",
    "parentFilePath":"/",
    "size":8,
    "status":"available",
    "type":"file",
    "updatedAt":"2019-10-28T06:22:23Z",
    "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
}

移动File

  • 以下代码用于移动File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void moveFile() throws Exception {
  try {
    HostingMoveFileRequest MoveFileRequest = new HostingMoveFileRequest();
    MoveFileRequest.driveId = "1";
    MoveFileRequest.filePath = "/java.zip";
    MoveFileRequest.toParentFilePath = "/ccp_sdk/";

    MoveFileModel MoveFileResponse = client.moveFile(MoveFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(MoveFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "domainId":"daily1405",
    "driveId":"1902",
    "filePath":"/test_folder/a.txt"
}

复制File

  • 以下代码用于复制File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void copyFile() throws Exception {
  try {
    HostingCopyFileRequest CopyFileRequest = new HostingCopyFileRequest();
    CopyFileRequest.filePath = "/ccp_sdk/java.zip";
    CopyFileRequest.driveId = "1";
    CopyFileRequest.toParentFilePath = "/";
    CopyFileModel CopyFileResponse = client.copyFile(CopyFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CopyFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "domainId":"daily1405",
    "driveId":"1902",
    "filePath":"/a.txt"
}

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void getFileUploadUrl() throws Exception {
  try {
    UploadPartInfo uploadPartInfo = new UploadPartInfo();
    uploadPartInfo.partNumber = 1L;
    HostingGetUploadUrlRequest GetUploadUrlRequest = new HostingGetUploadUrlRequest();
    GetUploadUrlRequest.driveId = "";
    GetUploadUrlRequest.filePath = "";
    GetUploadUrlRequest.uploadId = "";
    GetUploadUrlRequest.partInfoList = new ArrayList<UploadPartInfo>();
    GetUploadUrlRequest.partInfoList.add(uploadPartInfo);
    GetUploadUrlModel GetUploadUrlResponse = client.getUploadUrl(GetUploadUrlRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetUploadUrlResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "createAt":"2019-10-28T06:22:22.231Z",
    "domainId":"daily1405",
    "driveId":"1902",
    "filePath":"/a.txt",
    "partInfoList":[
        {
            "partNumber":1,
            "uploadUrl":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
        }
    ],
    "uploadId":"872F52602EB343D0ADCE3E75D008FB16"
}

获取File下载地址

  • 以下代码用于获取File下载地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 获取File下载地址
public static void getFileDownloadUrl() throws Exception {
  try {
    HostingGetDownloadUrlRequest GetDownloadUrlRequest = new HostingGetDownloadUrlRequest();
    GetDownloadUrlRequest.driveId = "1";
    GetDownloadUrlRequest.filePath = "5f3f2abeb28ec23886d844e397e75fa30f5339af";
    GetDownloadUrlRequest.expireSec = 3600L;
    GetDownloadUrlModel GetDownloadUrlResponse = client.getDownloadUrl(GetDownloadUrlRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetDownloadUrlResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "requestId":"6760BC8F-AA33-4862-AA26-0F8FA57EAF39",
    "expiration":"2019-10-28T07:22:24.044Z",
    "method":"GET",
    "url":"https://********.oss-cn-hangzhou.aliyuncs.***********8FB16"
}

Complete File

  • 以下代码用于创建Complete File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

  • 说明: 此处请求参数中的ETag是请求uploadUrl上传完成后从返回的Response Headers中获取的,详见最佳实践。

public static void completeFile() throws Exception {
  try {
    HostingCompleteFileRequest CompleteFileRequest = new HostingCompleteFileRequest();
    UploadPartInfo uploadPartInfo = new UploadPartInfo();
    CompleteFileRequest.driveId = "";
    CompleteFileRequest.filePath = "";
    CompleteFileRequest.uploadId = "";
    CompleteFileRequest.partInfoList = new ArrayList<UploadPartInfo>(Arrays.asList(uploadPartInfo));
    CompleteFileModel CompleteFileResponse = client.completeFile(CompleteFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CompleteFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "contentType":"text/plain",
    "crc64Hash":"6668564720710875145",
    "domainId":"daily1405",
    "driveId":"1902",
    "fileExtension":"",
    "filePath":"/a.txt",
    "name":"a.txt",
    "parentFilePath":"/",
    "size":0,
    "status":"uploading",
    "type":"file",
    "uploadId":"872F52602EB343D0ADCE3E75D008FB16",
    "crc":"6668564720710875145"
}

删除File

  • 以下代码用于删除File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void deleteFile() throws Exception {
  try {
    HostingDeleteFileRequest DeleteFileRequest = new HostingDeleteFileRequest();
    DeleteFileRequest.driveId = "1";
    DeleteFileRequest.filePath = "/ccp_sdk/java.zip";
    DeleteFileModel deleteFileResponse = client.deleteFile(DeleteFileRequest);

    System.out.println(new Gson().toJson(deleteFileResponse.headers));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
}

列举Stores File

  • 以下代码用于列举Stores File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档.

public static void listStoreFiles() throws Exception {
  try {
    String storeId = adminListStores();
    ListStoreFileRequest listStoreFileRequest = new ListStoreFileRequest();
    listStoreFileRequest.setStoreId(storeId);
    listStoreFileRequest.setLimit(10L);
    listStoreFileRequest.setParentFilePath("/");

    ListStorefileModel listStoreFileResponse = tokenClient.listStorefile(listStoreFileRequest);
    System.out.println(new Gson().toJson(listStoreFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    throw new Exception("failed to list store files");
  }
}
  • 返回结果

{
    "items":[
        {
            "domainId":"",
            "name":"5733dbd6**********5c0",
            "parentFilePath":"/",
            "storeId":"90fba2***********e1d9b",
            "type":"folder"
        }
    ],
    "nextMarker":"5733db************86e75c0/"
}

StandardMode Drive 相关API

创建drive

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 创建 Drive
public static void createDrive() throws Exception {
  try {
    CreateDriveRequest createDriveRequest = new CreateDriveRequest();
    createDriveRequest.totalSize = 100000L;
    createDriveRequest.driveName = "test_drive";
    createDriveRequest.driveType = "normal";
    createDriveRequest.owner = "superadmin";
    CreateDriveModel createDriveResponse = client.createDrive(createDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(createDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "domainId":"daily1404",
    "driveId":"603"
}

列举drive

  • 以下代码用于列举drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 列举 Drive 
public static void listDrive() throws Exception {
  try {
    ListDriveRequest listDriveRequest = new ListDriveRequest();
    listDriveRequest.limit = 1;
    listDriveRequest.owner = "";
    ListDrivesModel listDriveResponse = client.listDrives(listDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(listDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
  "items":[
    {
      "creator":"",
      "description":"",
      "domainId":"daily1404",
      "driveId":"603",
      "driveName":"test_drive",
      "driveType":"normal",
      "owner":"ldh",
      "relativePath":"",
      "status":"enabled",
      "storeId":"55ff60f575b24a8c97378f1e0a946904",
      "totalSize":100000,
      "usedSize":0
    }
  ],
    "nextMarker":""
}

查询drive

  • 以下代码用于查询drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 查询 Drive
public static void getDrive() throws Exception {
  try {
    GetDriveRequest getDriveRequest = new GetDriveRequest();
    getDriveRequest.driveId = "1";
    GetDriveModel getDriveResponse = client.getDrive(getDriveRequest);
    //打印结果
    System.out.println(new Gson().toJson(getDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "creator":"",
    "description":"",
    "domainId":"daily1404",
    "driveId":"603",
    "driveName":"test_drive",
    "driveType":"normal",
    "owner":"ldh",
    "relativePath":"",
    "status":"enabled",
    "storeId":"55ff60f575b24a8c97378f1e0a946904",
    "totalSize":100000,
    "usedSize":0
}

更新drive

  • 以下代码用于更新drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 更新 Drive
public static void updateDrive() throws Exception {
  try {
    UpdateDriveRequest updateDriveRequest = new UpdateDriveRequest();
    updateDriveRequest.driveId = "401";
    updateDriveRequest.description = "changed_drive";
    updateDriveRequest.totalSize = 1000000L;
    UpdateDriveModel updateDriveResponse = client.updateDrive(updateDriveRequest);

    // 打印结果
    System.out.println(new Gson().toJson(updateDriveResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}
  • 返回结果

{
    "creator":"",
    "description":"changed_drive",
    "domainId":"daily1404",
    "driveId":"603",
    "driveName":"test_drive",
    "driveType":"normal",
    "owner":"ldh",
    "relativePath":"",
    "status":"enabled",
    "storeId":"55ff60f575b24a8c97378f1e0a946904",
    "totalSize":1000000,
    "usedSize":0
}

删除drive

  • 以下代码用于删除drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 删除 Drive
public static void deleteDrive() throws Exception {
  try {
    DeleteDriveRequest deleteDriveRequest = new DeleteDriveRequest();
    deleteDriveRequest.driveId = "";

    DeleteDriveModel deleteDriveResponse = client.deleteDrive(deleteDriveRequest);

    // 此接口不返回body
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
}

StandardMode File 相关 API

创建File

  • 以下代码用于创建Drive,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 创建文件
public static void createFile() throws Exception {
  try {
    CreateFileRequest CreateFileRequest = new CreateFileRequest();
    CreateFileRequest.type = "folder";
    CreateFileRequest.name = "test_folder";
    CreateFileRequest.driveId = "1";
    CreateFileRequest.parentFileId = "root";
    CreateFileModel CreateFileResponse = client.createFile(CreateFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CreateFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "domainId":"daily1404",
    "driveId":"603",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
    "parentFileId":"root",
    "partInfoList":[
        {
            "partNumber":1,
            "uploadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
        }
    ],
    "rapidUpload":false,
    "type":"file",
    "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
}

列举File

  • 以下代码用于列举File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 列举文件
public static void listFile() throws Exception {
  try {
    ListFileRequest ListFileRequest = new ListFileRequest();
    ListFileRequest.driveId = "1";
    ListFileRequest.parentFileId = "root";
    ListFileRequest.limit = 1L;
    ListFileModel ListFileResponse = client.listFile(ListFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(ListFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "items":[
        {
            "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
            "contentHashName":"sha1",
            "contentType":"",
            "createdAt":"2019-10-28T08:40:54.398Z",
            "domainId":"daily1404",
            "downloadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****.txt",
            "driveId":"603",
            "fileExtension":"txt",
            "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
            "hidden":false,
            "name":"a.txt",
            "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
            "size":6,
            "starred":false,
            "status":"available",
            "type":"file",
            "updatedAt":"2019-10-28T08:40:55.398Z",
            "url":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
        }
    ],
    "nextMarker":""
}

查询File

  • 以下代码用于查询File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 查询File
public static void getFile() throws Exception {
  try {
    GetFileRequest GetFileRequest = new GetFileRequest();
    GetFileRequest.driveId = "1";
    GetFileRequest.fileId = "5f3b54672d4cd1b97fcb4e658385061b98d5863f";
    GetFileModel GetFileResponse = client.getFile(GetFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
    "contentHashName":"sha1",
    "contentType":"text/plain",
    "crc64Hash":"318318745347147982",
    "createdAt":"2019-10-28T08:40:54.398Z",
    "domainId":"daily1404",
    "downloadUrl":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943.txt",
    "driveId":"603",
    "fileExtension":"txt",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
    "hidden":false,
    "name":"a.txt",
    "parentFileId":"root",
    "size":6,
    "starred":false,
    "status":"available",
    "type":"file",
    "updatedAt":"2019-10-28T08:40:55.398Z",
    "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
    "url":"https://*********.oss-cn-hangzhou.aliyuncs.com/*****F943"
}

移动File

  • 以下代码用于移动File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 移动 File
public static void moveFile() throws Exception {
  try {
    MoveFileRequest MoveFileRequest = new MoveFileRequest();
    MoveFileRequest.driveId = "1";
    MoveFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
    MoveFileRequest.toParentFileId = "5f3b54672d4cd1b97fcb4e658385061b98d5863f";

    MoveFileModel MoveFileResponse = client.moveFile(MoveFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(MoveFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "domainId":"daily1404",
    "driveId":"603",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f"
}

复制File

  • 以下代码用于复制File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 复制 File
public static void copyFile() throws Exception {
  try {
    CopyFileRequest CopyFileRequest = new CopyFileRequest();
    CopyFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
    CopyFileRequest.driveId = "1";
    CopyFileRequest.toParentFileId = "root";
    CopyFileModel CopyFileResponse = client.copyFile(CopyFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CopyFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "domainId":"daily1404",
    "driveId":"603",
    "fileId":"5db6a997ce31eb635f2e4f4c9163ec3bd10af459"
}

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 获取File上传地址
public static void getFileUploadUrl() throws Exception {
  try {
    UploadPartInfo uploadPartInfo = new UploadPartInfo();
    uploadPartInfo.partNumber = 1L;
    GetUploadUrlRequest GetUploadUrlRequest = new GetUploadUrlRequest();
    GetUploadUrlRequest.driveId = "";
    GetUploadUrlRequest.fileId = "";
    GetUploadUrlRequest.uploadId = "";
    GetUploadUrlRequest.partInfoList = new ArrayList<UploadPartInfo>();
    GetUploadUrlRequest.partInfoList.add(uploadPartInfo);
    GetUploadUrlModel GetUploadUrlResponse = client.getUploadUrl(GetUploadUrlRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetUploadUrlResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "createAt":"2019-10-28T08:40:54.519Z",
    "domainId":"daily1404",
    "driveId":"603",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
    "partInfoList":[
        {
            "partNumber":1,
            "uploadUrl":"url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
        }
    ],
    "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943"
}

获取File下载地址

  • 以下代码用于获取File下载地,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 获取File下载地址
public static void getFileDownloadUrl() throws Exception {
  try {
    GetDownloadUrlRequest GetDownloadUrlRequest = new GetDownloadUrlRequest();
    GetDownloadUrlRequest.driveId = "1";
    GetDownloadUrlRequest.fileId = "5f3f2abeb28ec23886d844e397e75fa30f5339af";
    GetDownloadUrlRequest.expireSec = 3600L;
    GetDownloadUrlModel GetDownloadUrlResponse = client.getDownloadUrl(GetDownloadUrlRequest);

    // 打印结果
    System.out.println(new Gson().toJson(GetDownloadUrlResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "expiration":"2019-10-28T09:40:55.716Z",
    "method":"GET",
    "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
}

Complete File

  • 以下代码用于Complete File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 合并File分片
public static void completeFile() throws Exception {
  try {
    CompleteFileRequest CompleteFileRequest = new CompleteFileRequest();
    UploadPartInfo uploadPartInfo = new UploadPartInfo();
    CompleteFileRequest.driveId = "";
    CompleteFileRequest.fileId = "";
    CompleteFileRequest.uploadId = "";
    CompleteFileRequest.partInfoList = new ArrayList<UploadPartInfo>(Arrays.asList(uploadPartInfo));
    CompleteFileModel CompleteFileResponse = client.completeFile(CompleteFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(CompleteFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
    "contentHashName":"sha1",
    "contentType":"text/plain",
    "crc64Hash":"318318745347147982",
    "createdAt":"2019-10-28T08:40:54.398Z",
    "domainId":"daily1404",
    "driveId":"603",
    "fileExtension":"txt",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
    "hidden":false,
    "name":"a.txt",
    "parentFileId":"root",
    "size":6,
    "starred":false,
    "status":"available",
    "type":"file",
    "updatedAt":"2019-10-28T08:40:55.398Z",
    "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
    "crc":""
}

更新File

  • 以下代码用于更新File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 更新 file
public static void updateFile() throws Exception {
  try {
    UpdateFileMetaRequest UpdateFileMetaRequest = new UpdateFileMetaRequest();
    UpdateFileMetaRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
    UpdateFileMetaRequest.description = "changed_file";
    UpdateFileMetaRequest.driveId = "1";

    UpdateFileModel UpdateFileMetaResponse = client.updateFile(UpdateFileMetaRequest);

    // 打印结果
    System.out.println(new Gson().toJson(UpdateFileMetaResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
    "contentHashName":"sha1",
    "contentType":"text/plain",
    "crc64Hash":"318318745347147982",
    "createdAt":"2019-10-28T08:40:54.398Z",
    "description":"changed_file",
    "domainId":"daily1404",
    "downloadUrl":"https://******.oss-cn-hangzhou.aliyuncs.com/5****a.txt",
    "driveId":"603",
    "fileExtension":"txt",
    "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
    "hidden":false,
    "name":"a.txt",
    "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
    "size":6,
    "starred":false,
    "status":"available",
    "type":"file",
    "updatedAt":"2019-10-28T08:40:55.398Z",
    "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
    "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****wZM%3D"
}

搜索File

  • 以下代码用于搜索File,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 搜索 file
public static void searchFile() throws Exception {
  try {
    SearchFileRequest SearchFileRequest = new SearchFileRequest();
    SearchFileRequest.driveId = "1";
    SearchFileRequest.limit = 3;
    SearchFileRequest.orderBy = "type DESC";
    SearchFileRequest.query = "file_extension in [\"txt\"]";
    SearchFileModel SearchFileResponse = client.searchFile(SearchFileRequest);

    // 打印结果
    System.out.println(new Gson().toJson(SearchFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "items":[
        {
            "contentHash":"7C4A8D09CA3762AF61E59520943DC26494F8941B",
            "contentHashName":"sha1",
            "contentType":"text/plain",
            "crc64Hash":"318318745347147982",
            "createdAt":"2019-10-28T08:40:54.398Z",
            "description":"changed_file",
            "domainId":"daily1404",
            "downloadUrl":"https://************.oss-cn-hangzhou.aliyuncs.com/***a.txt",
            "driveId":"603",
            "fileExtension":"txt",
            "fileId":"5db6a996aa1292d7563644f0bc4847107171ed7f",
            "hidden":false,
            "name":"a.txt",
            "parentFileId":"5db6a996c7e9ae3a5e654a7798947b209989b963",
            "size":6,
            "starred":false,
            "status":"available",
            "type":"file",
            "updatedAt":"2019-10-28T08:40:55.398Z",
            "uploadId":"DEB65A38FCCA410BAC6DD23A8A11F943",
            "url":"https://****.oss-cn-hangzhou.aliyuncs.com/****ZM%3D"
        }
    ],
    "nextMarker":""
}

删除File

  • 以下代码用于删除file,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

// 删除 file
public static void deleteFile() throws Exception {
  try {
    DeleteFileRequest DeleteFileRequest = new DeleteFileRequest();
    DeleteFileRequest.driveId = "1";
    DeleteFileRequest.fileId = "5f3b578fdea43919eb3d4a3bac88b614a1754f52";
    DeleteFileModel deleteFileResponse = client.deleteFile(DeleteFileRequest);

    System.out.println(new Gson().toJson(deleteFileResponse.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getCode());
    System.out.println(new Gson().toJson(e.getData()));
  }
  catch (Exception e){
    System.out.println(e.getMessage());
  }
}

批量操作

  • 以下代码用于批量操作,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public static void batch() throws Exception {
  try {
    BatchRequest BatchRequest = new BatchRequest();
    BatchRequest.resource = "file";
    BatchSubRequest batchSubRequest = new BatchSubRequest();
    Map<String, Object> bodyMap = new HashMap<String, Object>();
    bodyMap.put("drive_id", "1");
    bodyMap.put("file_id", "5f3b578fdea43919eb3d4a3bac88b614a1754f52");
    batchSubRequest.body = bodyMap;
    batchSubRequest.url = "/file/get";
    batchSubRequest.method = "POST";
    batchSubRequest.id = "uuid";
    BatchRequest.requests = new ArrayList<BatchSubRequest>(Arrays.asList(batchSubRequest));
    BatchOperationModel response = client.batchOperation(BatchRequest);
    System.out.println(new Gson().toJson(response.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
    "responses":[
        {
            "status":200,
            "body":{
                "file_extension":"txt",
                "updated_at":"2020-03-30T08:15:58.267Z",
                "content_hash":"FAA12FD40AAC1F492082C90C2CD6C03B9ABDB826",
                "domain_id":"hz22",
                "size":36,
                "category":"doc",
                "content_hash_name":"sha1",
                "download_url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
                "crc64_hash":"13138712399852734283",
                "drive_id":"1",
                "hidden":false,
                "type":"file",
                "parent_file_id":"root",
                "status":"available",
                "description":"changed_file",
                "encrypt_mode":"none",
                "file_id":"5e81aabdae****d9836b36",
                "content_type":"application/oct-stream",
                "name":"testDJw8oWE6ef9464f66e5034f69aafd57cb2879170b.txt",
                "url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
                "created_at":"2020-03-30T08:15:57.361Z",
                "upload_id":"DFD1DA39317F45EC8534FEA57AE420D1",
                "starred":false
            },
            "id":"624830fa2c2347be8c0d8afc76faa04d"
        }
    ]
}

获取file已经上传的分片

  • 以下代码用于获取file已经上传的分片,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void listUploadedPart() throws Exception {
  try {
    ListUploadedPartRequest ListUploadedPartRequest = new ListUploadedPartRequest();
    ListUploadedPartRequest.driveId = "1";
    ListUploadedPartRequest.fileId = "****";
    ListUploadedPartRequest.uploadId = "****";
    ListUploadedPartsModel response = client.listUploadedParts(ListUploadedPartRequest);
    System.out.println(new Gson().toJson(response.body));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
  "drive_id" : "1",
  "file_id" : "5d5b846942cf94fa72324c14a4bda34e81da635d",
  "limit" : 1,
  "part_number_marker" : 1,
  "upload_id" : "00668396C0814D818D90F0A92B04B355"
}

获取异步信息

  • 以下代码用于获取异步信息,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。

public void getAsyncTask() throws Exception {
  try {
    GetAsyncTaskRequest getAsyncTaskRequest = new GetAsyncTaskRequest();
    getAsyncTaskRequest.asyncTaskId = "***";
    GetAsyncTaskInfoModel response = client.getAsyncTaskInfo(getAsyncTaskRequest);
    System.out.println(new Gson().toJson(response.headers));
  } catch (TeaException e) {
    System.out.println(e.getMessage());
  }
}
  • 返回结果

{
  "async_task_id" : "000e89fb-cf8f-11e9-8ab4-b6e980803a3b",
  "message" : "task is running",
  "state" : "success"
}

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

  1. 注册阿里云账号,详见阿里云账号注册流程

  2. 开启访问控制服务,详见RAM访问控制,并根据提示操作。

  3. 创建RAM子用户,并获取AK,SK。详见为RAM用户创建访问密钥。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

import com.aliyun.pds.client.Client;
import com.aliyun.pds.client.models.*;
import com.aliyun.tea.TeaException;
import com.google.gson.Gson;
import okhttp3.*;

import java.util.*;


public class Demo {
  private static Client client;
  private static RuntimeOptions runtime;

  public static void main(String[] args) throws  Exception{
    createClient();
    createFile();
  }

  public static  void createClient() throws Exception {
    Config config = new Config();
    config.domainId= "your domain id";
    config.protocol = "https";
    config.accessKeyId = "your accessKeyId";
    config.accessKeySecret = "your accessKeySecret";
    client = new Client(config);
    runtime = new RuntimeOptions();
  }

  public static  void createFullFile() throws Exception{
        try{
            System.out.println("-------------create file----------------");
            CreateFileRequest CreateFileRequest = new CreateFileRequest();
            CreateFileRequest.type = "file";
            CreateFileRequest.name = "a.txt";
            CreateFileRequest.driveId = "1";
            CreateFileRequest.parentFileId = "root";
            CreateFileRequest.contentType = "text/plain";
            CreateFileModel CreateFileResponse = client.createFile(CreateFileRequest);
            System.out.println(new Gson().toJson(CreateFileResponse.body));

            String uploadId = CreateFileResponse.body.uploadId;
            String fileId = CreateFileResponse.body.fileId;
            String uploadUrl = CreateFileResponse.body.partInfoList.get(0).uploadUrl;


            // upload file
            System.out.println("-------------upload file----------------");
            Request.Builder requestBuilder = new Request.Builder();
            RequestBody body = RequestBody.create(MediaType.parse(""), "123456");
            requestBuilder.url(uploadUrl);
            requestBuilder.put(body);

            Request request = requestBuilder.build();
            OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
            Response response = okHttpClient.newCall(request).execute();

            String etag = response.headers().get("ETag");
            System.out.println();

            // complete file
            System.out.println("-------------complete file----------------");
            UploadPartInfo uploadPartInfo1 = new UploadPartInfo();
            uploadPartInfo1.etag = etag;
            uploadPartInfo1.partNumber = 1L;
            CompleteFileRequest CompleteFileRequest = new CompleteFileRequest();
            CompleteFileRequest.driveId = "1";
            CompleteFileRequest.fileId = fileId;
            CompleteFileRequest.uploadId = uploadId;
            CompleteFileRequest.partInfoList = new ArrayList<UploadPartInfo>(Arrays.asList(uploadPartInfo1));
            CompleteFileModel CompleteFileResponse = client.completeFile(CompleteFileRequest);
            System.out.println(new Gson().toJson(CompleteFileResponse.body));

        }catch (TeaException e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCode());
            System.out.println(new Gson().toJson(e.getData()));
        }
    }
}
阿里云首页 网盘与相册服务 相关技术圈