推特 阿里云技术文档正文

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

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

Golang SDK 简介

下载地址

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

安装步骤

安装Go开发环境

您可以从 golang官方网站 下载并按说明安装 golang 开发环境。

安装CCP Go SDK

安装完 Go开发环境后,您需要将下面的依赖加入go.mod

CCP Path

  1. module github.com/aliyun/aliyun-ccp/ccppath-sdk/go
  2. go 1.12
  3. require (
  4. github.com/alibabacloud-go/tea v0.0.0-20191011054747-c132d91088dc
  5. github.com/aliyun/aliyun-ccp/baseclient/go master
  6. )

OSS Path

  1. module github.com/aliyun/aliyun-ccp/osspath-sdk/go
  2. go 1.12
  3. require (
  4. github.com/alibabacloud-go/tea v0.0.0-20191011054747-c132d91088dc
  5. github.com/aliyun/aliyun-ccp/baseclient/go master
  6. )

初始化Client

AK & SK 初始化客户端

  1. var ossConfig = new(client.Config).
  2. SetDomainId("your domain id").
  3. SetProtocol("https").
  4. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  5. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  6. // initial runtimeOptions
  7. var runtime = &client.RuntimeOptions{}
  8. // initial akClient
  9. var ossClient, _ = client.NewClient(ossConfig)

ClientID & Client Secret 初始化客户端

  1. var accessTokenConfig = new(client.Config).
  2. SetDomainId("your domain id").
  3. SetProtocol("https").
  4. SetClientId(os.Getenv("CLIENT_ID")).
  5. SetClientSecret(os.Getenv("CLIENT_SECRET")).
  6. SetAccessToken("ejy***************1asf").
  7. SetRefreshToken("O00*********MHb").
  8. SetExpireTime("2019-11-05T03:29:09Z")
  9. // initial runtimeOptions
  10. var runtime = &client.RuntimeOptions{}
  11. // initial tokenClient
  12. var tokenClient, _ = client.NewClient(accessTokenConfig)

构造请求

Account相关API

说明

客户端需要初始化如下

  1. // {domainId} 替换成自己的domainId
  2. var authConfig = new(client.Config).
  3. SetEndpoint("{ domainId }.auth.alicloudccp.com").
  4. SetProtocol("https").
  5. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  6. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  7. // initial runtimeOptions
  8. var runtime = &client.RuntimeOptions{}
  9. // initial authClient
  10. var authClient, _ = client.NewClient(authConfig)

获取图片验证码

  • 以下代码用于获取图片验证码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取图片验证码
  2. func getCaptcha() {
  3. getCaptchaRequest := new(client.GetCaptchaRequest).
  4. SetAppId(appId)
  5. response, err := authClient.GetCaptcha(getCaptchaRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. return
  9. }
  10. fmt.Println(response)
  11. }
  • 返回结果
  1. {
  2. "captcha": "iVBORw0KGgoAAAANSUhEUgAAAFAAAAAaCAIAAACvsEzwAAABQ0lEQVR42u3Yyw3CMAwA0Nw4IjZhAW7cYQQGQEKVGJDNSqRKUfAvjp1CWlr5QtWGPmInLmH8syNs4A28MvDwfGxg+ri8riDk64+3cx4dpbTSjMExuHuBtom5STJWg7E/3p4CgNODTh/zK2OcDncQXYPxmZwNppScYQzmzPthFwM8ajppWaU15iI4HwekMZnPQFgFtmldYG7pwmaugA3gaXyz9mMfLprJRUsotiqwXMO50KP1got7EpneXA0Lo/0SzFUv1oIBsVm/SiekUwtbS9lM1rAfrK9hv9YFFswph7lFmxNy5rlmuHZb0piFdcsAblnDBvA0gUqzclsqgp1m7+uhskvDvaet0/KbG7wP683F1lKzLbVpLZ2tudmsPDibwRxw4pnNi/gvgdiWnOzlgZ0zVnvXl3+jIH/9+syh55yc42HeYJ3SI2GdJBoAAAAASUVORK5CYII=",
  3. "captcha_format": "png",
  4. "captcha_id": "ff5db7ecd9988915ee56bebb52ed055aqSD2Cqce646"
  5. }

获取短信验证码

  • 以下代码用于获取短信验证码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取短信验证码
  2. func getMnsCode() {
  3. getMnsCodeRequest := new(client.MobileSendSmsCodeRequest).
  4. SetAppId(appId).
  5. SetPhoneNumber("1****5").
  6. SetType("login")
  7. response, err := authClient.MobileSendSmsCode(getMnsCodeRequest, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. return
  11. }
  12. fmt.Println(response)
  13. }
  • 返回结果
  1. {
  2. "requestId": "4CF58428-912F-4F4F-BE1D-4C06FE559097",
  3. "sms_code_id": "16340***************Slzf"
  4. }

验证手机号是否注册

  • 以下代码用于验证手机号是否注册,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func checkMobileIsExist() {
  2. checkMobileIsExistRequest := new(client.MobileCheckExistRequest).
  3. SetAppId(appId).
  4. SetPhoneNumber("13297039225")
  5. response, err := authClient.CheckExist(checkMobileIsExistRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. return
  9. }
  10. fmt.Println(response)
  11. }
  • 返回结果
  1. {
  2. "requestId": "0BF59873-1433-4D62-A465-93B7250BD698",
  3. "is_exist": true,
  4. "phone_number": "132*****225"
  5. }

手机号注册

  • 以下代码用于手机号注册,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 手机号注册
  2. func register() {
  3. registerRequest := new(client.MobileRegisterRequest).
  4. SetAppId(appId).
  5. SetPhoneNumber("132*****25").
  6. SetSmsCodeId("************").
  7. SetSmsCode("********")
  8. response, err := authClient.Register(registerRequest, runtime)
  9. if err != nil {
  10. fmt.Println(err.Error())
  11. return
  12. }
  13. fmt.Println(response)
  14. }
  • 返回结果
  1. {
  2. "requestId": "525FCA71-4C4A-4C35-AC72-24A4DC181646",
  3. "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX********P0ER7E-ASrJI",
  4. "avatar": null,
  5. "default_drive_id": null,
  6. "exist_link": [],
  7. "expire_time": "2019-11-04T11:50:06Z",
  8. "expires_in": 7200,
  9. "need_link": false,
  10. "nick_name": "132***225",
  11. "refresh_token": "A2lpoPv********1XRhl78",
  12. "role": "admin",
  13. "state": null,
  14. "token_type": "Bearer",
  15. "user_id": "6c23c98d36dc4b44b56b7d8b3f9f5211",
  16. "user_name": "132***225"
  17. }

手机号短信登录

  • 以下代码用于手机号短信登录,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func mnsCodeLogin() {
  2. mnsCodeLoginRequest := new(client.MobileLoginRequest).
  3. SetAppId(appId).
  4. SetSmsCode("****").
  5. SetSmsCodeId("******").
  6. SetPhoneNumber("132********25")
  7. response, err := authClient.Login(mnsCodeLoginRequest, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. }
  11. fmt.Println(response)
  12. }

