全部产品
云市场
云游戏

Golang SDK 简介

更新时间:2020-07-10 10:02:48

下载地址

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

安装步骤

安装Go开发环境

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

安装CCP Go SDK

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

CCP Path

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

OSS Path

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

初始化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. getCaptchaModel := new(client.GetCaptchaRequestModel)
  4. getCaptchaRequest := new(client.GetCaptchaRequest)
  5. getCaptchaRequest.SetAppId(os.Getenv("APP_KEY"))
  6. getCaptchaModel.SetBody(getCaptchaRequest)
  7. response, err := authClient.GetCaptcha(getCaptchaModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. getMnsCodeModel := new(client.MobileSendSmsCodeRequestModel)
  4. getMnsCodeRequest := new(client.MobileSendSmsCodeRequest)
  5. getMnsCodeRequest.SetAppId(os.Getenv("APP_KEY"))
  6. getMnsCodeRequest.SetPhoneNumber("1****5")
  7. getMnsCodeRequest.SetType("login")
  8. getMnsCodeModel.SetBody(getMnsCodeRequest)
  9. response, err := authClient.MobileSendSmsCode(getMnsCodeModel, runtime)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. fmt.Println(response.Body)
  15. }
  • 返回结果
  1. {
  2. "requestId": "4CF58428-912F-4F4F-BE1D-4C06FE559097",
  3. "sms_code_id": "16340***************Slzf"
  4. }

验证手机号是否注册

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

手机号注册

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

设置登录密码

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

密码登录

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

修改登录密码

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

参数中的EncryptedKey获取方式如下

​ 1.通过GetPublickey接口获取非对称加密的公钥。

​ 2.自己创建一对对称加密密钥(AES-128)。

​ 3.使用第一步拿到的公钥加密对称密钥的密钥(此步骤加密后的字符串即为EncryptedKey的值)

​ 4. 使用自己创建的对称密钥加密密码。(此步骤加密后的字符串即为NewPassword的值)

  1. func ChangePassword() {
  2. changePasswordModel := new(client.ChangePasswordRequestModel)
  3. changePasswordRequest := new(client.DefaultChangePasswordRequest)
  4. changePasswordRequest.SetAppId(os.Getenv("APP_KEY"))
  5. changePasswordRequest.SetNewPassword("new")
  6. changePasswordRequest.SetPhoneNumber("1****13")
  7. changePasswordRequest.SetEncryptedKey("Your EncryptedKey")
  8. changePasswordRequest.SetState("xxx")
  9. changePasswordModel.SetBody(changePasswordRequest)
  10. response, err := authClient.ChangePassword(changePasswordModel, runtime)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. fmt.Println(response.Body)
  16. }

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

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

通过账号获取访问令牌

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

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

获取用户绑定信息

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

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

确定绑定关系

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

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

