全部产品

Golang SDK 简介

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

下载地址

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

安装步骤

安装Go开发环境

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

安装PDS Go SDK

安装完 Go开发环境后,你需要执行以下操作获取最新的PDS SDK

StandardMode

  1. go get github.com/alibabacloud-go/pds-sdk

初始化Client

AK & SK 初始化客户端

  1. import (
  2. "github.com/alibabacloud-go/pds-sdk/client"
  3. )
  4. var Config = new(client.Config).
  5. SetDomainId("your domain id").
  6. SetProtocol("https").
  7. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  8. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  9. // initial akClient
  10. var Client, _ = client.NewClient(Config)

ClientID & Client Secret 初始化客户端

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

构造请求

Account相关API

说明

客户端需要初始化如下

  1. import (
  2. "github.com/alibabacloud-go/pds-sdk/client"
  3. )
  4. // {domainId} 替换成自己的domainId
  5. var authConfig = new(client.Config).
  6. SetEndpoint("{ domainId }.auth.alicloudccp.com").
  7. SetProtocol("https").
  8. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  9. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  10. // initial authClient
  11. var authClient, _ = client.NewClient(authConfig)

获取图片验证码

  • 以下代码用于获取图片验证码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 获取图片验证码
  2. func getCaptcha() {
  3. getCaptchaRequest := new(client.GetCaptchaRequest)
  4. getCaptchaRequest.SetAppId(os.Getenv("APP_KEY"))
  5. response, err := authClient.GetCaptcha(getCaptchaRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  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. getMnsCodeRequest.SetAppId(os.Getenv("APP_KEY"))
  5. getMnsCodeRequest.SetPhoneNumber("1****5")
  6. getMnsCodeRequest.SetType("login")
  7. response, err := authClient.MobileSendSmsCode(getMnsCodeRequest)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  1. {
  2. "requestId": "4CF58428-912F-4F4F-BE1D-4C06FE559097",
  3. "sms_code_id": "16340***************Slzf"
  4. }

验证手机号是否注册

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

手机号注册

  • 以下代码用于手机号注册,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. // 手机号注册
  2. func MobileRegister() {
  3. mobileRegisterRequest := new(client.MobileRegisterRequest)
  4. mobileRegisterRequest.SetAppId(os.Getenv("APP_KEY"))
  5. mobileRegisterRequest.SetPhoneNumber("1*******21")
  6. mobileRegisterRequest.SetSmsCode("xxx")
  7. mobileRegisterRequest.SetSmsCodeId("xxx")
  8. response, err := authClient.Register(mobileRegisterRequest)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  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. mobileLoginRequest := new(client.MobileLoginRequest)
  3. mobileLoginRequest.SetAppId(os.Getenv("APP_KEY"))
  4. mobileLoginRequest.SetSmsCode("****")
  5. mobileLoginRequest.SetSmsCodeId("******")
  6. mobileLoginRequest.SetPhoneNumber("132********25")
  7. response, err := authClient.Login(mobileLoginRequest)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }

设置登录密码

  • 以下代码用于设置登录密码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func SetPassword() {
  2. setPasswordRequest := new(client.DefaultSetPasswordRequest)
  3. setPasswordRequest.SetAppId(os.Getenv("APP_KEY"))
  4. setPasswordRequest.SetNewPassword("xx")
  5. setPasswordRequest.SetState("xxx")
  6. response, err := authClient.SetPassword(setPasswordRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Headers)
  12. }

密码登录

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

修改登录密码

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

参数中的EncryptedKey获取方式如下

  1. 通过GetPublickey接口获取非对称加密的公钥。
  2. 自己创建一对对称加密密钥(AES-128)。
  3. 使用第一步拿到的公钥加密对称密钥的密钥(此步骤加密后的字符串即为EncryptedKey的值)
  4. 使用自己创建的对称密钥加密密码。(此步骤加密后的字符串即为NewPassword的值)
  1. func ChangePassword() {
  2. changePasswordRequest := new(client.DefaultChangePasswordRequest)
  3. changePasswordRequest.SetAppId(os.Getenv("APP_KEY"))
  4. changePasswordRequest.SetNewPassword("new")
  5. changePasswordRequest.SetPhoneNumber("1****13")
  6. changePasswordRequest.SetEncryptedKey("Your EncryptedKey")
  7. changePasswordRequest.SetState("xxx")
  8. response, err := authClient.ChangePassword(changePasswordRequest)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }

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

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

通过账号获取访问令牌

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

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

获取用户绑定信息

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

  • 以下代码用于获取用户绑定信息,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func GetLinkInfoByUserId() {
  2. getLinkInfoByUserIdRequest := new(client.GetLinkInfoByUserIDRequest)
  3. getLinkInfoByUserIdRequest.SetUserId("****")
  4. response, err := authClient.GetLinkInfoByUserId(getLinkInfoByUserIdRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  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. getLinkInfoRequest.SetIdentity("1*******1")
  4. getLinkInfoRequest.SetType("mobile")
  5. response, err := authClient.GetLinkInfo(getLinkInfoRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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. accountLinkRequest.SetType("taobao")
  4. accountLinkRequest.SetIdentity("1233445")
  5. accountLinkRequest.SetUserId("12344556")
  6. accountLinkRequest.SetStatus("wait_link")
  7. response, err := authClient.Link(accountLinkRequest)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. cancelLinkRequest.SetTemporaryToken("xxx")
  4. response, err := authClient.CancelLink(cancelLinkRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }

确定绑定关系

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

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

获取PublicKey

  • 以下代码用于获取PublicKey,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func GetPublicKeyExample(){
  2. getPublicKeyRequest := new(client.GetAppPublicKeyRequest)
  3. getPublicKeyRequest.SetAppId("your app id")
  4. response, err := authClient.GetPublicKey(getPublicKeyRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  1. {
  2. "app_id":"xxxx",
  3. "public_key":"-----BEGIN RSA PUBLIC KEY-----↵************************************↵-----END RSA PUBLIC KEY-----"
  4. }

User 相关 API

创建User

  • 以下代码用于创建User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func CreateUserExample() {
  2. createUserRequest := new(client.CreateUserRequest)
  3. createUserRequest.SetUserId("admin")
  4. createUserRequest.SetUserName("admin")
  5. createUserRequest.SetRole("admin")
  6. response, err := ccpClient.CreateUser(createUserRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. }
  10. fmt.Println(response.Body)
  11. for s, s2 := range response.Headers {
  12. fmt.Println(s, *s2)
  13. }
  14. }
  • 返回结果
  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 GetUserExample() {
  2. getUserRequest := new(client.GetUserRequest)
  3. getUserRequest.SetUserId("admin")
  4. response, err := ccpClient.GetUser(getUserRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  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 ListUserExample() {
  2. listUserRequest := new(client.ListUserRequest)
  3. listUserRequest.SetLimit(10)
  4. response, err := tokenClient.ListUsers(listUserRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  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 UpdateUserExample() {
  2. updateUserRequest := new(client.UpdateUserRequest)
  3. updateUserRequest.SetUserId("admin")
  4. updateUserRequest.SetDescription("changed_user")
  5. response, err := ccpClient.UpdateUser(updateUserRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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 SearchUserExample() {
  2. searchUserRequest := new(client.SearchUserRequest)
  3. searchUserRequest.SetLimit(10)
  4. response, err := ccpClient.SearchUser(searchUserRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  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 DeleteUserExample() {
  2. deleteUserRequest := new(client.DeleteUserRequest)
  3. deleteUserRequest.SetUserId("admin")
  4. response, err := ccpClient.DeleteUser(deleteUserRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Headers) // deleteUser接口不返回body,若需获取RequestID,请从Headers中获取
  10. }

StandardMode Drive 相关API

创建drive

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

列举drive

  • 以下代码用于列举drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func ListDriveExample() {
  2. listDriveRequest := new(client.ListDriveRequest)
  3. listDriveRequest.SetLimit(10)
  4. listDriveRequest.SetOwner("superadmin")
  5. response, err := Client.ListDrives(listDriveRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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 GetDriveExample() {
  2. getDriveReq := new(client.GetDriveRequest)
  3. getDriveReq.SetDriveId("1")
  4. resp, err := Client.GetDrive(getDriveReq)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(resp.Body)
  10. }
  • 返回结果
  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 UpdateDriveExample(driveId string) {
  2. updateDriveRequest := new(client.UpdateDriveRequest)
  3. updateDriveRequest.SetDriveId(driveId)
  4. updateDriveRequest.SetDescription("changed drive")
  5. response, err := Client.UpdateDrive(updateDriveRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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 DeleteDriveExample(driveId string) {
  2. deleteDriveRequest := new(client.DeleteDriveRequest)
  3. deleteDriveRequest.SetDriveId(driveId)
  4. response, err := Client.DeleteDrive(deleteDriveRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Printf("%v", response.Headers) // deleteDrive接口不返回body,若需获取RequestID,请从Headers中获取
  10. }

获取指定用户默认Drive

  • 以下代码用于获取指定用户的默认drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func GetDefaultDrive(userId string) {
  2. getDefaultDriveRequest := new(client.GetDefaultDriveRequest)
  3. getDefaultDriveRequest.SetUserId(userId)
  4. response, err := Client.GetDefaultDrive(getDefaultDriveRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }

获取调用者所有Drive

说明:只支持tokenClient方式调用

  • 以下代码用于获取调用者名下所有drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func ListMyDrives() {
  2. listMyDriveRequest := new(client.ListMyDriveRequest)
  3. response, err := tokenClient.ListMyDrives(listMyDriveRequest)
  4. if err != nil {
  5. fmt.Println(err)
  6. return
  7. }
  8. fmt.Println(response.Body)
  9. }

StandardMode File 相关 API

创建File

  • 以下代码用于创建File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func CreateFileExample() (fileId string) {
  2. createFileRequest := new(client.CreateFileRequest).SetDriveId("1").
  3. SetName("test___folder").
  4. SetType("folder").
  5. SetParentFileId("root").SetDescription("asdasdasdadadsa")
  6. response, err := Client.CreateFile(createFileRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. return *response.Body.FileId
  13. }
  • 返回结果
  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 ListFileExample() {
  2. listFileRequest := new(client.ListFileRequest)
  3. listFileRequest.SetParentFileId("root")
  4. listFileRequest.SetDriveId("1")
  5. listFileRequest.SetStatus("uploading")
  6. response, err := Client.ListFile(listFileRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 GetFileExample() {
  2. getFileRequest := new(client.GetFileRequest)
  3. getFileRequest.SetFileId("5f3b54672d4cd1b97fcb4e658385061b98d5863f")
  4. getFileRequest.SetDriveId("1")
  5. response, err := Client.GetFile(getFileRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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 MoveFileExample() {
  2. moveFileRequest := new(client.MoveFileRequest)
  3. moveFileRequest.SetToParentFileId("5f3b54672d4cd1b97fcb4e658385061b98d5863f")
  4. moveFileRequest.SetFileId("5f3f2abeb28ec23886d844e397e75fa30f5339af")
  5. moveFileRequest.SetDriveId("1")
  6. response, err := Client.MoveFile(moveFileRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 CopyFileExample() {
  2. copyFileRequest := new(client.CopyFileRequest)
  3. copyFileRequest.SetToParentFileId("root")
  4. copyFileRequest.SetFileId("5f3f2abeb28ec23886d844e397e75fa30f5339af")
  5. copyFileRequest.SetDriveId("1")
  6. copyFileRequest.SetNewName("abcdefg")
  7. response, err := Client.CopyFile(copyFileRequest)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 GetUploadUrlExample() {
  2. var uploadInfo = new(client.UploadPartInfo)
  3. getFileUploadUrlRequest := new(client.GetUploadUrlRequest)
  4. getFileUploadUrlRequest.SetUploadId("xxx")
  5. getFileUploadUrlRequest.SetFileId("/xxxx/")
  6. getFileUploadUrlRequest.SetDriveId("xxx")
  7. getFileUploadUrlRequest.SetPartInfoList([]*client.UploadPartInfo{uploadInfo})
  8. response, err := Client.GetUploadUrl(getFileUploadUrlRequest)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  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 GetDownloadUrlExample() {
  2. getDownloadUrlRequest := new(client.GetDownloadUrlRequest)
  3. getDownloadUrlRequest.SetFileId("5f3f2cc137eebe837534410483f12d4a64325a4f")
  4. getDownloadUrlRequest.SetDriveId("1")
  5. getDownloadUrlRequest.SetExpireSec(3600)
  6. response, err := Client.GetDownloadUrl(getDownloadUrlRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 CompleteFileExample() {
  2. completeFileRequest := new(client.CompleteFileRequest)
  3. completeFileRequest.SetDriveId("2201")
  4. completeFileRequest.SetFileId("xxx")
  5. completeFileRequest.SetUploadId("xxx")
  6. completeFileRequest.SetPartInfoList([]*client.UploadPartInfo{})
  7. response, err := Client.CompleteFile(completeFileRequest)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 UpdateFileExample(fileId string) {
  2. updateFileRequest := new(client.UpdateFileMetaRequest)
  3. updateFileRequest.SetDriveId("1")
  4. updateFileRequest.SetFileId(fileId)
  5. updateFileRequest.SetDescription("Changed File")
  6. response, err := Client.UpdateFile(updateFileRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 ScanFileExample() {
  2. scanFileRequest := new(client.ScanFileMetaRequest)
  3. scanFileRequest.SetDriveId("1")
  4. scanFileRequest.SetCategory("doc")
  5. response, err := Client.ScanFileMeta(scanFileRequest)
  6. if err != nil {
  7. fmt.Println(err.Error())
  8. return
  9. }
  10. fmt.Println(response.Body)
  11. }
  • 返回结果
  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 DeleteFileExample(fileId string) {
  2. deleteFileRequest := new(client.DeleteFileRequest)
  3. deleteFileRequest.SetDriveId("1")
  4. deleteFileRequest.SetFileId(fileId)
  5. response, err := Client.DeleteFile(deleteFileRequest)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Headers) // deleteFile接口不返回body,若需获取RequestID,请从Headers中获取
  11. }

获取异步任务状态

  • 以下代码用于获取一部,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func GetAsyncTaskExample() {
  2. getAsyncTaskRequest := new(client.GetAsyncTaskRequest)
  3. getAsyncTaskRequest.SetAsyncTaskId("**")
  4. response, err := Client.GetAsyncTaskInfo(getAsyncTaskRequest)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(response.Body)
  10. }
  • 返回结果
  1. {
  2. "async_task_id" : "000e89fb-cf8f-11e9-8ab4-b6e980803a3b",
  3. "message" : "task is running",
  4. "state" : "success"
  5. }

批量任务

  • 以下代码用于执行批量操作,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func BatchExample() {
  2. var subRequestBody map[string]interface{}
  3. subRequestBody = make(map[string]interface{})
  4. subRequestBody["drive_id"] = "1"
  5. subRequestBody["file_id"] = "5f3f2abeb28ec23886d844e397e75fa30f5339af"
  6. sub := new(client.BatchSubRequest)
  7. sub.SetBody(subRequestBody)
  8. sub.SetUrl("/file/get")
  9. sub.SetMethod("POST")
  10. sub.SetId("uuid")
  11. batchRequest := new(client.BatchRequest)
  12. batchRequest.SetResource("file")
  13. batchRequest.SetRequests([]*client.BatchSubRequest{sub})
  14. response, err := Client.BatchOperation(batchRequest)
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. fmt.Println(response.Body)
  20. }
  • 返回结果
  1. {
  2. "responses":[
  3. {
  4. "status":200,
  5. "body":{
  6. "file_extension":"txt",
  7. "updated_at":"2020-03-30T08:15:58.267Z",
  8. "content_hash":"FAA12FD40AAC1F492082C90C2CD6C03B9ABDB826",
  9. "domain_id":"hz22",
  10. "size":36,
  11. "category":"doc",
  12. "content_hash_name":"sha1",
  13. "download_url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
  14. "crc64_hash":"13138712399852734283",
  15. "drive_id":"1",
  16. "hidden":false,
  17. "type":"file",
  18. "parent_file_id":"root",
  19. "status":"available",
  20. "description":"changed_file",
  21. "encrypt_mode":"none",
  22. "file_id":"5e81aabdae****d9836b36",
  23. "content_type":"application/oct-stream",
  24. "name":"testDJw8oWE6ef9464f66e5034f69aafd57cb2879170b.txt",
  25. "url":"https://ccp-daily-default-c**n-han***Z8bG%2B9C4VII%3D",
  26. "created_at":"2020-03-30T08:15:57.361Z",
  27. "upload_id":"DFD1DA39317F45EC8534FEA57AE420D1",
  28. "starred":false
  29. },
  30. "id":"624830fa2c2347be8c0d8afc76faa04d"
  31. }
  32. ]
  33. }

列举已经上传的分片

  • 以下代码用于执行批量操作,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func ListUploadedParts() {
  2. listUploadedPartsRequest := new(client.ListUploadedPartRequest)
  3. listUploadedPartsRequest.SetDriveId("**")
  4. listUploadedPartsRequest.SetFileId("**")
  5. listUploadedPartsRequest.SetUploadId("***")
  6. response, err := Client.ListUploadedParts(listUploadedPartsRequest)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  1. {
  2. "drive_id" : "1",
  3. "file_id" : "5d5b846942cf94fa72324c14a4bda34e81da635d",
  4. "limit" : 1,
  5. "part_number_marker" : 1,
  6. "upload_id" : "00668396C0814D818D90F0A92B04B355"
  7. }

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

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

创建APP(获取ClientID, ClientSecret)

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

StandardMode 上传文件 Demo

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/alibabacloud-go/pds-sdk/client"
  5. "net/http"
  6. "strings"
  7. "os"
  8. )
  9. var clientConf = new(client.Config).
  10. SetDomainId("{Domain ID}").
  11. SetProtocol("https").
  12. SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
  13. SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
  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.CreateFileRequest).
  22. SetDriveId("1").
  23. SetName("aaaa.txt").
  24. SetType("file").
  25. SetParentFileId("root").
  26. SetContentType("text/plain")
  27. response, err := Client.CreateFile(createFileRequest)
  28. if err != nil {
  29. fmt.Println(err.Error())
  30. return
  31. }
  32. var uploadUrl, uploadId, FileId *string
  33. if response != nil {
  34. uploadUrl = response.Body.PartInfoList[0].UploadUrl
  35. fmt.Println(uploadUrl)
  36. uploadId = response.Body.UploadId
  37. FileId = response.Body.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.CompleteFileRequest).
  52. SetDriveId("1").
  53. SetFileId(*FileId).
  54. SetUploadId(*uploadId).
  55. SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
  56. completeResponse, completeErr := Client.CompleteFile(completeFileRequest)
  57. if completeErr != nil {
  58. fmt.Println(completeErr.Error())
  59. }
  60. if completeResponse != nil {
  61. fmt.Println(completeResponse.Body)
  62. }
  63. }
  64. }