设置登录密码

  • 以下代码用于设置登录密码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func setPassword() {
  2. setPasswordRequest := new(client.DefaultSetPasswordRequest).
  3. SetAppId(appId).
  4. SetNewPassword("").
  5. SetState("") //state 注册的时候返回
  6. err := authClient.SetPassword(setPasswordRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. }

密码登录

  • 以下代码用于密码登录,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func passwordLogin() {
  2. passwordLoginRequest := new(client.MobileLoginRequest).
  3. SetAppId(appId).
  4. SetPhoneNumber("132******5").
  5. SetPassword("********")
  6. response, err := authClient.Login(passwordLoginRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(response)
  11. }

修改登录密码

  • 以下代码用于修改登录密码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func changePassword() {
  2. changePasswordRequest := new(client.DefaultChangePasswordRequest).
  3. SetAppId(appId).
  4. SetPhoneNumber("132****225").
  5. SetNewPassword("***********").
  6. SetSmsCodeId("").
  7. SetSmsCode("")
  8. err := authClient.ChangePassword(changePasswordRequest, runtime)
  9. if err != nil {
  10. fmt.Println(err.Error())
  11. }
  12. // 此接口不返回Body
  13. }

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

  • 以下代码用于通过刷新令牌获取访问令牌,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getTokenByRefreshToken() {
  2. getTokenRequest := new(client.TokenRequest).
  3. SetAppId(appId).
  4. SetGrantType("refresh_token").
  5. SetRefreshToken("")
  6. response, err := authClient.Token(getTokenRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(response)
  11. }

通过账号获取访问令牌

此接口需要ak,sk调用, 请使用ak, sk初始化客户端

  • 以下代码用于通过账号获取访问令牌,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getAccessTokenByLinkInfo() {
  2. getTokenByLinkInfoReq := new(client.GetAccessTokenByLinkInfoRequest).
  3. SetType("mobile").
  4. SetIdentity("13*****225")
  5. response, err := authClient.GetAccessTokenByLinkInfo(getTokenByLinkInfoReq, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. return
  9. }
  10. fmt.Println(response)
  11. }

获取用户绑定信息

此接口需要ak,sk调用, 请使用ak, sk初始化客户端

  • 以下代码用于获取用户绑定信息,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getLinkInfoByUserId() {
  2. getInfoRequest := new(client.GetLinkInfoByUserIDRequest).
  3. SetUserId("test_user1")
  4. response, err := authClient.GetLinkInfoByUserId(getInfoRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())go
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "793162FD-F1F2-4658-9DF2-B14213987B9F",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "authentication_type": "ding",
  7. "created_at": 1571915575496,
  8. "domain_id": "dai****5",
  9. "identity": "g3QxL*****QiEiE",
  10. "last_login_time": 1571915575496,
  11. "status": "normal",
  12. "user_id": "5190***********bf5"
  13. }
  14. ]
  15. }

获取用户认证方式

此接口需要ak,sk调用, 请使用ak, sk初始化客户端

  • 以下代码用于获取用户认证方式,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getLinkInfo() {
  2. getLinkInfoRequest := new(client.GetByLinkInfoRequest).
  3. SetIdentity("1243125125").
  4. SetType("mobile")
  5. response, err := authClient.GetLinkInfo(getLinkInfoRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "BFB8B3C4-7727-401D-9B65-495337FF5ADD",
  3. "authentication_type": "mobile",
  4. "created_at": 1571905906341,
  5. "domain_id": "daily1405",
  6. "identity": "13******225",
  7. "last_login_time": 1571905906341,
  8. "status": "normal",
  9. "user_id": "6c23c9***************5211"
  10. }

绑定用户认证方式

此接口需要ak,sk调用, 请使用ak, sk初始化客户端

  • 以下代码用于绑定用户认证方式,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func accountLink() {
  2. accountLinkRequest := new(client.AccountLinkRequest).
  3. SetType("toabao").
  4. SetIdentity("1233445").
  5. SetUserId("12344556").
  6. SetStatus("wait_link")
  7. response, err := authClient.Link(accountLinkRequest, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. }
  11. fmt.Println(response)
  12. }
  • 返回结果
  1. {
  2. "requestId": "8A62ABC5-92D4-4CF3-A22F-D6FD908D24C6",
  3. "access_token": "eyJhbGciOiJSUzI1NiIsIn****LvWYRJiG29hUiUhjZvD2vS0EdItpSiXMucOBk3J5z1EuN9aeycHaTcA",
  4. "avatar": null,
  5. "default_drive_id": null,
  6. "exist_link": [
  7. {
  8. "identity": "15******29",
  9. "type": "mobile"
  10. }
  11. ],
  12. "expire_time": null,
  13. "expires_in": 300,
  14. "need_link": true,
  15. "nick_name": null,
  16. "refresh_token": null,
  17. "role": null,
  18. "state": null,
  19. "token_type": "Bearer",
  20. "user_id": null,
  21. "user_name": null
  22. }

取消绑定关系

  • 以下代码用于取消绑定关系,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func cancelLink() {
  2. cancelLinkRequest := new(client.CancelLinkRequest).
  3. SetTemporaryToken("")
  4. response, err := authClient.CancelLink(cancelLinkRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }

确定绑定关系

此接口需要accessToken调用, 请使用clientId,ClientSecret初始化客户端

  • 以下代码用于确定绑定关系,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func confirmLink() {
  2. confirmLinkRequest := new(client.ConfirmLinkRequest).
  3. SetTemporaryToken("")
  4. response, err := authClient.ConfirmLink(confirmLinkRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }

User 相关 API

创建User

  • 以下代码用于创建User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func createUser() {
  2. createUserRequest := new(client.CreateUserRequest).
  3. SetUserId("test_user").
  4. SetRole("user").
  5. SetUserName("test_user")
  6. response, err := ccpClient.CreateUser(createUserRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(response)
  11. }
  • 返回结果
  1. {
  2. "requestId": "A2808B75-251C-426E-9727-116081F41AC3",
  3. "avatar": null,
  4. "created_at": 1573089204640,
  5. "default_drive_id": "1301",
  6. "description": null,
  7. "domain_id": "daily1404",
  8. "email": null,
  9. "nick_name": null,
  10. "phone": null,
  11. "role": "user",
  12. "status": "enabled",
  13. "updated_at": 0,
  14. "user_id": "test_user",
  15. "user_name": "test_user"
  16. }

获取User

  • 以下代码用于获取User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getUser() {
  2. getUserRequest := new(client.GetUserRequest).
  3. SetUserId("test_user")
  4. response, err := ossClient.GetUser(getUserRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "2678D6FC-1090-4724-A070-AA0B57811581",
  3. "avatar": null,
  4. "created_at": 1573089204640,
  5. "default_drive_id": "1301",
  6. "description": null,
  7. "domain_id": "daily1404",
  8. "email": null,
  9. "nick_name": null,
  10. "phone": null,
  11. "role": "user",
  12. "status": "enabled",
  13. "updated_at": 1573089204655,
  14. "user_id": "test_user",
  15. "user_name": "test_user"
  16. }

列举User

  • 以下代码用于列举User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func listUser() {
  2. listUserRequest := new(client.ListUserRequest).
  3. SetLimit(10)
  4. response, err := ossClient.ListUsers(listUserRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  • 返回结果
  1. {
  2. "requestId": "8E399439-B244-434B-A212-3BF702891692",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "avatar": "",
  7. "created_at": 1572340950210,
  8. "default_drive_id": "901",
  9. "description": "",
  10. "domain_id": "daily1404",
  11. "email": "",
  12. "nick_name": "root",
  13. "phone": "",
  14. "role": "user",
  15. "status": "enabled",
  16. "updated_at": 1572340950225,
  17. "user_id": "10c58ee7455a418a90f4454cc85a372e",
  18. "user_name": "root"
  19. },
  20. {
  21. "requestId": null,
  22. "avatar": "",
  23. "created_at": 1573089204640,
  24. "default_drive_id": "1301",
  25. "description": "",
  26. "domain_id": "daily1404",
  27. "email": "",
  28. "nick_name": "",
  29. "phone": "",
  30. "role": "user",
  31. "status": "enabled",
  32. "updated_at": 1573089204655,
  33. "user_id": "test_user",
  34. "user_name": "test_user"
  35. }
  36. ],
  37. "next_marker": null
  38. }

更新User

  • 以下代码用于更新User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func updateUser() {
  2. updateUserRequest := new(client.UpdateUserRequest).
  3. SetUserId("test_user").
  4. SetDescription("changed_user")
  5. response, err := ossClient.UpdateUser(updateUserRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "B81E6A92-53C7-4715-AD9F-D3DB10154D78",
  3. "avatar": null,
  4. "created_at": 1573089204640,
  5. "default_drive_id": "1301",
  6. "description": "changed_user",
  7. "domain_id": "daily1404",
  8. "email": null,
  9. "nick_name": null,
  10. "phone": null,
  11. "role": "user",
  12. "status": "enabled",
  13. "updated_at": 1573089205333,
  14. "user_id": "test_user",
  15. "user_name": "test_user"
  16. }

搜索User

  • 以下代码用于搜索User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func searchUser() {
  2. searchUserRequest := new(client.SearchUserRequest).
  3. SetUserName("test_user")
  4. response, err := ossClient.SearchUser(searchUserRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "C292180C-9DC9-4EC1-9C64-AD0FE215E159",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "avatar": "",
  7. "created_at": 1573089204640,
  8. "default_drive_id": "1301",
  9. "description": "changed_user",
  10. "domain_id": "daily1404",
  11. "email": "",
  12. "nick_name": "",
  13. "phone": "",
  14. "role": "user",
  15. "status": "enabled",
  16. "updated_at": 1573089576836,
  17. "user_id": "test_user",
  18. "user_name": "test_user"
  19. }
  20. ],
  21. "next_marker": null
  22. }

删除User

  • 以下代码用于删除User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func deleteUser() {
  2. deleteUserRequest := new(client.DeleteUserRequest).
  3. SetUserId("test_user")
  4. err := ossClient.DeleteUser(deleteUserRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. // 此接口不返回Body
  9. }

OSSPath Drive 相关API

说明

  • OSSPath 创建drive 需要先拿到store_id,需要先调用AdminListStores的接口。

创建drive

  • 以下代码用于创建drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. // list stores
  2. func listStores() string {
  3. var storeId string
  4. listStoresRequest := new(client.AdminListStoresRequest)
  5. response, err := tokenClient.AdminListStores(listStoresRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. if response != nil {
  10. fmt.Println(response)
  11. storeId = *response.Items[0].StoreId
  12. }
  13. return storeId
  14. }
  15. // create drive
  16. func createDrive() string {
  17. var driveId string
  18. storeId := listStores()
  19. if storeId != "" {
  20. createDriveRequest := new(client.CreateDriveRequest).
  21. SetDriveName("测试").
  22. SetOwner("5***************bf5").
  23. SetTotalSize(100000000).
  24. SetDescription("测试").
  25. SetDriveType("normal").
  26. SetRelativePath("/ldh/").
  27. SetStoreId(storeId)
  28. response, err := ossClient.CreateDrive(createDriveRequest, runtime)
  29. if err != nil {
  30. fmt.Println(err.Error())
  31. }
  32. if response != nil {
  33. fmt.Println(response)
  34. driveId = *response.DriveId
  35. }
  36. }
  37. return driveId
  38. }
  • 返回结果
  1. // list stores result
  2. {
  3. "requestId": "FD9BA144-FBE6-47DC-8C24-834F68FA0881",
  4. "items": [
  5. {
  6. "accelerate_endpoint": "",
  7. "base_path": "",
  8. "bucket": "ccp-daily-test",
  9. "customized_endpoint": "",
  10. "endpoint": "https://oss-cn-hangzhou.aliyuncs.com",
  11. "internal_endpoint": "",
  12. "ownership": "custom",
  13. "policy": "",
  14. "role_arn": "",
  15. "store_id": "90fb***************1d9b",
  16. "type": "oss"
  17. }
  18. ]
  19. }
  20. // create drive result
  21. {
  22. "requestId": "78A0512B-DBF5-40A2-8147-7F0EE414B1C0",
  23. "domain_id": "daily1405",
  24. "drive_id": "2501"
  25. }

查询drive

  • 以下代码用于查询drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func getDrive(driveId string) {
  2. getDriveRequest := new(client.GetDriveRequest).
  3. SetDriveId(driveId)
  4. response, err := ossClient.GetDrive(getDriveRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "414050C9-C05B-4FFA-BB46-9E891CE3153C",
  3. "creator": null,
  4. "description": "测试",
  5. "domain_id": "d***",
  6. "drive_id": "2501",
  7. "drive_name": "测试",
  8. "drive_type": "normal",
  9. "owner": "5190*************dbf5",
  10. "relative_path": "/ldh/",
  11. "status": "enabled",
  12. "store_id": "90f**************d9b",
  13. "total_size": 100000000,
  14. "used_size": 0
  15. }

列举drive

  • 以下代码用于列举drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func listDrive() {
  2. listDriveRequest := new(client.ListDriveRequest).
  3. SetOwner("51901a***************dbf5").
  4. SetLimit(10)
  5. response, err := ossClient.ListDrives(listDriveRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "6DB486F0-CDF5-46CD-B5B6-7B3D819174E8",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "creator": "",
  7. "description": "test_drive",
  8. "domain_id": "daily****",
  9. "drive_id": "2201",
  10. "drive_name": "test_drive",
  11. "drive_type": "normal",
  12. "owner": "51901a4********2edbf5",
  13. "relative_path": "/test_drive/",
  14. "status": "enabled",
  15. "store_id": "90fba*******d9b",
  16. "total_size": 100000000,
  17. "used_size": 0
  18. }
  19. ],
  20. "next_marker": null
  21. }

更新drive

  • 以下代码用于更新drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func updateDrive(driveId string) {
  2. updateDriveRequest := new(client.UpdateDriveRequest).
  3. SetDriveId(driveId).
  4. SetDescription("changed_drive")
  5. response, err := ossClient.UpdateDrive(updateDriveRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "251478CD-3AB2-4DA0-B0DE-A6D3BF3933D1",
  3. "creator": null,
  4. "description": "changed_drive",
  5. "domain_id": "daily1405",
  6. "drive_id": "2501",
  7. "drive_name": "测试",
  8. "drive_type": "normal",
  9. "owner": "51901a43476d4a5a98141ca8c12edbf5",
  10. "relative_path": "/ldh/",
  11. "status": "enabled",
  12. "store_id": "90fba27e9c40452d91d83b204aee1d9b",
  13. "total_size": 100000000,
  14. "used_size": 0
  15. }

删除drive

  • 以下代码用于删除drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func deleteDrive(driveId string) {
  2. deleteDriveRequest := new(client.DeleteDriveRequest).
  3. SetDriveId(driveId)
  4. err := ossClient.DeleteDrive(deleteDriveRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. // 此接口不返回Body
  9. }

OSSPath Share 相关 API

创建Share

  • 以下代码用于创建Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档
  1. func createShare(driveId string) string {
  2. permissions := []*string{ptrString("FILE.LIST")}
  3. var shareId string
  4. createShareRequest := new(client.CreateShareRequest).
  5. SetOwner("51901a4*******************f5").
  6. SetShareName("test_share").
  7. SetDriveId(driveId).
  8. SetPermissions(permissions).
  9. SetShareFilePath("/test_share/").
  10. SetExpiration(getExpireTime())
  11. response, err := tokenClient.CreateShare(createShareRequest, runtime)
  12. if err != nil {
  13. fmt.Println(err.Error())
  14. }
  15. if response != nil {
  16. fmt.Println(response)
  17. shareId = *response.ShareId
  18. }
  19. return shareId
  20. }
  21. func ptrString(str string) *string {
  22. return &str
  23. }
  24. // get expire time
  25. func getExpireTime() string {
  26. now := time.Now().UTC()
  27. h, _ := time.ParseDuration("1h")
  28. expireTime := now.Add(h).Format("2006-01-02T15:04:05Z")
  29. return expireTime
  30. }
  • 返回结果
  1. {
  2. "requestId": "50774D14-AFA6-4997-84EF-F4C951056276",
  3. "domain_id": "dai***1",
  4. "share_id": "04902f6d-cc3a-4afd-af6d-d34f430db840"
  5. }

查询Share

  • 以下代码用于查询Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func getShare(shareId string) {
  2. getShareRequest := new(client.GetShareRequest).
  3. SetShareId(shareId)
  4. response, err := tokenClient.GetShare(getShareRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "429C2A82-1327-460C-829A-4434729F72CD",
  3. "created_at": "2019-11-07T01:45:31.609Z",
  4. "creator": "6c23*******f5211",
  5. "description": null,
  6. "domain_id": "dai***05",
  7. "drive_id": "2201",
  8. "expiration": "2019-11-07T02:45:31Z",
  9. "expired": false,
  10. "owner": "51901a4******edbf5",
  11. "permissions": [
  12. "FILE.LIST"
  13. ],
  14. "share_file_path": "/test_share/",
  15. "share_id": "04902f6d-cc3a-4afd-af6d-d34f430db840",
  16. "share_name": "test_share",
  17. "status": "enabled",
  18. "updated_at": "2019-11-07T01:45:31.609Z"
  19. }

列举Share

  • 以下代码用于列举Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func listShare() {
  2. listShareRequest := new(client.ListShareRequest).
  3. SetOwner("51901******5").
  4. SetCreator("61adf*****d353s").SetLimit(10)
  5. response, err := tokenClient.ListShare(listShareRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "0CBADEC0-9739-4A1E-89B5-70CF5AEE7E32",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "created_at": "2019-11-07T01:49:39.215Z",
  7. "creator": "6c23c98d*******9f5211",
  8. "description": "",
  9. "domain_id": "da*****5",
  10. "drive_id": "2201",
  11. "expiration": "2019-11-07T02:49:39Z",
  12. "expired": false,
  13. "owner": "51901a4*******12edbf5",
  14. "permissions": [
  15. "FILE.LIST"
  16. ],
  17. "share_file_path": "/test_share/",
  18. "share_id": "0e5ee6e1-9640-4c22-93f4-b3362d355b1e",
  19. "share_name": "test_share",
  20. "status": "enabled",
  21. "updated_at": "2019-11-07T01:49:39.215Z"
  22. }
  23. ],
  24. "next_marker": null
  25. }

更新Share

  • 以下代码用于更新Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func updateShare(shareId string) {
  2. updateShareReuqest := new(client.UpdateShareRequest).
  3. SetShareId(shareId).
  4. SetDescription("changed_share")
  5. response, err := tokenClient.UpdateShare(updateShareReuqest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "3F484A79-0578-4683-97D6-FBFC5D747506",
  3. "created_at": "2019-11-07T01:49:39.215Z",
  4. "creator": "6c23c9*********211",
  5. "description": "changed_share",
  6. "domain_id": "dai****1",
  7. "drive_id": "2201",
  8. "expiration": "2019-11-07T02:49:39Z",
  9. "expired": false,
  10. "owner": "51901a****************bf5",
  11. "permissions": [],
  12. "share_file_path": "/test_share/",
  13. "share_id": "0e5ee6e1-9640-4c22-93f4-b3362d355b1e",
  14. "share_name": "test_share",
  15. "status": "enabled",
  16. "updated_at": "2019-11-07T01:49:39.215Z"
  17. }

删除Share

  • 以下代码用于删除Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func deleteShare(shareId string) {
  2. deleteShareRequest := new(client.DeleteShareRequest).
  3. SetShareId(shareId)
  4. err := tokenClient.DeleteShare(deleteShareRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. // 此接口不返回Body
  9. }

OSSPath File相关 API

创建File

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

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

  1. func createFile() {
  2. createFileRequest := new(client.OSSCreateFileRequest).SetDriveId("2201").
  3. SetName("b.txt").
  4. SetType("file").
  5. SetParentFilePath("/").
  6. SetContentType("text/plain")
  7. response, err := ossClient.CreateFile(createFileRequest, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. return
  11. }
  12. fmt.Println(response)
  13. }
  • 返回结果
  1. {
  2. "requestId": "8249D5E8-3D07-4D56-AE69-7E337C6B4659",
  3. "domain_id": "dai***05",
  4. "drive_id": "2201",
  5. "file_path": "/a.jpg",
  6. "part_info_list": [
  7. {
  8. "etag": null,
  9. "part_number": 1,
  10. "part_size": null,
  11. "upload_url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?E911F0FBD3"
  12. }
  13. ],
  14. "share_id": null,
  15. "type": "file",
  16. "upload_id": "35EE8647048349D09A950DE911F0FBD3"
  17. }

列举File

  • 以下代码用于列举File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func listFile() {
  2. listFileRequest := new(client.OSSListFileRequest).
  3. SetDriveId("2201").
  4. SetParentFilePath("/xxx/").
  5. SetLimit(10)
  6. response, err := ossClient.ListFile(listFileRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. return
  10. }
  11. fmt.Println(response)
  12. }
  • 返回结果
  1. {
  2. "requestId": "8BA9F8EA-E1A3-4E58-90A8-339336EC8A64",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "content_hash": null,
  7. "content_hash_name": null,
  8. "content_type": "",
  9. "crc64_hash": null,
  10. "created_at": null,
  11. "description": null,
  12. "domain_id": "dai***05",
  13. "download_url": null,
  14. "drive_id": "2201",
  15. "file_extension": "",
  16. "file_path": "/test_folder/",
  17. "name": "test_folder",
  18. "parent_file_path": "/",
  19. "share_id": null,
  20. "size": null,
  21. "status": "available",
  22. "thumbnail": null,
  23. "trashed_at": null,
  24. "type": "folder",
  25. "updated_at": null,
  26. "upload_id": null,
  27. "url": null
  28. },
  29. {
  30. "requestId": null,
  31. "content_hash": null,
  32. "content_hash_name": null,
  33. "content_type": "",
  34. "crc64_hash": null,
  35. "created_at": null,
  36. "description": null,
  37. "domain_id": "daily1405",
  38. "download_url": "https://ccp-da*********",
  39. "drive_id": "2201",
  40. "file_extension": "jpg",
  41. "file_path": "/a.jpg",
  42. "name": "a.jpg",
  43. "parent_file_path": "/",
  44. "share_id": null,
  45. "size": 3448250,
  46. "status": "available",
  47. "thumbnail": "https://ccp-daily-t******0",
  48. "trashed_at": null,
  49. "type": "file",
  50. "updated_at": "2019-11-07T06:49:08Z",
  51. "upload_id": null,
  52. "url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/te***%3D"
  53. }
  54. ],
  55. "next_marker": null
  56. }

查询File

  • 以下代码用于查询File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getFile() {
  2. getFileRequest := new(client.OSSGetFileRequest).
  3. SetDriveId("2201").
  4. SetFilePath("/xxxx/")
  5. response, err := ossClient.GetFile(getFileRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. return
  9. }
  10. fmt.Println(response)
  11. }
  • 返回结果
  1. {
  2. "requestId": "7D044402-4A39-40B6-8A21-5D1A2741F5FC",
  3. "content_hash": null,
  4. "content_hash_name": null,
  5. "content_type": "image/jpg",
  6. "crc64_hash": null,
  7. "created_at": null,
  8. "description": null,
  9. "domain_id": "daily1405",
  10. "download_url": "https://ccp-d*********eg%3D",
  11. "drive_id": "2201",
  12. "file_extension": null,
  13. "file_path": "/a.jpg",
  14. "name": "a.jpg",
  15. "parent_file_path": "/",
  16. "share_id": null,
  17. "size": 3448250,
  18. "status": "available",
  19. "thumbnail": null,
  20. "trashed_at": null,
  21. "type": "file",
  22. "updated_at": "2019-11-07T06:49:08Z",
  23. "upload_id": null,
  24. "url": "https://ccp-daily-***QQkX8PWQ0JFdcQfhvY0WfEv2P0JdFAz93aYgXDZn0AjAdL1JNz3G6BGcjaaBOmrsERm9J1NnFeg%3D"
  25. }

移动File

  • 以下代码用于移动File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func moveFile() {
  2. moveFileRequest := new(client.OSSMoveFileRequest).
  3. SetDriveId("2201").
  4. SetFilePath("/xxx/").
  5. SetToParentFilePath("/yyyy/")
  6. response, err := ossClient.MoveFile(moveFileRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. return
  10. }
  11. fmt.Println(response)
  12. }
  • 返回结果
  1. {
  2. "requestId": "675E40E6-1BFF-4A53-8A81-B300F03102C4",
  3. "async_task_id": null,
  4. "domain_id": "daily1405",
  5. "drive_id": "2201",
  6. "file_path": "/test_folder/a.jpg",
  7. "share_id": null
  8. }

复制File

  • 以下代码用于复制File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func copyFile() {
  2. copyFileRequest := new(client.OSSCopyFileRequest).
  3. SetDriveId("2201").
  4. SetFilePath("/xxx/").
  5. SetToParentFilePath("/yyy/")
  6. response, err := ossClient.CopyFile(copyFileRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. return
  10. }
  11. fmt.Println(response)
  12. }
  • 返回结果
  1. {
  2. "requestId": "205CB077-F075-43D2-A181-4114587B4694",
  3. "async_task_id": null,
  4. "domain_id": "daily1405",
  5. "drive_id": "2201",
  6. "file_path": "/a.jpg",
  7. "share_id": null
  8. }

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getUploadUrl() {
  2. var uploadInfo = new(client.UploadPartInfo)
  3. getUploadUrlrequest := new(client.OSSGetUploadUrlRequest).
  4. SetDriveId("").
  5. SetPartInfoList([]*client.UploadPartInfo{uploadInfo}).
  6. SetUploadId("").
  7. SetFilePath("")
  8. response, err := ossClient.GetUploadUrl(getUploadUrlrequest, runtime)
  9. if err != nil {
  10. fmt.Println(err.Error())
  11. return
  12. }
  13. fmt.Println(response)
  14. }
  • 返回结果
  1. {
  2. "requestId": "0A433A26-D29E-4038-ACEB-1359615C3C58",
  3. "create_at": "2019-11-07T06:49:07.304Z",
  4. "domain_id": "daily1405",
  5. "drive_id": "2201",
  6. "file_path": "/a.jpg",
  7. "part_info_list": [
  8. {
  9. "etag": null,
  10. "part_number": 1,
  11. "part_size": null,
  12. "upload_url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?Expires=1573112947\u0026OSSAc****ploadId=35EE8647048349D09A950DE911F0FBD3"
  13. }
  14. ],
  15. "upload_id": "35EE8647048349D09A950DE911F0FBD3"
  16. }

获取File下载地址

  • 以下代码用于获取File下载地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getDownloadUrl() {
  2. getDownloadUrlRequest := new(client.OSSGetDownloadUrlRequest).
  3. SetDriveId("2201").
  4. SetFilePath("/xxx").
  5. SetExpireSec(3600)
  6. response, err := ossClient.GetDownloadUrl(getDownloadUrlRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. return
  10. }
  11. fmt.Println(response)
  12. }
  • 返回结果
  1. {
  2. "requestId": "ECDF23DD-7EFC-4C09-A89B-4747D08212CA",
  3. "expiration": "2019-11-07T07:04:08.488Z",
  4. "method": "GET",
  5. "url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?Expires=1573110248\u0026OSSAcce****NnFeg%3D"
  6. }

Complete File

  • 以下代码用于Complete File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func completeFile() {
  2. completeFileRequest := new(client.OSSCompleteFileRequest).
  3. SetDriveId("2201").
  4. SetFilePath(*filePath).
  5. SetUploadId(*uploadId).
  6. SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
  7. completeResponse, completeErr := ossClient.CompleteFile(completeFileRequest, runtime)
  8. if completeErr != nil {
  9. fmt.Println(completeErr.Error())
  10. }
  11. fmt.Println(completeResponse)
  12. }
  • 返回结果
  1. {
  2. "requestId": "F89A859B-6FC3-4791-B7A2-BB2F0AB78709",
  3. "content_hash": null,
  4. "content_hash_name": null,
  5. "content_type": "image/jpg",
  6. "crc64_hash": "1864830531212108369",
  7. "created_at": null,
  8. "description": null,
  9. "domain_id": "daily1405",
  10. "download_url": null,
  11. "drive_id": "2201",
  12. "file_extension": null,
  13. "file_path": "/a.jpg",
  14. "name": "a.jpg",
  15. "parent_file_path": "/",
  16. "share_id": null,
  17. "size": 0,
  18. "status": "uploading",
  19. "thumbnail": null,
  20. "trashed_at": null,
  21. "type": "file",
  22. "updated_at": null,
  23. "upload_id": "35EE8647048349D09A950DE911F0FBD3",
  24. "url": null,
  25. "crc": "1864830531212108369"
  26. }

删除File

  • 以下代码用于删除File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func deleteFile() {
  2. deleteFileRequest := new(client.OSSDeleteFileRequest).SetDriveId("1").
  3. SetFilePath("xxx")
  4. err := ossClient.DeleteFile(deleteFileRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. // 此接口不返回Body
  9. }

列举Stores File

  • 以下代码用于列举Stores File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func listStoreFiles() {
  2. lisStoreFileReq := new(client.ListStoreFileRequest).
  3. SetStoreId("90fba27e********4aee1d9b")
  4. res, err := tokenClient.ListStorefile(lisStoreFileReq, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. return
  8. }
  9. fmt.Println(res)
  10. }
  • 返回结果
  1. {
  2. "requestId": "1536E018-5F86-4927-91E2-89171EDBF792",
  3. "items": [
  4. {
  5. "domain_id": "",
  6. "name": "bky",
  7. "parent_file_path": "/",
  8. "store_id": "90fba2**************ee1d9b",
  9. "type": "folder"
  10. },
  11. {
  12. "domain_id": "",
  13. "name": "superadmin",
  14. "parent_file_path": "/",
  15. "store_id": "90fba2**************ee1d9b",
  16. "type": "folder"
  17. },
  18. {
  19. "domain_id": "",
  20. "name": "test",
  21. "parent_file_path": "/",
  22. "store_id": "90fba2**************ee1d9b",
  23. "type": "folder"
  24. },
  25. {
  26. "domain_id": "",
  27. "name": "test_drive",
  28. "parent_file_path": "/",
  29. "store_id": "90fba2**************ee1d9b",
  30. "type": "folder"
  31. }
  32. ],
  33. "next_marker": null
  34. }

CCPPath Drive 相关API

创建drive

  • 以下代码用于创建drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func createDrive() string {
  2. var driveId string
  3. createDriveRequest := new(client.CreateDriveRequest).
  4. SetDriveName("测试").
  5. SetOwner("superadmin").
  6. SetTotalSize(100000000).
  7. SetDescription("测试").
  8. SetDriveType("normal")
  9. response, err := ccpClient.CreateDrive(createDriveRequest, runtime)
  10. if err != nil {
  11. fmt.Println(err.Error())
  12. }
  13. if response != nil {
  14. fmt.Println(response)
  15. driveId = *response.DriveId
  16. }
  17. return driveId
  18. }
  • 返回结果
  1. {
  2. "requestId": "DFACCE19-F046-4739-AFBE-6ABBF0D2DA52",
  3. "domain_id": "dai****04",
  4. "drive_id": "1401"
  5. }

列举drive

  • 以下代码用于列举drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func listDrive() {
  2. listDriveRequest := new(client.ListDriveRequest).
  3. SetOwner("superadmin").
  4. SetLimit(10)
  5. response, err := ccpClient.ListDrives(listDriveRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "F84761E7-03BD-4DEF-875E-7809C1578DD7",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "creator": "System",
  7. "description": "Created by system",
  8. "domain_id": "daily1404",
  9. "drive_id": "1",
  10. "drive_name": "Default",
  11. "drive_type": "normal",
  12. "owner": "superadmin",
  13. "relative_path": "",
  14. "status": "enabled",
  15. "store_id": "55ff60f************46904",
  16. "total_size": 1073741824,
  17. "used_size": 5212716
  18. }
  19. ],
  20. "next_marker": null
  21. }

查询drive

  • 以下代码用于查询drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getDrive(driveId string) {
  2. getDriveRequest := new(client.GetDriveRequest).
  3. SetDriveId(driveId)
  4. response, err := ccpClient.GetDrive(getDriveRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. fmt.Println(response)
  9. }
  • 返回结果
  1. {
  2. "requestId": "91F9CCE0-2576-4D85-925A-630B1013B98D",
  3. "creator": null,
  4. "description": "测试",
  5. "domain_id": "dai***1",
  6. "drive_id": "1401",
  7. "drive_name": "测试",
  8. "drive_type": "normal",
  9. "owner": "superadmin",
  10. "relative_path": null,
  11. "status": "enabled",
  12. "store_id": "55ff6************904",
  13. "total_size": 100000000,
  14. "used_size": 0
  15. }

更新drive

  • 以下代码用于更新drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func updateDrive(driveId string) {
  2. updateDriveRequest := new(client.UpdateDriveRequest).
  3. SetDriveId(driveId).
  4. SetDescription("changed_drive")
  5. response, err := ccpClient.UpdateDrive(updateDriveRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(response)
  10. }
  • 返回结果
  1. {
  2. "requestId": "B9B9D80D-932B-4BE2-A106-F2DBC8AEFD3B",
  3. "creator": null,
  4. "description": "changed_drive",
  5. "domain_id": "daily1404",
  6. "drive_id": "1401",
  7. "drive_name": "测试",
  8. "drive_type": "normal",
  9. "owner": "superadmin",
  10. "relative_path": null,
  11. "status": "enabled",
  12. "store_id": "55ff60f************946904",
  13. "total_size": 100000000,
  14. "used_size": 0
  15. }

删除drive

  • 以下代码用于删除drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func deleteDrive(driveId string) {
  2. deleteDriveRequest := new(client.DeleteDriveRequest).
  3. SetDriveId(driveId)
  4. err := ccpClient.DeleteDrive(deleteDriveRequest, runtime)
  5. if err != nil {
  6. fmt.Println(err.Error())
  7. }
  8. // 此接口不返回Body
  9. }

CCPPath File 相关 API

创建File

  • 以下代码用于创建File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func createFile() {
  2. createFileRequest := new(client.CCPCreateFileRequest).
  3. SetDriveId("1").
  4. SetName("b.txt").
  5. SetType("file").
  6. SetParentFileId("root").
  7. SetContentType("text/plain")
  8. response, err := ccpClient.CreateFile(createFileRequest, runtime)
  9. if err != nil {
  10. fmt.Println(err.Error())
  11. return
  12. }
  13. if response != nil {
  14. fmt.Println(response)
  15. }
  16. }
  • 返回结果
  1. {
  2. "requestId": "1087FD69-4F94-424C-890C-024212DAFB5D",
  3. "domain_id": "dai***s04",
  4. "drive_id": "1",
  5. "file_id": "5dc38ecb4e247b6d547b4335971642f2152fb560",
  6. "parent_file_id": "root",
  7. "part_info_list": [
  8. {
  9. "etag": null,
  10. "part_number": 1,
  11. "part_size": null,
  12. "upload_url": "https://ccp-daily1404-hz-1571887934.oss-cn-***A4FCFA"
  13. }
  14. ],
  15. "rapid_upload": false,
  16. "type": "file",
  17. "upload_id": "6057AF147DA741C29424D537BCA4FCFA"
  18. }

列举File

  • 以下代码用于列举File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func listFile() {
  2. listFileReq := new(client.CCPListFileRequest).
  3. SetDriveId("1").
  4. SetParentFileId("root").
  5. SetLimit(10)
  6. listFileRes, err := ccpClient.ListFile(listFileReq, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(listFileRes)
  11. }
  • 返回结果
  1. {
  2. "requestId": "9427C190-0258-4D76-950E-E3A3BAF1DA00",
  3. "items": [
  4. {
  5. "requestId": null,
  6. "category": "",
  7. "content_hash": null,
  8. "content_hash_name": null,
  9. "content_type": "",
  10. "crc64_hash": null,
  11. "created_at": "2019-11-07T08:09:52.833Z",
  12. "description": null,
  13. "domain_id": "daily1404",
  14. "download_url": null,
  15. "drive_id": "1",
  16. "file_extension": "",
  17. "file_id": "5dc3d150a1571d350f12492096bca248a9f47336",
  18. "hidden": false,
  19. "image_media_metadata": null,
  20. "labels": null,
  21. "meta": null,
  22. "name": "test_folder",
  23. "parent_file_id": "root",
  24. "size": null,
  25. "starred": false,
  26. "status": "available",
  27. "thumbnail": null,
  28. "trashed_at": null,
  29. "type": "folder",
  30. "updated_at": "2019-11-07T08:09:52.833Z",
  31. "upload_id": null,
  32. "url": null
  33. },
  34. {
  35. "requestId": null,
  36. "category": "image",
  37. "content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
  38. "content_hash_name": "sha1",
  39. "content_type": "",
  40. "crc64_hash": null,
  41. "created_at": "2019-11-07T08:09:52.877Z",
  42. "description": null,
  43. "domain_id": "daily1404",
  44. "download_url": "https://ccp-d***jpg",
  45. "drive_id": "1",
  46. "file_extension": "jpg",
  47. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  48. "hidden": false,
  49. "image_media_metadata": null,
  50. "labels": null,
  51. "meta": null,
  52. "name": "a.jpg",
  53. "parent_file_id": "root",
  54. "size": 3448250,
  55. "starred": false,
  56. "status": "available",
  57. "thumbnail": "https://cc****w_128%2Climit_0",
  58. "trashed_at": null,
  59. "type": "file",
  60. "updated_at": "2019-11-07T08:09:54.877Z",
  61. "upload_id": null,
  62. "url": "https://ccp-dail**8eyId=LTAIi5kx6X0mwR8k\u0026Signature=WhbAImqKrMmXgJyBzNZ4EWGcBEY%3D"
  63. }
  64. ],
  65. "next_marker": null
  66. }

查询File

  • 以下代码用于查询File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func searcFile() {
  2. searchFileReq := new(client.CCPSearchFileRequest).
  3. SetDriveId("1").
  4. SetLimit(10).
  5. SetOrderBy("type DESC").
  6. SetQuery("file_extension in ['txt']")
  7. searchFileRes, err := ccpClient.SearchFile(searchFileReq, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. }
  11. fmt.Println(searchFileRes)
  12. }
  • 返回结果
  1. {
  2. "requestId": "2D44D3FB-97A1-4D78-AD56-B68FE3EF5845",
  3. "category": "image",
  4. "content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
  5. "content_hash_name": "sha1",
  6. "content_type": "image/jpg",
  7. "crc64_hash": "1864830531212108369",
  8. "created_at": "2019-11-07T08:09:52.877Z",
  9. "description": "changed_file",
  10. "domain_id": "daily1404",
  11. "download_url": "https://ccp-daily1******u0026response-content-disposition=attachment%3Bfilename%3Da.jpg",
  12. "drive_id": "1",
  13. "file_extension": "jpg",
  14. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  15. "hidden": false,
  16. "image_media_metadata": null,
  17. "labels": null,
  18. "meta": null,
  19. "name": "a.jpg",
  20. "parent_file_id": "root",
  21. "size": 3448250,
  22. "starred": false,
  23. "status": "available",
  24. "thumbnail": "https://ccp-daily1404-h***28%2Climit_0",
  25. "trashed_at": null,
  26. "type": "file",
  27. "updated_at": "2019-11-07T08:09:54.877Z",
  28. "upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
  29. "url": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db***3D"
  30. }

移动File

  • 以下代码用于移动File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func moveFile() {
  2. moveFileRequest := new(client.CCPMoveFileRequest).
  3. SetFileId(FileId).
  4. SetDriveId("1").
  5. SetToParentFileId(foderId)
  6. moveRes, err := ccpClient.MoveFile(moveFileRequest, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(moveRes)
  11. }
  • 返回结果
  1. {
  2. "requestId": "BBF646CE-73DF-4A82-A637-5F419E9D220D",
  3. "async_task_id": null,
  4. "domain_id": "daily1404",
  5. "drive_id": "1",
  6. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568"
  7. }

复制File

  • 以下代码用于复制File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func copyFile() {
  2. copyFileReq := new(client.CCPCopyFileRequest).
  3. SetFileId(FileId).
  4. SetDriveId("1").
  5. SetToParentFileId("root")
  6. copyRes, err := ccpClient.CopyFile(copyFileReq, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(copyRes)
  11. }
  • 返回结果
  1. {
  2. "requestId": "2913878A-663E-4B6B-AD9D-53406AAC7DAB",
  3. "async_task_id": null,
  4. "domain_id": "daily1404",
  5. "drive_id": "1",
  6. "file_id": "5dc3d15d9d751fdd49d44924b98baf430e84e612"
  7. }

获取File上传地址

  • 以下代码用于获取File上传地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getUploadUrl() {
  2. uploadPart := new(client.UploadPartInfo)
  3. uploadPart.SetPartNumber(1)
  4. getUploadUrlReq := new(client.CCPGetUploadUrlRequest).
  5. SetUploadId(uploadId).
  6. SetDriveId("1").
  7. SetFileId(FileId).
  8. SetPartInfoList([]*client.UploadPartInfo{uploadPart})
  9. uploadurlResponse, err := ccpClient.GetUploadUrl(getUploadUrlReq, runtime)
  10. if err != nil {
  11. fmt.Println(err.Error())
  12. }
  13. fmt.Println(uploadurlResponse)
  14. }
  • 返回结果
  1. {
  2. "requestId": "DECE883C-F54C-4B6E-B860-EB02D84BEA53",
  3. "create_at": "2019-11-07T08:09:53.098Z",
  4. "domain_id": "daily1404",
  5. "drive_id": "1",
  6. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  7. "part_info_list": [
  8. {
  9. "etag": null,
  10. "part_number": 1,
  11. "part_size": null,
  12. "upload_url": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5***0026uploadId=CF977B42DE8D4CA38F4EB8334F0C759A"
  13. }
  14. ],
  15. "upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A"
  16. }

获取File下载地址

  • 以下代码用于获取File下载地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func getDownloadUrl() {
  2. getDownloadRequest := new(client.CCPGetDownloadUrlRequest).
  3. SetDriveId("1").
  4. SetFileId(FileId)
  5. getdownloadResponse, err := ccpClient.GetDownloadUrl(getDownloadRequest, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(getdownloadResponse)
  10. }
  • 返回结果
  1. {
  2. "requestId": "BC2B0E86-798A-4537-B621-AD91ECF91F9C",
  3. "expiration": "2019-11-07T08:24:54.437Z",
  4. "method": "GET",
  5. "url": "https://ccp-d*****3Da.jpg"
  6. }

Complete File

  • 以下代码用于Complete File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func completeFile() {
  2. uploadPartInfo := new(client.UploadPartInfo)
  3. uploadPartInfo.SetEtag(Etag)
  4. uploadPartInfo.SetPartNumber(1)
  5. // complete file
  6. completeFileRequest := new(client.CCPCompleteFileRequest).
  7. SetDriveId("1").
  8. SetFileId(*FileId).
  9. SetUploadId(*uploadId).
  10. SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
  11. completeResponse, err := ccpClient.CompleteFile(completeFileRequest, runtime)
  12. if err != nil {
  13. fmt.Println(err.Error())
  14. }
  15. fmt.Println(completeResponse)
  16. }
  • 返回结果
  1. {
  2. "requestId": "77F6CF69-3458-4A2F-9823-F3ED8F919723",
  3. "category": "image",
  4. "content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
  5. "content_hash_name": "sha1",
  6. "content_type": "image/jpg",
  7. "crc64_hash": "1864830531212108369",
  8. "created_at": "2019-11-07T08:09:52.877Z",
  9. "description": null,
  10. "domain_id": "daily1404",
  11. "download_url": null,
  12. "drive_id": "1",
  13. "file_extension": "jpg",
  14. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  15. "hidden": false,
  16. "image_media_metadata": null,
  17. "labels": null,
  18. "meta": null,
  19. "name": "a.jpg",
  20. "parent_file_id": "root",
  21. "size": 3448250,
  22. "starred": false,
  23. "status": "available",
  24. "thumbnail": null,
  25. "trashed_at": null,
  26. "type": "file",
  27. "updated_at": "2019-11-07T08:09:54.877Z",
  28. "upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
  29. "url": null,
  30. "crc": null
  31. }

更新File

  • 以下代码用于更新File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func updateFile() {
  2. updateFileReq := new(client.CCPUpdateFileMetaRequest).
  3. SetFileId(FileId).
  4. SetDriveId("1").
  5. SetDescription("changed_file")
  6. updateFileRes, err := ccpClient.UpdateFile(updateFileReq, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. fmt.Println(updateFileRes)
  11. }
  • 返回结果
  1. {
  2. "requestId": "5619DE43-D03F-451A-A299-C982C9CE1B3B",
  3. "category": "image",
  4. "content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
  5. "content_hash_name": "sha1",
  6. "content_type": "image/jpg",
  7. "crc64_hash": "1864830531212108369",
  8. "created_at": "2019-11-07T08:09:52.877Z",
  9. "description": "changed_file",
  10. "domain_id": "daily1404",
  11. "download_url": "https://ccp****sponse-content-disposition=attachment%3Bfilename%3Da.jpg",
  12. "drive_id": "1",
  13. "file_extension": "jpg",
  14. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  15. "hidden": false,
  16. "image_media_metadata": null,
  17. "labels": null,
  18. "meta": null,
  19. "name": "a.jpg",
  20. "parent_file_id": "root",
  21. "size": 3448250,
  22. "starred": false,
  23. "status": "available",
  24. "thumbnail": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db80819d*****%2Climit_0",
  25. "trashed_at": null,
  26. "type": "file",
  27. "updated_at": "2019-11-07T08:09:54.877Z",
  28. "upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
  29. "url": "https://ccp-daily1*******%3D"
  30. }

搜索File

  • 以下代码用于搜索File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func searchFile() {
  2. searchFileReq := new(client.CCPSearchFileRequest).
  3. SetDriveId("1").
  4. SetLimit(10).
  5. SetOrderBy("type DESC").
  6. SetQuery("file_extension in ['jpg']")
  7. searchFileRes, err := ccpClient.SearchFile(searchFileReq, runtime)
  8. if err != nil {
  9. fmt.Println(err.Error())
  10. }
  11. fmt.Println(searchFileRes)
  12. }
  • 返回结果
  1. {
  2. "requestId": "5396BEA0-7F5C-4C3D-B37F-B1675377A825",
  3. "items": [
  4. {
  5. "requestId": "5619DE43-D03F-451A-A299-C982C9CE1B3B",
  6. "category": "image",
  7. "content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
  8. "content_hash_name": "sha1",
  9. "content_type": "image/jpg",
  10. "crc64_hash": "1864830531212108369",
  11. "created_at": "2019-11-07T08:09:52.877Z",
  12. "description": "changed_file",
  13. "domain_id": "daily1404",
  14. "download_url": "https://ccp****sponse-content-disposition=attachment%3Bfilename%3Da.jpg",
  15. "drive_id": "1",
  16. "file_extension": "jpg",
  17. "file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
  18. "hidden": false,
  19. "image_media_metadata": null,
  20. "labels": null,
  21. "meta": null,
  22. "name": "a.jpg",
  23. "parent_file_id": "root",
  24. "size": 3448250,
  25. "starred": false,
  26. "status": "available",
  27. "thumbnail": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db80819d*****%2Climit_0",
  28. "trashed_at": null,
  29. "type": "file",
  30. "updated_at": "2019-11-07T08:09:54.877Z",
  31. "upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
  32. "url": "https://ccp-daily1*******%3D"
  33. }
  34. ],
  35. "next_marker": null
  36. }

删除File

  • 以下代码用于删除File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func deleteFile() {
  2. deleteFileReq := new(client.CCPDeleteFileRequest).
  3. SetDriveId("1").
  4. SetFileId(*FileId)
  5. deleteFileRes, err := ccpClient.DeleteFile(deleteFileReq, runtime)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. }
  9. fmt.Println(deleteFileRes)
  10. // 此接口不返回Body
  11. }

创建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. package main
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-ccp/ccppath-sdk/go/client"
  5. "net/http"
  6. "strings"
  7. )
  8. var clientConf = new(client.Config).
  9. SetDomainId("your domain id").
  10. SetProtocol("https").
  11. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  12. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  13. var runtime = new(client.RuntimeOptions)
  14. var ccpClient, _ = client.NewClient(clientConf)
  15. func main() {
  16. createFile()
  17. }
  18. func createFile() {
  19. // create file
  20. fmt.Println("******************create file******************")
  21. createFileRequest := new(client.CCPCreateFileRequest).
  22. SetDriveId("2201").
  23. SetName("b.txt").
  24. SetType("file").
  25. SetParentFileId("root").
  26. SetContentType("text/plain")
  27. response, err := ccpClient.CreateFile(createFileRequest, runtime)
  28. if err != nil {
  29. fmt.Println(err.Error())
  30. return
  31. }
  32. var uploadUrl, uploadId, FileId *string
  33. fmt.Println(response)
  34. if response != nil {
  35. uploadUrl = response.PartInfoList[0].UploadUrl
  36. uploadId = response.UploadId
  37. FileId = response.FileId
  38. // upload file
  39. // 此处可以使用os.Open()上传本地文件, 具体使用请参考os package。
  40. fmt.Println("******************upload file******************")
  41. content := strings.NewReader("12345678")
  42. req, _ := http.NewRequest("PUT", *uploadUrl, content)
  43. req.Header.Add("Content-Type", "")
  44. res, _ := http.DefaultClient.Do(req)
  45. Etag := res.Header.Get("ETag")
  46. uploadPartInfo := new(client.UploadPartInfo)
  47. uploadPartInfo.SetEtag(Etag)
  48. uploadPartInfo.SetPartNumber(1)
  49. // complete file
  50. fmt.Println("******************complete file******************")
  51. completeFileRequest := new(client.CCPCompleteFileRequest).
  52. SetDriveId("2201").
  53. SetFileId(*FileId).
  54. SetUploadId(*uploadId).
  55. SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
  56. completeResponse, completeErr := ccpClient.CompleteFile(completeFileRequest, runtime)
  57. if completeErr != nil {
  58. fmt.Println(completeErr.Error())
  59. }
  60. fmt.Println(completeResponse)
  61. }
  62. }
版权声明

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

评论

-----