Golang SDK 简介
下载地址
安装步骤
安装Go开发环境
您可以从 golang官方网站 下载并按说明安装 golang 开发环境。
安装CCP Go SDK
安装完 Go开发环境后,您需要将下面的依赖加入go.mod
CCP Path
module github.com/aliyun/aliyun-ccp/ccppath-sdk/go
go 1.12
require (
github.com/alibabacloud-go/tea v0.0.0-20191011054747-c132d91088dc
github.com/aliyun/aliyun-ccp/baseclient/go master
)
OSS Path
module github.com/aliyun/aliyun-ccp/osspath-sdk/go
go 1.12
require (
github.com/alibabacloud-go/tea v0.0.0-20191011054747-c132d91088dc
github.com/aliyun/aliyun-ccp/baseclient/go master
)
初始化Client
AK & SK 初始化客户端
var ossConfig = new(client.Config).
SetDomainId("your domain id").
SetProtocol("https").
SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
// initial runtimeOptions
var runtime = &client.RuntimeOptions{}
// initial akClient
var ossClient, _ = client.NewClient(ossConfig)
ClientID & Client Secret 初始化客户端
var accessTokenConfig = new(client.Config).
SetDomainId("your domain id").
SetProtocol("https").
SetClientId(os.Getenv("CLIENT_ID")).
SetClientSecret(os.Getenv("CLIENT_SECRET")).
SetAccessToken("ejy***************1asf").
SetRefreshToken("O00*********MHb").
SetExpireTime("2019-11-05T03:29:09Z")
// initial runtimeOptions
var runtime = &client.RuntimeOptions{}
// initial tokenClient
var tokenClient, _ = client.NewClient(accessTokenConfig)
构造请求
Account相关API
说明
客户端需要初始化如下
// {domainId} 替换成自己的domainId
var authConfig = new(client.Config).
SetEndpoint("{ domainId }.auth.alicloudccp.com").
SetProtocol("https").
SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
// initial runtimeOptions
var runtime = &client.RuntimeOptions{}
// initial authClient
var authClient, _ = client.NewClient(authConfig)
获取图片验证码
- 以下代码用于获取图片验证码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
// 获取图片验证码
func getCaptcha() {
getCaptchaRequest := new(client.GetCaptchaRequest).
SetAppId(appId)
response, err := authClient.GetCaptcha(getCaptchaRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"captcha": "iVBORw0KGgoAAAANSUhEUgAAAFAAAAAaCAIAAACvsEzwAAABQ0lEQVR42u3Yyw3CMAwA0Nw4IjZhAW7cYQQGQEKVGJDNSqRKUfAvjp1CWlr5QtWGPmInLmH8syNs4A28MvDwfGxg+ri8riDk64+3cx4dpbTSjMExuHuBtom5STJWg7E/3p4CgNODTh/zK2OcDncQXYPxmZwNppScYQzmzPthFwM8ajppWaU15iI4HwekMZnPQFgFtmldYG7pwmaugA3gaXyz9mMfLprJRUsotiqwXMO50KP1got7EpneXA0Lo/0SzFUv1oIBsVm/SiekUwtbS9lM1rAfrK9hv9YFFswph7lFmxNy5rlmuHZb0piFdcsAblnDBvA0gUqzclsqgp1m7+uhskvDvaet0/KbG7wP683F1lKzLbVpLZ2tudmsPDibwRxw4pnNi/gvgdiWnOzlgZ0zVnvXl3+jIH/9+syh55yc42HeYJ3SI2GdJBoAAAAASUVORK5CYII=",
"captcha_format": "png",
"captcha_id": "ff5db7ecd9988915ee56bebb52ed055aqSD2Cqce646"
}
获取短信验证码
- 以下代码用于获取短信验证码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
// 获取短信验证码
func getMnsCode() {
getMnsCodeRequest := new(client.MobileSendSmsCodeRequest).
SetAppId(appId).
SetPhoneNumber("1****5").
SetType("login")
response, err := authClient.MobileSendSmsCode(getMnsCodeRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "4CF58428-912F-4F4F-BE1D-4C06FE559097",
"sms_code_id": "16340***************Slzf"
}
验证手机号是否注册
- 以下代码用于验证手机号是否注册,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func checkMobileIsExist() {
checkMobileIsExistRequest := new(client.MobileCheckExistRequest).
SetAppId(appId).
SetPhoneNumber("13297039225")
response, err := authClient.CheckExist(checkMobileIsExistRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "0BF59873-1433-4D62-A465-93B7250BD698",
"is_exist": true,
"phone_number": "132*****225"
}
手机号注册
- 以下代码用于手机号注册,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
// 手机号注册
func register() {
registerRequest := new(client.MobileRegisterRequest).
SetAppId(appId).
SetPhoneNumber("132*****25").
SetSmsCodeId("************").
SetSmsCode("********")
response, err := authClient.Register(registerRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "525FCA71-4C4A-4C35-AC72-24A4DC181646",
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX********P0ER7E-ASrJI",
"avatar": null,
"default_drive_id": null,
"exist_link": [],
"expire_time": "2019-11-04T11:50:06Z",
"expires_in": 7200,
"need_link": false,
"nick_name": "132***225",
"refresh_token": "A2lpoPv********1XRhl78",
"role": "admin",
"state": null,
"token_type": "Bearer",
"user_id": "6c23c98d36dc4b44b56b7d8b3f9f5211",
"user_name": "132***225"
}
手机号短信登录
- 以下代码用于手机号短信登录,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func mnsCodeLogin() {
mnsCodeLoginRequest := new(client.MobileLoginRequest).
SetAppId(appId).
SetSmsCode("****").
SetSmsCodeId("******").
SetPhoneNumber("132********25")
response, err := authClient.Login(mnsCodeLoginRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
设置登录密码
- 以下代码用于设置登录密码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func setPassword() {
setPasswordRequest := new(client.DefaultSetPasswordRequest).
SetAppId(appId).
SetNewPassword("").
SetState("") //state 注册的时候返回
err := authClient.SetPassword(setPasswordRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
}
密码登录
- 以下代码用于密码登录,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func passwordLogin() {
passwordLoginRequest := new(client.MobileLoginRequest).
SetAppId(appId).
SetPhoneNumber("132******5").
SetPassword("********")
response, err := authClient.Login(passwordLoginRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
修改登录密码
- 以下代码用于修改登录密码,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func changePassword() {
changePasswordRequest := new(client.DefaultChangePasswordRequest).
SetAppId(appId).
SetPhoneNumber("132****225").
SetNewPassword("***********").
SetSmsCodeId("").
SetSmsCode("")
err := authClient.ChangePassword(changePasswordRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
通过刷新令牌获取访问令牌
- 以下代码用于通过刷新令牌获取访问令牌,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getTokenByRefreshToken() {
getTokenRequest := new(client.TokenRequest).
SetAppId(appId).
SetGrantType("refresh_token").
SetRefreshToken("")
response, err := authClient.Token(getTokenRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
通过账号获取访问令牌
此接口需要ak,sk调用, 请使用ak, sk初始化客户端
- 以下代码用于通过账号获取访问令牌,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getAccessTokenByLinkInfo() {
getTokenByLinkInfoReq := new(client.GetAccessTokenByLinkInfoRequest).
SetType("mobile").
SetIdentity("13*****225")
response, err := authClient.GetAccessTokenByLinkInfo(getTokenByLinkInfoReq, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
获取用户绑定信息
此接口需要ak,sk调用, 请使用ak, sk初始化客户端
- 以下代码用于获取用户绑定信息,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getLinkInfoByUserId() {
getInfoRequest := new(client.GetLinkInfoByUserIDRequest).
SetUserId("test_user1")
response, err := authClient.GetLinkInfoByUserId(getInfoRequest, runtime)
if err != nil {
fmt.Println(err.Error())go
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "793162FD-F1F2-4658-9DF2-B14213987B9F",
"items": [
{
"requestId": null,
"authentication_type": "ding",
"created_at": 1571915575496,
"domain_id": "dai****5",
"identity": "g3QxL*****QiEiE",
"last_login_time": 1571915575496,
"status": "normal",
"user_id": "5190***********bf5"
}
]
}
获取用户认证方式
此接口需要ak,sk调用, 请使用ak, sk初始化客户端
- 以下代码用于获取用户认证方式,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getLinkInfo() {
getLinkInfoRequest := new(client.GetByLinkInfoRequest).
SetIdentity("1243125125").
SetType("mobile")
response, err := authClient.GetLinkInfo(getLinkInfoRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "BFB8B3C4-7727-401D-9B65-495337FF5ADD",
"authentication_type": "mobile",
"created_at": 1571905906341,
"domain_id": "daily1405",
"identity": "13******225",
"last_login_time": 1571905906341,
"status": "normal",
"user_id": "6c23c9***************5211"
}
绑定用户认证方式
此接口需要ak,sk调用, 请使用ak, sk初始化客户端
- 以下代码用于绑定用户认证方式,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func accountLink() {
accountLinkRequest := new(client.AccountLinkRequest).
SetType("toabao").
SetIdentity("1233445").
SetUserId("12344556").
SetStatus("wait_link")
response, err := authClient.Link(accountLinkRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "8A62ABC5-92D4-4CF3-A22F-D6FD908D24C6",
"access_token": "eyJhbGciOiJSUzI1NiIsIn****LvWYRJiG29hUiUhjZvD2vS0EdItpSiXMucOBk3J5z1EuN9aeycHaTcA",
"avatar": null,
"default_drive_id": null,
"exist_link": [
{
"identity": "15******29",
"type": "mobile"
}
],
"expire_time": null,
"expires_in": 300,
"need_link": true,
"nick_name": null,
"refresh_token": null,
"role": null,
"state": null,
"token_type": "Bearer",
"user_id": null,
"user_name": null
}
取消绑定关系
- 以下代码用于取消绑定关系,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func cancelLink() {
cancelLinkRequest := new(client.CancelLinkRequest).
SetTemporaryToken("")
response, err := authClient.CancelLink(cancelLinkRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
确定绑定关系
此接口需要accessToken调用, 请使用clientId,ClientSecret初始化客户端
- 以下代码用于确定绑定关系,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func confirmLink() {
confirmLinkRequest := new(client.ConfirmLinkRequest).
SetTemporaryToken("")
response, err := authClient.ConfirmLink(confirmLinkRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果参见手机号注册的返回结果
User 相关 API
创建User
- 以下代码用于创建User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func createUser() {
createUserRequest := new(client.CreateUserRequest).
SetUserId("test_user").
SetRole("user").
SetUserName("test_user")
response, err := ccpClient.CreateUser(createUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "A2808B75-251C-426E-9727-116081F41AC3",
"avatar": null,
"created_at": 1573089204640,
"default_drive_id": "1301",
"description": null,
"domain_id": "daily1404",
"email": null,
"nick_name": null,
"phone": null,
"role": "user",
"status": "enabled",
"updated_at": 0,
"user_id": "test_user",
"user_name": "test_user"
}
获取User
- 以下代码用于获取User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getUser() {
getUserRequest := new(client.GetUserRequest).
SetUserId("test_user")
response, err := ossClient.GetUser(getUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "2678D6FC-1090-4724-A070-AA0B57811581",
"avatar": null,
"created_at": 1573089204640,
"default_drive_id": "1301",
"description": null,
"domain_id": "daily1404",
"email": null,
"nick_name": null,
"phone": null,
"role": "user",
"status": "enabled",
"updated_at": 1573089204655,
"user_id": "test_user",
"user_name": "test_user"
}
列举User
- 以下代码用于列举User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func listUser() {
listUserRequest := new(client.ListUserRequest).
SetLimit(10)
response, err := ossClient.ListUsers(listUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
- 返回结果
{
"requestId": "8E399439-B244-434B-A212-3BF702891692",
"items": [
{
"requestId": null,
"avatar": "",
"created_at": 1572340950210,
"default_drive_id": "901",
"description": "",
"domain_id": "daily1404",
"email": "",
"nick_name": "root",
"phone": "",
"role": "user",
"status": "enabled",
"updated_at": 1572340950225,
"user_id": "10c58ee7455a418a90f4454cc85a372e",
"user_name": "root"
},
{
"requestId": null,
"avatar": "",
"created_at": 1573089204640,
"default_drive_id": "1301",
"description": "",
"domain_id": "daily1404",
"email": "",
"nick_name": "",
"phone": "",
"role": "user",
"status": "enabled",
"updated_at": 1573089204655,
"user_id": "test_user",
"user_name": "test_user"
}
],
"next_marker": null
}
更新User
- 以下代码用于更新User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func updateUser() {
updateUserRequest := new(client.UpdateUserRequest).
SetUserId("test_user").
SetDescription("changed_user")
response, err := ossClient.UpdateUser(updateUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "B81E6A92-53C7-4715-AD9F-D3DB10154D78",
"avatar": null,
"created_at": 1573089204640,
"default_drive_id": "1301",
"description": "changed_user",
"domain_id": "daily1404",
"email": null,
"nick_name": null,
"phone": null,
"role": "user",
"status": "enabled",
"updated_at": 1573089205333,
"user_id": "test_user",
"user_name": "test_user"
}
搜索User
- 以下代码用于搜索User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func searchUser() {
searchUserRequest := new(client.SearchUserRequest).
SetUserName("test_user")
response, err := ossClient.SearchUser(searchUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "C292180C-9DC9-4EC1-9C64-AD0FE215E159",
"items": [
{
"requestId": null,
"avatar": "",
"created_at": 1573089204640,
"default_drive_id": "1301",
"description": "changed_user",
"domain_id": "daily1404",
"email": "",
"nick_name": "",
"phone": "",
"role": "user",
"status": "enabled",
"updated_at": 1573089576836,
"user_id": "test_user",
"user_name": "test_user"
}
],
"next_marker": null
}
删除User
- 以下代码用于删除User,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func deleteUser() {
deleteUserRequest := new(client.DeleteUserRequest).
SetUserId("test_user")
err := ossClient.DeleteUser(deleteUserRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
OSSPath Drive 相关API
说明
- OSSPath 创建drive 需要先拿到
store_id
,需要先调用AdminListStores
的接口。
创建drive
- 以下代码用于创建drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
// list stores
func listStores() string {
var storeId string
listStoresRequest := new(client.AdminListStoresRequest)
response, err := tokenClient.AdminListStores(listStoresRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
if response != nil {
fmt.Println(response)
storeId = *response.Items[0].StoreId
}
return storeId
}
// create drive
func createDrive() string {
var driveId string
storeId := listStores()
if storeId != "" {
createDriveRequest := new(client.CreateDriveRequest).
SetDriveName("测试").
SetOwner("5***************bf5").
SetTotalSize(100000000).
SetDescription("测试").
SetDriveType("normal").
SetRelativePath("/ldh/").
SetStoreId(storeId)
response, err := ossClient.CreateDrive(createDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
if response != nil {
fmt.Println(response)
driveId = *response.DriveId
}
}
return driveId
}
- 返回结果
// list stores result
{
"requestId": "FD9BA144-FBE6-47DC-8C24-834F68FA0881",
"items": [
{
"accelerate_endpoint": "",
"base_path": "",
"bucket": "ccp-daily-test",
"customized_endpoint": "",
"endpoint": "https://oss-cn-hangzhou.aliyuncs.com",
"internal_endpoint": "",
"ownership": "custom",
"policy": "",
"role_arn": "",
"store_id": "90fb***************1d9b",
"type": "oss"
}
]
}
// create drive result
{
"requestId": "78A0512B-DBF5-40A2-8147-7F0EE414B1C0",
"domain_id": "daily1405",
"drive_id": "2501"
}
查询drive
- 以下代码用于查询drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func getDrive(driveId string) {
getDriveRequest := new(client.GetDriveRequest).
SetDriveId(driveId)
response, err := ossClient.GetDrive(getDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "414050C9-C05B-4FFA-BB46-9E891CE3153C",
"creator": null,
"description": "测试",
"domain_id": "d***",
"drive_id": "2501",
"drive_name": "测试",
"drive_type": "normal",
"owner": "5190*************dbf5",
"relative_path": "/ldh/",
"status": "enabled",
"store_id": "90f**************d9b",
"total_size": 100000000,
"used_size": 0
}
列举drive
- 以下代码用于列举drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func listDrive() {
listDriveRequest := new(client.ListDriveRequest).
SetOwner("51901a***************dbf5").
SetLimit(10)
response, err := ossClient.ListDrives(listDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "6DB486F0-CDF5-46CD-B5B6-7B3D819174E8",
"items": [
{
"requestId": null,
"creator": "",
"description": "test_drive",
"domain_id": "daily****",
"drive_id": "2201",
"drive_name": "test_drive",
"drive_type": "normal",
"owner": "51901a4********2edbf5",
"relative_path": "/test_drive/",
"status": "enabled",
"store_id": "90fba*******d9b",
"total_size": 100000000,
"used_size": 0
}
],
"next_marker": null
}
更新drive
- 以下代码用于更新drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func updateDrive(driveId string) {
updateDriveRequest := new(client.UpdateDriveRequest).
SetDriveId(driveId).
SetDescription("changed_drive")
response, err := ossClient.UpdateDrive(updateDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "251478CD-3AB2-4DA0-B0DE-A6D3BF3933D1",
"creator": null,
"description": "changed_drive",
"domain_id": "daily1405",
"drive_id": "2501",
"drive_name": "测试",
"drive_type": "normal",
"owner": "51901a43476d4a5a98141ca8c12edbf5",
"relative_path": "/ldh/",
"status": "enabled",
"store_id": "90fba27e9c40452d91d83b204aee1d9b",
"total_size": 100000000,
"used_size": 0
}
删除drive
- 以下代码用于删除drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func deleteDrive(driveId string) {
deleteDriveRequest := new(client.DeleteDriveRequest).
SetDriveId(driveId)
err := ossClient.DeleteDrive(deleteDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
OSSPath Share 相关 API
创建Share
- 以下代码用于创建Share,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档
func createShare(driveId string) string {
permissions := []*string{ptrString("FILE.LIST")}
var shareId string
createShareRequest := new(client.CreateShareRequest).
SetOwner("51901a4*******************f5").
SetShareName("test_share").
SetDriveId(driveId).
SetPermissions(permissions).
SetShareFilePath("/test_share/").
SetExpiration(getExpireTime())
response, err := tokenClient.CreateShare(createShareRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
if response != nil {
fmt.Println(response)
shareId = *response.ShareId
}
return shareId
}
func ptrString(str string) *string {
return &str
}
// get expire time
func getExpireTime() string {
now := time.Now().UTC()
h, _ := time.ParseDuration("1h")
expireTime := now.Add(h).Format("2006-01-02T15:04:05Z")
return expireTime
}
- 返回结果
{
"requestId": "50774D14-AFA6-4997-84EF-F4C951056276",
"domain_id": "dai***1",
"share_id": "04902f6d-cc3a-4afd-af6d-d34f430db840"
}
查询Share
- 以下代码用于查询Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func getShare(shareId string) {
getShareRequest := new(client.GetShareRequest).
SetShareId(shareId)
response, err := tokenClient.GetShare(getShareRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "429C2A82-1327-460C-829A-4434729F72CD",
"created_at": "2019-11-07T01:45:31.609Z",
"creator": "6c23*******f5211",
"description": null,
"domain_id": "dai***05",
"drive_id": "2201",
"expiration": "2019-11-07T02:45:31Z",
"expired": false,
"owner": "51901a4******edbf5",
"permissions": [
"FILE.LIST"
],
"share_file_path": "/test_share/",
"share_id": "04902f6d-cc3a-4afd-af6d-d34f430db840",
"share_name": "test_share",
"status": "enabled",
"updated_at": "2019-11-07T01:45:31.609Z"
}
列举Share
- 以下代码用于列举Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func listShare() {
listShareRequest := new(client.ListShareRequest).
SetOwner("51901******5").
SetCreator("61adf*****d353s").SetLimit(10)
response, err := tokenClient.ListShare(listShareRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "0CBADEC0-9739-4A1E-89B5-70CF5AEE7E32",
"items": [
{
"requestId": null,
"created_at": "2019-11-07T01:49:39.215Z",
"creator": "6c23c98d*******9f5211",
"description": "",
"domain_id": "da*****5",
"drive_id": "2201",
"expiration": "2019-11-07T02:49:39Z",
"expired": false,
"owner": "51901a4*******12edbf5",
"permissions": [
"FILE.LIST"
],
"share_file_path": "/test_share/",
"share_id": "0e5ee6e1-9640-4c22-93f4-b3362d355b1e",
"share_name": "test_share",
"status": "enabled",
"updated_at": "2019-11-07T01:49:39.215Z"
}
],
"next_marker": null
}
更新Share
- 以下代码用于更新Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func updateShare(shareId string) {
updateShareReuqest := new(client.UpdateShareRequest).
SetShareId(shareId).
SetDescription("changed_share")
response, err := tokenClient.UpdateShare(updateShareReuqest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "3F484A79-0578-4683-97D6-FBFC5D747506",
"created_at": "2019-11-07T01:49:39.215Z",
"creator": "6c23c9*********211",
"description": "changed_share",
"domain_id": "dai****1",
"drive_id": "2201",
"expiration": "2019-11-07T02:49:39Z",
"expired": false,
"owner": "51901a****************bf5",
"permissions": [],
"share_file_path": "/test_share/",
"share_id": "0e5ee6e1-9640-4c22-93f4-b3362d355b1e",
"share_name": "test_share",
"status": "enabled",
"updated_at": "2019-11-07T01:49:39.215Z"
}
删除Share
- 以下代码用于删除Share,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func deleteShare(shareId string) {
deleteShareRequest := new(client.DeleteShareRequest).
SetShareId(shareId)
err := tokenClient.DeleteShare(deleteShareRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
OSSPath File相关 API
创建File
以下代码用于创建File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
说明: 此处创建File 还需要上传文件到OSS, 具体操作详见最佳实践。
func createFile() {
createFileRequest := new(client.OSSCreateFileRequest).SetDriveId("2201").
SetName("b.txt").
SetType("file").
SetParentFilePath("/").
SetContentType("text/plain")
response, err := ossClient.CreateFile(createFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "8249D5E8-3D07-4D56-AE69-7E337C6B4659",
"domain_id": "dai***05",
"drive_id": "2201",
"file_path": "/a.jpg",
"part_info_list": [
{
"etag": null,
"part_number": 1,
"part_size": null,
"upload_url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?E911F0FBD3"
}
],
"share_id": null,
"type": "file",
"upload_id": "35EE8647048349D09A950DE911F0FBD3"
}
列举File
- 以下代码用于列举File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func listFile() {
listFileRequest := new(client.OSSListFileRequest).
SetDriveId("2201").
SetParentFilePath("/xxx/").
SetLimit(10)
response, err := ossClient.ListFile(listFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "8BA9F8EA-E1A3-4E58-90A8-339336EC8A64",
"items": [
{
"requestId": null,
"content_hash": null,
"content_hash_name": null,
"content_type": "",
"crc64_hash": null,
"created_at": null,
"description": null,
"domain_id": "dai***05",
"download_url": null,
"drive_id": "2201",
"file_extension": "",
"file_path": "/test_folder/",
"name": "test_folder",
"parent_file_path": "/",
"share_id": null,
"size": null,
"status": "available",
"thumbnail": null,
"trashed_at": null,
"type": "folder",
"updated_at": null,
"upload_id": null,
"url": null
},
{
"requestId": null,
"content_hash": null,
"content_hash_name": null,
"content_type": "",
"crc64_hash": null,
"created_at": null,
"description": null,
"domain_id": "daily1405",
"download_url": "https://ccp-da*********",
"drive_id": "2201",
"file_extension": "jpg",
"file_path": "/a.jpg",
"name": "a.jpg",
"parent_file_path": "/",
"share_id": null,
"size": 3448250,
"status": "available",
"thumbnail": "https://ccp-daily-t******0",
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T06:49:08Z",
"upload_id": null,
"url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/te***%3D"
}
],
"next_marker": null
}
查询File
- 以下代码用于查询File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getFile() {
getFileRequest := new(client.OSSGetFileRequest).
SetDriveId("2201").
SetFilePath("/xxxx/")
response, err := ossClient.GetFile(getFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "7D044402-4A39-40B6-8A21-5D1A2741F5FC",
"content_hash": null,
"content_hash_name": null,
"content_type": "image/jpg",
"crc64_hash": null,
"created_at": null,
"description": null,
"domain_id": "daily1405",
"download_url": "https://ccp-d*********eg%3D",
"drive_id": "2201",
"file_extension": null,
"file_path": "/a.jpg",
"name": "a.jpg",
"parent_file_path": "/",
"share_id": null,
"size": 3448250,
"status": "available",
"thumbnail": null,
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T06:49:08Z",
"upload_id": null,
"url": "https://ccp-daily-***QQkX8PWQ0JFdcQfhvY0WfEv2P0JdFAz93aYgXDZn0AjAdL1JNz3G6BGcjaaBOmrsERm9J1NnFeg%3D"
}
移动File
- 以下代码用于移动File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func moveFile() {
moveFileRequest := new(client.OSSMoveFileRequest).
SetDriveId("2201").
SetFilePath("/xxx/").
SetToParentFilePath("/yyyy/")
response, err := ossClient.MoveFile(moveFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "675E40E6-1BFF-4A53-8A81-B300F03102C4",
"async_task_id": null,
"domain_id": "daily1405",
"drive_id": "2201",
"file_path": "/test_folder/a.jpg",
"share_id": null
}
复制File
- 以下代码用于复制File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func copyFile() {
copyFileRequest := new(client.OSSCopyFileRequest).
SetDriveId("2201").
SetFilePath("/xxx/").
SetToParentFilePath("/yyy/")
response, err := ossClient.CopyFile(copyFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "205CB077-F075-43D2-A181-4114587B4694",
"async_task_id": null,
"domain_id": "daily1405",
"drive_id": "2201",
"file_path": "/a.jpg",
"share_id": null
}
获取File上传地址
- 以下代码用于获取File上传地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getUploadUrl() {
var uploadInfo = new(client.UploadPartInfo)
getUploadUrlrequest := new(client.OSSGetUploadUrlRequest).
SetDriveId("").
SetPartInfoList([]*client.UploadPartInfo{uploadInfo}).
SetUploadId("").
SetFilePath("")
response, err := ossClient.GetUploadUrl(getUploadUrlrequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "0A433A26-D29E-4038-ACEB-1359615C3C58",
"create_at": "2019-11-07T06:49:07.304Z",
"domain_id": "daily1405",
"drive_id": "2201",
"file_path": "/a.jpg",
"part_info_list": [
{
"etag": null,
"part_number": 1,
"part_size": null,
"upload_url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?Expires=1573112947\u0026OSSAc****ploadId=35EE8647048349D09A950DE911F0FBD3"
}
],
"upload_id": "35EE8647048349D09A950DE911F0FBD3"
}
获取File下载地址
- 以下代码用于获取File下载地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getDownloadUrl() {
getDownloadUrlRequest := new(client.OSSGetDownloadUrlRequest).
SetDriveId("2201").
SetFilePath("/xxx").
SetExpireSec(3600)
response, err := ossClient.GetDownloadUrl(getDownloadUrlRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "ECDF23DD-7EFC-4C09-A89B-4747D08212CA",
"expiration": "2019-11-07T07:04:08.488Z",
"method": "GET",
"url": "https://ccp-daily-test.oss-cn-hangzhou.aliyuncs.com/test_drive%2Fa.jpg?Expires=1573110248\u0026OSSAcce****NnFeg%3D"
}
Complete File
- 以下代码用于Complete File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func completeFile() {
completeFileRequest := new(client.OSSCompleteFileRequest).
SetDriveId("2201").
SetFilePath(*filePath).
SetUploadId(*uploadId).
SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
completeResponse, completeErr := ossClient.CompleteFile(completeFileRequest, runtime)
if completeErr != nil {
fmt.Println(completeErr.Error())
}
fmt.Println(completeResponse)
}
- 返回结果
{
"requestId": "F89A859B-6FC3-4791-B7A2-BB2F0AB78709",
"content_hash": null,
"content_hash_name": null,
"content_type": "image/jpg",
"crc64_hash": "1864830531212108369",
"created_at": null,
"description": null,
"domain_id": "daily1405",
"download_url": null,
"drive_id": "2201",
"file_extension": null,
"file_path": "/a.jpg",
"name": "a.jpg",
"parent_file_path": "/",
"share_id": null,
"size": 0,
"status": "uploading",
"thumbnail": null,
"trashed_at": null,
"type": "file",
"updated_at": null,
"upload_id": "35EE8647048349D09A950DE911F0FBD3",
"url": null,
"crc": "1864830531212108369"
}
删除File
- 以下代码用于删除File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func deleteFile() {
deleteFileRequest := new(client.OSSDeleteFileRequest).SetDriveId("1").
SetFilePath("xxx")
err := ossClient.DeleteFile(deleteFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
列举Stores File
- 以下代码用于列举Stores File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func listStoreFiles() {
lisStoreFileReq := new(client.ListStoreFileRequest).
SetStoreId("90fba27e********4aee1d9b")
res, err := tokenClient.ListStorefile(lisStoreFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(res)
}
- 返回结果
{
"requestId": "1536E018-5F86-4927-91E2-89171EDBF792",
"items": [
{
"domain_id": "",
"name": "bky",
"parent_file_path": "/",
"store_id": "90fba2**************ee1d9b",
"type": "folder"
},
{
"domain_id": "",
"name": "superadmin",
"parent_file_path": "/",
"store_id": "90fba2**************ee1d9b",
"type": "folder"
},
{
"domain_id": "",
"name": "test",
"parent_file_path": "/",
"store_id": "90fba2**************ee1d9b",
"type": "folder"
},
{
"domain_id": "",
"name": "test_drive",
"parent_file_path": "/",
"store_id": "90fba2**************ee1d9b",
"type": "folder"
}
],
"next_marker": null
}
CCPPath Drive 相关API
创建drive
- 以下代码用于创建drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func createDrive() string {
var driveId string
createDriveRequest := new(client.CreateDriveRequest).
SetDriveName("测试").
SetOwner("superadmin").
SetTotalSize(100000000).
SetDescription("测试").
SetDriveType("normal")
response, err := ccpClient.CreateDrive(createDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
if response != nil {
fmt.Println(response)
driveId = *response.DriveId
}
return driveId
}
- 返回结果
{
"requestId": "DFACCE19-F046-4739-AFBE-6ABBF0D2DA52",
"domain_id": "dai****04",
"drive_id": "1401"
}
列举drive
- 以下代码用于列举drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func listDrive() {
listDriveRequest := new(client.ListDriveRequest).
SetOwner("superadmin").
SetLimit(10)
response, err := ccpClient.ListDrives(listDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "F84761E7-03BD-4DEF-875E-7809C1578DD7",
"items": [
{
"requestId": null,
"creator": "System",
"description": "Created by system",
"domain_id": "daily1404",
"drive_id": "1",
"drive_name": "Default",
"drive_type": "normal",
"owner": "superadmin",
"relative_path": "",
"status": "enabled",
"store_id": "55ff60f************46904",
"total_size": 1073741824,
"used_size": 5212716
}
],
"next_marker": null
}
查询drive
- 以下代码用于查询drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getDrive(driveId string) {
getDriveRequest := new(client.GetDriveRequest).
SetDriveId(driveId)
response, err := ccpClient.GetDrive(getDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "91F9CCE0-2576-4D85-925A-630B1013B98D",
"creator": null,
"description": "测试",
"domain_id": "dai***1",
"drive_id": "1401",
"drive_name": "测试",
"drive_type": "normal",
"owner": "superadmin",
"relative_path": null,
"status": "enabled",
"store_id": "55ff6************904",
"total_size": 100000000,
"used_size": 0
}
更新drive
- 以下代码用于更新drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func updateDrive(driveId string) {
updateDriveRequest := new(client.UpdateDriveRequest).
SetDriveId(driveId).
SetDescription("changed_drive")
response, err := ccpClient.UpdateDrive(updateDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response)
}
- 返回结果
{
"requestId": "B9B9D80D-932B-4BE2-A106-F2DBC8AEFD3B",
"creator": null,
"description": "changed_drive",
"domain_id": "daily1404",
"drive_id": "1401",
"drive_name": "测试",
"drive_type": "normal",
"owner": "superadmin",
"relative_path": null,
"status": "enabled",
"store_id": "55ff60f************946904",
"total_size": 100000000,
"used_size": 0
}
删除drive
- 以下代码用于删除drive,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func deleteDrive(driveId string) {
deleteDriveRequest := new(client.DeleteDriveRequest).
SetDriveId(driveId)
err := ccpClient.DeleteDrive(deleteDriveRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
// 此接口不返回Body
}
CCPPath File 相关 API
创建File
- 以下代码用于创建File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func createFile() {
createFileRequest := new(client.CCPCreateFileRequest).
SetDriveId("1").
SetName("b.txt").
SetType("file").
SetParentFileId("root").
SetContentType("text/plain")
response, err := ccpClient.CreateFile(createFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
if response != nil {
fmt.Println(response)
}
}
- 返回结果
{
"requestId": "1087FD69-4F94-424C-890C-024212DAFB5D",
"domain_id": "dai***s04",
"drive_id": "1",
"file_id": "5dc38ecb4e247b6d547b4335971642f2152fb560",
"parent_file_id": "root",
"part_info_list": [
{
"etag": null,
"part_number": 1,
"part_size": null,
"upload_url": "https://ccp-daily1404-hz-1571887934.oss-cn-***A4FCFA"
}
],
"rapid_upload": false,
"type": "file",
"upload_id": "6057AF147DA741C29424D537BCA4FCFA"
}
列举File
- 以下代码用于列举File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func listFile() {
listFileReq := new(client.CCPListFileRequest).
SetDriveId("1").
SetParentFileId("root").
SetLimit(10)
listFileRes, err := ccpClient.ListFile(listFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(listFileRes)
}
- 返回结果
{
"requestId": "9427C190-0258-4D76-950E-E3A3BAF1DA00",
"items": [
{
"requestId": null,
"category": "",
"content_hash": null,
"content_hash_name": null,
"content_type": "",
"crc64_hash": null,
"created_at": "2019-11-07T08:09:52.833Z",
"description": null,
"domain_id": "daily1404",
"download_url": null,
"drive_id": "1",
"file_extension": "",
"file_id": "5dc3d150a1571d350f12492096bca248a9f47336",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "test_folder",
"parent_file_id": "root",
"size": null,
"starred": false,
"status": "available",
"thumbnail": null,
"trashed_at": null,
"type": "folder",
"updated_at": "2019-11-07T08:09:52.833Z",
"upload_id": null,
"url": null
},
{
"requestId": null,
"category": "image",
"content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
"content_hash_name": "sha1",
"content_type": "",
"crc64_hash": null,
"created_at": "2019-11-07T08:09:52.877Z",
"description": null,
"domain_id": "daily1404",
"download_url": "https://ccp-d***jpg",
"drive_id": "1",
"file_extension": "jpg",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "a.jpg",
"parent_file_id": "root",
"size": 3448250,
"starred": false,
"status": "available",
"thumbnail": "https://cc****w_128%2Climit_0",
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T08:09:54.877Z",
"upload_id": null,
"url": "https://ccp-dail**8eyId=LTAIi5kx6X0mwR8k\u0026Signature=WhbAImqKrMmXgJyBzNZ4EWGcBEY%3D"
}
],
"next_marker": null
}
查询File
- 以下代码用于查询File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func searcFile() {
searchFileReq := new(client.CCPSearchFileRequest).
SetDriveId("1").
SetLimit(10).
SetOrderBy("type DESC").
SetQuery("file_extension in ['txt']")
searchFileRes, err := ccpClient.SearchFile(searchFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(searchFileRes)
}
- 返回结果
{
"requestId": "2D44D3FB-97A1-4D78-AD56-B68FE3EF5845",
"category": "image",
"content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
"content_hash_name": "sha1",
"content_type": "image/jpg",
"crc64_hash": "1864830531212108369",
"created_at": "2019-11-07T08:09:52.877Z",
"description": "changed_file",
"domain_id": "daily1404",
"download_url": "https://ccp-daily1******u0026response-content-disposition=attachment%3Bfilename%3Da.jpg",
"drive_id": "1",
"file_extension": "jpg",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "a.jpg",
"parent_file_id": "root",
"size": 3448250,
"starred": false,
"status": "available",
"thumbnail": "https://ccp-daily1404-h***28%2Climit_0",
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T08:09:54.877Z",
"upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
"url": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db***3D"
}
移动File
- 以下代码用于移动File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func moveFile() {
moveFileRequest := new(client.CCPMoveFileRequest).
SetFileId(FileId).
SetDriveId("1").
SetToParentFileId(foderId)
moveRes, err := ccpClient.MoveFile(moveFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(moveRes)
}
- 返回结果
{
"requestId": "BBF646CE-73DF-4A82-A637-5F419E9D220D",
"async_task_id": null,
"domain_id": "daily1404",
"drive_id": "1",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568"
}
复制File
- 以下代码用于复制File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func copyFile() {
copyFileReq := new(client.CCPCopyFileRequest).
SetFileId(FileId).
SetDriveId("1").
SetToParentFileId("root")
copyRes, err := ccpClient.CopyFile(copyFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(copyRes)
}
- 返回结果
{
"requestId": "2913878A-663E-4B6B-AD9D-53406AAC7DAB",
"async_task_id": null,
"domain_id": "daily1404",
"drive_id": "1",
"file_id": "5dc3d15d9d751fdd49d44924b98baf430e84e612"
}
获取File上传地址
- 以下代码用于获取File上传地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getUploadUrl() {
uploadPart := new(client.UploadPartInfo)
uploadPart.SetPartNumber(1)
getUploadUrlReq := new(client.CCPGetUploadUrlRequest).
SetUploadId(uploadId).
SetDriveId("1").
SetFileId(FileId).
SetPartInfoList([]*client.UploadPartInfo{uploadPart})
uploadurlResponse, err := ccpClient.GetUploadUrl(getUploadUrlReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(uploadurlResponse)
}
- 返回结果
{
"requestId": "DECE883C-F54C-4B6E-B860-EB02D84BEA53",
"create_at": "2019-11-07T08:09:53.098Z",
"domain_id": "daily1404",
"drive_id": "1",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"part_info_list": [
{
"etag": null,
"part_number": 1,
"part_size": null,
"upload_url": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5***0026uploadId=CF977B42DE8D4CA38F4EB8334F0C759A"
}
],
"upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A"
}
获取File下载地址
- 以下代码用于获取File下载地址,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func getDownloadUrl() {
getDownloadRequest := new(client.CCPGetDownloadUrlRequest).
SetDriveId("1").
SetFileId(FileId)
getdownloadResponse, err := ccpClient.GetDownloadUrl(getDownloadRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(getdownloadResponse)
}
- 返回结果
{
"requestId": "BC2B0E86-798A-4537-B621-AD91ECF91F9C",
"expiration": "2019-11-07T08:24:54.437Z",
"method": "GET",
"url": "https://ccp-d*****3Da.jpg"
}
Complete File
- 以下代码用于Complete File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func completeFile() {
uploadPartInfo := new(client.UploadPartInfo)
uploadPartInfo.SetEtag(Etag)
uploadPartInfo.SetPartNumber(1)
// complete file
completeFileRequest := new(client.CCPCompleteFileRequest).
SetDriveId("1").
SetFileId(*FileId).
SetUploadId(*uploadId).
SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
completeResponse, err := ccpClient.CompleteFile(completeFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(completeResponse)
}
- 返回结果
{
"requestId": "77F6CF69-3458-4A2F-9823-F3ED8F919723",
"category": "image",
"content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
"content_hash_name": "sha1",
"content_type": "image/jpg",
"crc64_hash": "1864830531212108369",
"created_at": "2019-11-07T08:09:52.877Z",
"description": null,
"domain_id": "daily1404",
"download_url": null,
"drive_id": "1",
"file_extension": "jpg",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "a.jpg",
"parent_file_id": "root",
"size": 3448250,
"starred": false,
"status": "available",
"thumbnail": null,
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T08:09:54.877Z",
"upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
"url": null,
"crc": null
}
更新File
- 以下代码用于更新File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func updateFile() {
updateFileReq := new(client.CCPUpdateFileMetaRequest).
SetFileId(FileId).
SetDriveId("1").
SetDescription("changed_file")
updateFileRes, err := ccpClient.UpdateFile(updateFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(updateFileRes)
}
- 返回结果
{
"requestId": "5619DE43-D03F-451A-A299-C982C9CE1B3B",
"category": "image",
"content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
"content_hash_name": "sha1",
"content_type": "image/jpg",
"crc64_hash": "1864830531212108369",
"created_at": "2019-11-07T08:09:52.877Z",
"description": "changed_file",
"domain_id": "daily1404",
"download_url": "https://ccp****sponse-content-disposition=attachment%3Bfilename%3Da.jpg",
"drive_id": "1",
"file_extension": "jpg",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "a.jpg",
"parent_file_id": "root",
"size": 3448250,
"starred": false,
"status": "available",
"thumbnail": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db80819d*****%2Climit_0",
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T08:09:54.877Z",
"upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
"url": "https://ccp-daily1*******%3D"
}
搜索File
- 以下代码用于搜索File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档。
func searchFile() {
searchFileReq := new(client.CCPSearchFileRequest).
SetDriveId("1").
SetLimit(10).
SetOrderBy("type DESC").
SetQuery("file_extension in ['jpg']")
searchFileRes, err := ccpClient.SearchFile(searchFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(searchFileRes)
}
- 返回结果
{
"requestId": "5396BEA0-7F5C-4C3D-B37F-B1675377A825",
"items": [
{
"requestId": "5619DE43-D03F-451A-A299-C982C9CE1B3B",
"category": "image",
"content_hash": "CB1CFD826047EA301C84BBA1964DE573C57383E3",
"content_hash_name": "sha1",
"content_type": "image/jpg",
"crc64_hash": "1864830531212108369",
"created_at": "2019-11-07T08:09:52.877Z",
"description": "changed_file",
"domain_id": "daily1404",
"download_url": "https://ccp****sponse-content-disposition=attachment%3Bfilename%3Da.jpg",
"drive_id": "1",
"file_extension": "jpg",
"file_id": "5dc3d150b89a2ee80ac74bc08d18ba5ec1a86568",
"hidden": false,
"image_media_metadata": null,
"labels": null,
"meta": null,
"name": "a.jpg",
"parent_file_id": "root",
"size": 3448250,
"starred": false,
"status": "available",
"thumbnail": "https://ccp-daily1404-hz-1571887934.oss-cn-hangzhou.aliyuncs.com/5db80819d*****%2Climit_0",
"trashed_at": null,
"type": "file",
"updated_at": "2019-11-07T08:09:54.877Z",
"upload_id": "CF977B42DE8D4CA38F4EB8334F0C759A",
"url": "https://ccp-daily1*******%3D"
}
],
"next_marker": null
}
删除File
- 以下代码用于删除File,此处的示例目的是展示关键参数 ,其他参数请查看官方文档
func deleteFile() {
deleteFileReq := new(client.CCPDeleteFileRequest).
SetDriveId("1").
SetFileId(*FileId)
deleteFileRes, err := ccpClient.DeleteFile(deleteFileReq, runtime)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(deleteFileRes)
// 此接口不返回Body
}
附
创建RAM子用户(获取AK,SK)
创建APP(获取ClientID, ClientSecret)
- 首先,您需要开通内容协作平台(CCP)服务。如果没有开通,请到产品详情页面开通 。
- 您需要在CCP官网控制台创建一个域(Domain) 。详见创建CCPath域实例和创建OSSPath域实例。
- 创建APP,选择类型为”Web服务应用”。确定APP的访问Scope: 支持的Scope列表, 这个Scope要在用户授权页面展示。创建完成,可以得到APP ID(ClientID) 和 Secret(ClientSecret)。这个是授权认证的凭证,Secret要注意保密不要泄露。
CCPPath 上传文件
package main
import (
"fmt"
"github.com/aliyun/aliyun-ccp/ccppath-sdk/go/client"
"net/http"
"strings"
)
var clientConf = new(client.Config).
SetDomainId("your domain id").
SetProtocol("https").
SetAccessKeyId(os.Getenv("ACCESS_KEY_ID")).
SetAccessKeySecret(os.Getenv("ACCESS_KEY_SECRET"))
var runtime = new(client.RuntimeOptions)
var ccpClient, _ = client.NewClient(clientConf)
func main() {
createFile()
}
func createFile() {
// create file
fmt.Println("******************create file******************")
createFileRequest := new(client.CCPCreateFileRequest).
SetDriveId("2201").
SetName("b.txt").
SetType("file").
SetParentFileId("root").
SetContentType("text/plain")
response, err := ccpClient.CreateFile(createFileRequest, runtime)
if err != nil {
fmt.Println(err.Error())
return
}
var uploadUrl, uploadId, FileId *string
fmt.Println(response)
if response != nil {
uploadUrl = response.PartInfoList[0].UploadUrl
uploadId = response.UploadId
FileId = response.FileId
// upload file
// 此处可以使用os.Open()上传本地文件, 具体使用请参考os package。
fmt.Println("******************upload file******************")
content := strings.NewReader("12345678")
req, _ := http.NewRequest("PUT", *uploadUrl, content)
req.Header.Add("Content-Type", "")
res, _ := http.DefaultClient.Do(req)
Etag := res.Header.Get("ETag")
uploadPartInfo := new(client.UploadPartInfo)
uploadPartInfo.SetEtag(Etag)
uploadPartInfo.SetPartNumber(1)
// complete file
fmt.Println("******************complete file******************")
completeFileRequest := new(client.CCPCompleteFileRequest).
SetDriveId("2201").
SetFileId(*FileId).
SetUploadId(*uploadId).
SetPartInfoList([]*client.UploadPartInfo{uploadPartInfo})
completeResponse, completeErr := ccpClient.CompleteFile(completeFileRequest, runtime)
if completeErr != nil {
fmt.Println(completeErr.Error())
}
fmt.Println(completeResponse)
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论