获取PublicKey

  • 以下代码用于获取PublicKey,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func GetPublicKeyExample(){
  2. getPublicKeyRequestModel := new(client.GetPublicKeyRequestModel)
  3. getPublicKeyRequest := new(client.GetAppPublicKeyRequest)
  4. getPublicKeyRequest.SetAppId("your app id")
  5. getPublicKeyRequestModel.SetBody(getPublicKeyRequest)
  6. response, err := authClient.GetPublicKey(getPublicKeyRequestModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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. createUserModel := new(client.CreateUserRequestModel)
  3. createUserRequest := new(client.CreateUserRequest)
  4. createUserRequest.SetUserId("xxx")
  5. createUserRequest.SetUserName("xxx")
  6. createUserRequest.SetRole("user")
  7. createUserModel.SetBody(createUserRequest)
  8. response, err := ossClient.CreateUser(createUserModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  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. getUserModel := new(client.GetUserRequestModel)
  3. getUserRequest := new(client.GetUserRequest)
  4. getUserRequest.SetUserId("xxx")
  5. getUserModel.SetBody(getUserRequest)
  6. response, err := ossClient.GetUser(getUserModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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. listUserModel := new(client.ListUsersRequestModel)
  3. listUserRequest := new(client.ListUserRequest)
  4. listUserRequest.SetLimit(10)
  5. listUserRequest.SetMarker("xx")
  6. listUserModel.SetBody(listUserRequest)
  7. response, err := ossClient.ListUsers(listUserModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. updateUserModel := new(client.UpdateUserRequestModel)
  3. updateUserRequest := new(client.UpdateUserRequest)
  4. updateUserRequest.SetUserId("xx")
  5. updateUserRequest.SetDescription("changed_user")
  6. updateUserModel.SetBody(updateUserRequest)
  7. response, err := ossClient.UpdateUser(updateUserModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. searchUserModel := new(client.SearchUserRequestModel)
  3. searchUserRequest := new(client.SearchUserRequest)
  4. searchUserRequest.SetLimit(10)
  5. searchUserRequest.SetMarker("xx")
  6. searchUserRequest.SetUserName("test_user")
  7. searchUserModel.SetBody(searchUserRequest)
  8. response, err := ossClient.SearchUser(searchUserModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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. deleteUserModel := new(client.DeleteUserRequestModel)
  3. deleteUserRequest := new(client.DeleteUserRequest)
  4. deleteUserRequest.SetUserId("test_user")
  5. deleteUserModel.SetBody(deleteUserRequest)
  6. response, err := ossClient.DeleteUser(deleteUserModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Headers) // deleteUser接口不返回body,若需获取RequestID,请从Headers中获取
  12. }

OSSPath Drive 相关API

说明

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

创建drive

  • 以下代码用于创建drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. // list stores
  2. func AdminListStoresExample() {
  3. listStoresModel := new(client.AdminListStoresRequestModel)
  4. listStoresRequest := new(client.AdminListStoresRequest)
  5. listStoresModel.SetBody(listStoresRequest)
  6. response, err := ossClient.AdminListStores(listStoresModel, runtime)
  7. if err != nil {
  8. fmt.Println(err.Error())
  9. }
  10. if response != nil {
  11. fmt.Println("storeId is", *response.Body.Items[0].StoreId)
  12. }
  13. }
  14. // create drive
  15. func CreateDriveExample() {
  16. createDriveModel := new(client.CreateDriveRequestModel)
  17. createDriveRequest := new(client.CreateDriveRequest)
  18. createDriveRequest.SetDriveType("normal")
  19. createDriveRequest.SetDriveName("your drive name")
  20. createDriveRequest.SetTotalSize(10240000)
  21. createDriveRequest.SetOwner("your accout id")
  22. createDriveRequest.SetRelativePath("/abc/")
  23. createDriveRequest.SetStoreId("your store id")
  24. createDriveRequest.SetDescription("test")
  25. createDriveModel.SetBody(createDriveRequest)
  26. resp, err := ossClient.CreateDrive(createDriveModel, runtime)
  27. if err != nil {
  28. fmt.Println(err)
  29. return
  30. }
  31. fmt.Println(resp.Body)
  32. }
  • 返回结果
  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 GetDriveExample() {
  2. getDriveModel := new(client.GetDriveRequestModel)
  3. getDriveReq := new(client.GetDriveRequest)
  4. getDriveReq.SetDriveId("301")
  5. getDriveModel.SetBody(getDriveReq)
  6. resp, err := ossClient.GetDrive(getDriveModel, &client.RuntimeOptions{})
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(resp.Body)
  12. }
  • 返回结果
  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 ListDriveExample() {
  2. listDriveModel := new(client.ListDrivesRequestModel)
  3. listDriveRequest := new(client.ListDriveRequest)
  4. listDriveRequest.SetLimit(10)
  5. listDriveRequest.SetOwner("your accout id")
  6. response, err := ossClient.ListDrives(listDriveModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 UpdateDriveExample() {
  2. updateDriveModel := new(client.UpdateDriveRequestModel)
  3. updateDriveRequest := new(client.UpdateDriveRequest)
  4. updateDriveRequest.SetDriveId("your drive id")
  5. updateDriveRequest.SetDescription("changed drive")
  6. response, err := ossClient.UpdateDrive(updateDriveModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 DeleteDriveExample() {
  2. deleteDriveModel := new(client.DeleteDriveRequestModel)
  3. deleteDriveRequest := new(client.DeleteDriveRequest)
  4. deleteDriveRequest.SetDriveId("your drive id ")
  5. response, err := ossClient.DeleteDrive(deleteDriveModel, runtime)
  6. if err != nil {
  7. fmt.Println(err)
  8. return
  9. }
  10. fmt.Println(response.Headers) // deleteDrive接口不返回body,若需获取RequestID,请从Headers中获取
  11. }

OSSPath Share 相关 API

创建Share

  • 以下代码用于创建Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档
  1. func getExpireTime() string {
  2. now := time.Now().UTC()
  3. h, _ := time.ParseDuration("1h")
  4. expireTime := now.Add(h).Format("2006-01-02T15:04:05Z")
  5. return expireTime
  6. }
  7. func CreateShareExample() {
  8. permissions := []*string{ptrString("FILE.LIST")}
  9. createShareModel := new(client.CreateShareRequestModel)
  10. createShareRequest := new(client.CreateShareRequest)
  11. createShareRequest.SetDriveId("xx")
  12. createShareRequest.SetOwner("xxx")
  13. createShareRequest.SetShareName("xxx")
  14. createShareRequest.SetPermissions(permissions)
  15. createShareRequest.SetShareFilePath("/xxx/")
  16. createShareRequest.SetExpiration(getExpireTime())
  17. createShareModel.SetBody(createShareRequest)
  18. response, err := ossClient.CreateShare(createShareModel, runtime)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. fmt.Println(response.Body)
  24. }
  • 返回结果
  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 GetShareExample() {
  2. getShareModel := new(client.GetShareRequestModel)
  3. getShareRequest := new(client.GetShareRequest)
  4. getShareRequest.SetShareId("xxx")
  5. getShareModel.SetBody(getShareRequest)
  6. response, err := ossClient.GetShare(getShareModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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 ListShareExample() {
  2. listShareModel := new(client.ListShareRequestModel)
  3. listShareRequest := new(client.ListShareRequest)
  4. listShareRequest.SetLimit(10)
  5. listShareRequest.SetOwner("xxx")
  6. listShareRequest.SetCreator("xxx")
  7. listShareModel.SetBody(listShareRequest)
  8. response, err := ossClient.ListShare(listShareModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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 UpdateShareExample() {
  2. updateShareModel := new(client.UpdateShareRequestModel)
  3. updateShareRequest := new(client.UpdateShareRequest)
  4. updateShareRequest.SetShareId("xxx")
  5. updateShareRequest.SetDescription("changed_share")
  6. updateShareModel.SetBody(updateShareRequest)
  7. response, err := ossClient.UpdateShare(updateShareModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 DeleteShareExample() {
  2. deleteShareModel := new(client.DeleteShareRequestModel)
  3. deleteShareRequest := new(client.DeleteShareRequest)
  4. deleteShareRequest.SetShareId("1")
  5. deleteShareModel.SetBody(deleteShareRequest)
  6. response, err := ossClient.DeleteShare(deleteShareModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Headers) // deleteShare接口不返回body,若需获取RequestID,请从Headers中获取
  12. }

OSSPath File相关 API

创建File

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

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

  1. func CreateFileExample() {
  2. createFileModel := new(client.CreateFileRequestModel)
  3. createFileRequest := new(client.OSSCreateFileRequest).SetDriveId("2201").
  4. SetName("b.txt").
  5. SetType("file").
  6. SetParentFilePath("/").
  7. SetContentType("text/plain")
  8. createFileModel.SetBody(createFileRequest)
  9. response, err := ossClient.CreateFile(createFileModel, runtime)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. fmt.Println(response.Body)
  15. }
  • 返回结果
  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 ListFileExample() {
  2. listFileModel := new(client.ListFileRequestModel)
  3. listFileRequest := new(client.OSSListFileRequest)
  4. listFileRequest.SetParentFilePath("/")
  5. listFileRequest.SetDriveId("xxx")
  6. listFileModel.SetBody(listFileRequest)
  7. response, err := ossClient.ListFile(listFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 GetFileExample() {
  2. getFileModel := new(client.GetFileRequestModel)
  3. getFileRequest := new(client.OSSGetFileRequest)
  4. getFileRequest.SetFilePath("xxx")
  5. getFileRequest.SetDriveId("xxx")
  6. getFileModel.SetBody(getFileRequest)
  7. response, err := ossClient.GetFile(getFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 MoveFileExample() {
  2. moveFileModel := new(client.MoveFileRequestModel)
  3. moveFileRequest := new(client.OSSMoveFileRequest)
  4. moveFileRequest.SetToParentFilePath("/")
  5. moveFileRequest.SetFilePath("/xxxx/")
  6. moveFileRequest.SetDriveId("xxx")
  7. moveFileModel.SetBody(moveFileRequest)
  8. response, err := ossClient.MoveFile(moveFileModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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 CopyFileExample() {
  2. copyFileModel := new(client.CopyFileRequestModel)
  3. copyFileRequest := new(client.OSSCopyFileRequest)
  4. copyFileRequest.SetToParentFilePath("/")
  5. copyFileRequest.SetFilePath("/xxxx/")
  6. copyFileRequest.SetDriveId("xxx")
  7. copyFileModel.SetBody(copyFileRequest)
  8. response, err := ossClient.CopyFile(copyFileModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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 GetUploadUrlExample() {
  2. var uploadInfo = new(client.UploadPartInfo)
  3. getFileUploadUrlModel := new(client.GetUploadUrlRequestModel)
  4. getFileUploadUrlRequest := new(client.OSSGetUploadUrlRequest)
  5. getFileUploadUrlRequest.SetUploadId("xxx")
  6. getFileUploadUrlRequest.SetFilePath("/xxxx/")
  7. getFileUploadUrlRequest.SetDriveId("xxx")
  8. getFileUploadUrlRequest.SetPartInfoList([]*client.UploadPartInfo{uploadInfo})
  9. getFileUploadUrlModel.SetBody(getFileUploadUrlRequest)
  10. response, err := ossClient.GetUploadUrl(getFileUploadUrlModel, runtime)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. fmt.Println(response.Body)
  16. }
  • 返回结果
  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 GetDownloadUrlExample() {
  2. getDownloadUrlModel := new(client.GetDownloadUrlRequestModel)
  3. getDownloadUrlRequest := new(client.OSSGetDownloadUrlRequest)
  4. getDownloadUrlRequest.SetFilePath("xxx")
  5. getDownloadUrlRequest.SetDriveId("xxx")
  6. getDownloadUrlRequest.SetExpireSec(3600)
  7. getDownloadUrlModel.SetBody(getDownloadUrlRequest)
  8. response, err := ossClient.GetDownloadUrl(getDownloadUrlModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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 CompleteFileExample() {
  2. completeFileModel := new(client.CompleteFileRequestModel)
  3. completeFileRequest := new(client.OSSCompleteFileRequest)
  4. completeFileRequest.SetDriveId("2201")
  5. completeFileRequest.SetFilePath("xxx")
  6. completeFileRequest.SetUploadId("xxx")
  7. completeFileRequest.SetPartInfoList([]*client.UploadPartInfo{})
  8. completeFileModel.SetBody(completeFileRequest)
  9. response, err := ossClient.CompleteFile(completeFileModel, runtime)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. fmt.Println(response.Body)
  15. }
  • 返回结果
  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 DeleteFileExample() {
  2. deleteFileModel := new(client.DeleteFileRequestModel)
  3. deleteFileRequest := new(client.OSSDeleteFileRequest)
  4. deleteFileRequest.SetDriveId("1")
  5. deleteFileRequest.SetFilePath("xxx")
  6. deleteFileModel.SetBody(deleteFileRequest)
  7. response, err := ossClient.DeleteFile(deleteFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Headers) // deleteFile接口不返回body,若需获取RequestID,请从Headers中获取
  13. }

列举Stores File

  • 以下代码用于列举Stores File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func ListStoreFilesExample() {
  2. listStoreFileModel := new(client.ListStorefileRequestModel)
  3. listStoreFileRequest := new(client.ListStoreFileRequest)
  4. listStoreFileRequest.SetStoreId("xxx")
  5. listStoreFileRequest.SetParentFilePath("xxx")
  6. listStoreFileModel.SetBody(listStoreFileRequest)
  7. response, err := ossClient.ListStorefile(listStoreFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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 CreateDriveExample() {
  2. createDriveModel := new(client.CreateDriveRequestModel)
  3. createDriveRequest := new(client.CreateDriveRequest)
  4. createDriveRequest.SetDriveType("normal")
  5. createDriveRequest.SetDriveName("your drive name")
  6. createDriveRequest.SetTotalSize(10240000)
  7. createDriveRequest.SetOwner("your accout id")
  8. createDriveRequest.SetDescription("test")
  9. createDriveModel.SetBody(createDriveRequest)
  10. resp, err := ossClient.CreateDrive(createDriveModel, runtime)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. fmt.Println(resp.Body)
  16. }
  • 返回结果
  1. {
  2. "requestId": "DFACCE19-F046-4739-AFBE-6ABBF0D2DA52",
  3. "domain_id": "dai****04",
  4. "drive_id": "1401"
  5. }

列举drive

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

CCPPath File 相关 API

创建File

  • 以下代码用于创建File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
  1. func CreateFileExample() {
  2. createFileModel := new(client.CreateFileRequestModel)
  3. createFileRequest := new(client.CCPCreateFileRequest).SetDriveId("2201").
  4. SetName("b.txt").
  5. SetType("file").
  6. SetParentFileId("/").
  7. SetContentType("text/plain")
  8. createFileModel.SetBody(createFileRequest)
  9. response, err := ccpClient.CreateFile(createFileModel, runtime)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. fmt.Println(response.Body)
  15. }
  • 返回结果
  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. listFileModel := new(client.ListFileRequestModel)
  3. listFileRequest := new(client.CCPListFileRequest)
  4. listFileRequest.SetParentFileId("/")
  5. listFileRequest.SetDriveId("xxx")
  6. listFileModel.SetBody(listFileRequest)
  7. response, err := ccpClient.ListFile(listFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. getFileModel := new(client.GetFileRequestModel)
  3. getFileRequest := new(client.CCPGetFileRequest)
  4. getFileRequest.SetFileId("xxx")
  5. getFileRequest.SetDriveId("xxx")
  6. getFileModel.SetBody(getFileRequest)
  7. response, err := ccpClient.GetFile(getFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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. moveFileModel := new(client.MoveFileRequestModel)
  3. moveFileRequest := new(client.CCPMoveFileRequest)
  4. moveFileRequest.SetToParentFileId("/")
  5. moveFileRequest.SetFileId("/xxxx/")
  6. moveFileRequest.SetDriveId("xxx")
  7. moveFileModel.SetBody(moveFileRequest)
  8. response, err := ccpClient.MoveFile(moveFileModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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. copyFileModel := new(client.CopyFileRequestModel)
  3. copyFileRequest := new(client.CCPCopyFileRequest)
  4. copyFileRequest.SetToParentFileId("/")
  5. copyFileRequest.SetFileId("/xxxx/")
  6. copyFileRequest.SetDriveId("xxx")
  7. copyFileModel.SetBody(copyFileRequest)
  8. response, err := ccpClient.CopyFile(copyFileModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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. getFileUploadUrlModel := new(client.GetUploadUrlRequestModel)
  4. getFileUploadUrlRequest := new(client.CCPGetUploadUrlRequest)
  5. getFileUploadUrlRequest.SetUploadId("xxx")
  6. getFileUploadUrlRequest.SetFileId("/xxxx/")
  7. getFileUploadUrlRequest.SetDriveId("xxx")
  8. getFileUploadUrlRequest.SetPartInfoList([]*client.UploadPartInfo{uploadInfo})
  9. getFileUploadUrlModel.SetBody(getFileUploadUrlRequest)
  10. response, err := ccpClient.GetUploadUrl(getFileUploadUrlModel, runtime)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. fmt.Println(response.Body)
  16. }
  • 返回结果
  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. getDownloadUrlModel := new(client.GetDownloadUrlRequestModel)
  3. getDownloadUrlRequest := new(client.CCPGetDownloadUrlRequest)
  4. getDownloadUrlRequest.SetFileId("xxx")
  5. getDownloadUrlRequest.SetDriveId("xxx")
  6. getDownloadUrlRequest.SetExpireSec(3600)
  7. getDownloadUrlModel.SetBody(getDownloadUrlRequest)
  8. response, err := ccpClient.GetDownloadUrl(getDownloadUrlModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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. completeFileModel := new(client.CompleteFileRequestModel)
  3. completeFileRequest := new(client.CCPCompleteFileRequest)
  4. completeFileRequest.SetDriveId("2201")
  5. completeFileRequest.SetFileId("xxx")
  6. completeFileRequest.SetUploadId("xxx")
  7. completeFileRequest.SetPartInfoList([]*client.UploadPartInfo{})
  8. completeFileModel.SetBody(completeFileRequest)
  9. response, err := ccpClient.CompleteFile(completeFileModel, runtime)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. fmt.Println(response.Body)
  15. }
  • 返回结果
  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. updateFileModel := new(client.UpdateFileRequestModel)
  3. updateFileRequest := new(client.CCPUpdateFileMetaRequest)
  4. updateFileRequest.SetDriveId("1")
  5. updateFileRequest.SetFileId("xxx")
  6. updateFileRequest.SetDescription("Changed File")
  7. updateFileModel.SetBody(updateFileRequest)
  8. response, err := ccpClient.UpdateFile(updateFileModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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 SearchFilExample() {
  2. searchFileModel := new(client.SearchFileRequestModel)
  3. searchFileRequest := new(client.CCPSearchFileRequest)
  4. searchFileRequest.SetDriveId("**")
  5. searchFileRequest.SetQuery(`name match "abc"`)
  6. searchFileModel.SetBody(searchFileRequest)
  7. response, err := ccpClient.SearchFile(searchFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Body)
  13. }
  • 返回结果
  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() {
  2. deleteFileModel := new(client.DeleteFileRequestModel)
  3. deleteFileRequest := new(client.CCPDeleteFileRequest)
  4. deleteFileRequest.SetDriveId("1")
  5. deleteFileRequest.SetFileId("xxx")
  6. deleteFileModel.SetBody(deleteFileRequest)
  7. response, err := ccpClient.DeleteFile(deleteFileModel, runtime)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(response.Headers) // deleteFile接口不返回body,若需获取RequestID,请从Headers中获取
  13. }

获取异步任务状态

  • 以下代码用于获取一部,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
  1. func GetAsyncTaskExample() {
  2. getAsyncTaskModel := new(client.GetAsyncTaskInfoRequestModel)
  3. getAsyncTaskRequest := new(client.CCPGetAsyncTaskRequest)
  4. getAsyncTaskRequest.SetAsyncTaskId("**")
  5. getAsyncTaskModel.SetBody(getAsyncTaskRequest)
  6. response, err := ccpClient.GetAsyncTaskInfo(getAsyncTaskModel, runtime)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(response.Body)
  12. }
  • 返回结果
  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"] = "**"
  5. subRequestBody["file_id"] = "***"
  6. sub := new(client.BatchSubRequest)
  7. sub.SetBody(subRequestBody)
  8. sub.SetUrl("/file/get")
  9. sub.SetMethod("POST")
  10. sub.SetId("uuid")
  11. batchModel := new(client.BatchOperationRequestModel)
  12. batchRequest := new(client.CCPBatchRequest)
  13. batchRequest.SetResource("file")
  14. batchRequest.SetRequests([]*client.BatchSubRequest{sub})
  15. batchModel.SetBody(batchRequest)
  16. response, err := ccpClient.BatchOperation(batchModel, runtime)
  17. if err != nil {
  18. fmt.Println(err)
  19. return
  20. }
  21. fmt.Println(response.Body)
  22. }
  • 返回结果
  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. listUploadedPartsModel := new(client.ListUploadedPartsRequestModel)
  3. listUploadedPartsRequest := new(client.CCPListUploadedPartRequest)
  4. listUploadedPartsRequest.SetDriveId("**")
  5. listUploadedPartsRequest.SetFileId("**")
  6. listUploadedPartsRequest.SetUploadId("***")
  7. listUploadedPartsModel.SetBody(listUploadedPartsRequest)
  8. response, err := ccpClient.ListUploadedParts(listUploadedPartsModel, runtime)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println(response.Body)
  14. }
  • 返回结果
  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. 首先,您需要开通内容协作平台(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/alibabacloud-go/ccppath-sdk/client"
  5. "net/http"
  6. "os"
  7. "strings"
  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 runtime = new(client.RuntimeOptions)
  15. var ccpClient, _ = client.NewClient(clientConf)
  16. func main() {
  17. createFile()
  18. }
  19. func createFile() {
  20. // create file
  21. fmt.Println("******************create file******************")
  22. createFileModel := new(client.CreateFileRequestModel)
  23. createFileRequest := new(client.CCPCreateFileRequest).
  24. SetDriveId("1").
  25. SetName("b.txt").
  26. SetType("file").
  27. SetParentFileId("root").
  28. SetContentType("text/plain")
  29. createFileModel.SetBody(createFileRequest)
  30. response, err := ccpClient.CreateFile(createFileModel, runtime)
  31. if err != nil {
  32. fmt.Println(err.Error())
  33. return
  34. }
  35. var uploadUrl, uploadId, FileId *string
  36. fmt.Println(response.Body)
  37. if response != nil {
  38. uploadUrl = response.Body.PartInfoList[0].UploadUrl
  39. uploadId = response.Body.UploadId
  40. FileId = response.Body.FileId
  41. // upload file
  42. // 此处可以使用os.Open()上传本地文件, 具体使用请参考os package。
  43. fmt.Println("******************upload file******************")
  44. content := strings.NewReader("12345678")
  45. req, _ := http.NewRequest("PUT", *uploadUrl, content)
  46. req.Header.Add("Content-Type", "")
  47. res, _ := http.DefaultClient.Do(req)
  48. Etag := res.Header.Get("ETag")
  49. uploadPartInfo := new(client.UploadPartInfo)
  50. uploadPartInfo.SetEtag(Etag)
  51. uploadPartInfo.SetPartNumber(1)
  52. // complete file
  53. fmt.Println("******************complete file******************")
  54. completeFileModel := new(client.CompleteFileRequestModel)
  55. completeFileRequest := new(client.CCPCompleteFileRequest).
  56. SetDriveId("1").
  57. SetFileId(*FileId).
  58. SetUploadId(*uploadId).
  59. SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
  60. completeFileModel.SetBody(completeFileRequest)
  61. completeResponse, completeErr := ccpClient.CompleteFile(completeFileModel, runtime)
  62. if completeErr != nil {
  63. fmt.Println(completeErr.Error())
  64. }
  65. if completeResponse != nil {
  66. fmt.Println(completeResponse.Body)
  67. }
  68. }
  69. }