数据地址
更新时间:
本文介绍如何使用SDK调用数据地址相关的方法。
创建数据地址
以下示例代码用于创建OSS类型的数据地址。
重要
创建OSS类型的数据地址前需要先进行角色配置,并获取到角色 (rolename)。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
address := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
addressType := "oss"
// 以下参数请根据实际值填写。
regionId := "oss-cn-beijing"
bucket := "examplebucket"
prefix := "***/"
roleName := "rolename_xxxxx"
detail := mgwpackage.AddressDetail{
AddressType: &addressType,
RegionId: ®ionId,
Bucket: &bucket,
Prefix: &prefix,
Role: &roleName,
}
_, err = client.CreateAddress(&userId, &mgwpackage.CreateAddressRequest{
ImportAddress: &mgwpackage.CreateAddressInfo{
Name: &address,
AddressDetail: &detail,
}})
if err != nil{
fmt.Errorf("create address failed, %v", err)
return
}
}
以下示例代码用于创建Oss Inventory类型的数据地址。
重要
创建Oss Inventory类型的数据地址前需要先进行角色配置, 并获取到角色和清单角色 (rolename 和 invrolename)。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
address := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
addressType := "ossinv"
// 以下参数请根据实际值填写。
prefix := "***/"
regionId := "oss-cn-beijing"
bucket := "examplebucket"
roleName := "rolename_xxxxx"
invLocation := "oss"
invBucket := "exampleinvbucket"
invRegionId := "oss-cn-beijing"
invRole := "invrolename_*******"
invPath := "dir/manifest.json"
detail := mgwpackage.AddressDetail{
AddressType: &addressType,
RegionId: ®ionId,
Bucket: &bucket,
Prefix: &prefix,
Role: &roleName,
InvLocation: &invLocation,
InvBucket: &invBucket,
InvRegionId: &invRegionId,
InvRole: &invRole,
invPath: &invPath,
}
_, err = client.CreateAddress(&userId, &mgwpackage.CreateAddressRequest{
ImportAddress: &mgwpackage.CreateAddressInfo{
Name: &address,
AddressDetail: &detail,
}})
if err != nil{
fmt.Errorf("create address failed, %v", err)
return
}
}
以下示例代码用于创建S3类型的数据地址。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
address := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
addressType := "s3"
// 以下参数请根据实际值填写。
accessId := "***"
accessSecret := "******"
domain := "s3.ap-southeast-1.amazonaws.com"
bucket := "examplebucket"
prefix := "***/"
detail := mgwpackage.AddressDetail{
AddressType: &addressType,
AccessId: &accessId,
AccessSecret: &accessSecret,
Domain: &domain,
Bucket: &bucket,
Prefix: &prefix,
}
_, err = client.CreateAddress(&userId, &mgwpackage.CreateAddressRequest{
ImportAddress: &mgwpackage.CreateAddressInfo{
Name: &address,
AddressDetail: &detail,
}})
if err != nil{
fmt.Errorf("create address failed, %v", err)
return
}
}
以下示例代码用于创建HTTP类型的数据地址。
重要
创建HTTP类型的数据地址前需要先进行角色配置, 并获取到清单角色 (invrolename)。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
address := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
addressType := "http"
// 以下参数请根据实际值填写。
invDomain := "oss-cn-beijing.aliyuncs.com"
invBucket := "exampleinvbucket"
invLocation := "oss"
invPath := "dir/manifest.json"
invRole:= "invrolename_*******"
detail := mgwpackage.AddressDetail{
AddressType: &addressType,
InvDomain: &invDomain,
InvLocation: &invLocation,
InvPath: &invPath,
InvRole: &invRole,
}
_, err = client.CreateAddress(&userId, &mgwpackage.CreateAddressRequest{
ImportAddress: &mgwpackage.CreateAddressInfo{
Name: &address,
AddressDetail: &detail,
}})
if err != nil{
fmt.Errorf("create address failed, %v", err)
return
}
}
以下示例代码用于创建LocalFS类型的数据地址。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
address := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
addressType := "local"
// 以下参数请根据实际值填写。
prefix := "/***/"
detail := mgwpackage.AddressDetail{
AddressType: &addressType,
Prefix: &prefix,
}
_, err = client.CreateAddress(&userId, &mgwpackage.CreateAddressRequest{
ImportAddress: &mgwpackage.CreateAddressInfo{
Name: &address,
AddressDetail: &detail,
}})
if err != nil{
fmt.Errorf("create address failed, %v", err)
return
}
}
获取数据地址详情
以下示例代码旨在获取数据地址的详细信息。
package main
import (
"encoding/json"
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
addressName := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
resp, err := client.GetAddress(&userId, &addressName)
if err != nil {
fmt.Errorf("get address failed, %v", err)
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
fmt.Println("covert failed:", err)
return
}
fmt.Println(string(jsonBytes))
}
正常返回示例
{
"ImportAddress": {
"Owner": "test_owner",
"Name": "test_name",
"AddressDetail": {
"AddressType": "ossinv",
"Bucket": "test_bucket",
"Domain": "test_domain",
"Prefix": "test_prefix",
"AccessId": "test_access_id",
"AccessSecret": "test_secret_key",
"Role": "test_role",
"InvDomain": "test_inv_domain",
"InvBucket": "test_inv_bucket",
"InvAccessId": "test_inv_access_id",
"InvAccessSecret": "test_inv_secret_key",
"InvPath": "manifest.json",
"InvRole": "test_inv_role",
"InvLocation": "oss",
"AgentList": "agent1,agent2",
"RegionId": "test_region_id",
"InvRegionId": "test_inv_region_id"
},
"CreateTime": "2024-05-01T12:00:00.000Z",
"ModifyTime": "2024-05-01T12:00:00.000Z",
"VerifyTime": "2024-05-01T12:00:00.000Z",
"Version": "test_id",
"Tags": "K1:V1,K2:V2",
"Status": "avaliable",
"VerifyResult": {
"HttpCode": "400",
"ErrorCode": "InvalidArgument",
"ErrorMsg": "Invalid argument."
}
}
}
校验数据地址
以下示例代码用于验证指定的数据地址是否可用。
package main
import (
"encoding/json"
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
addressName := "exampleaddress"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
resp, err := client.VerifyAddress(&userId, &addressName)
if err != nil {
fmt.Errorf("verify address failed, %v", err)
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
fmt.Println("covert failed:", err)
return
}
// 仅当status 字段值为 available 时表示数据地址有效,其它任何值均表示无效。
fmt.Println(string(jsonBytes))
}
正常返回地址可用示例
{
"VerifyAddressResponse": {
"Status": "available",
"VerifyTime": "2024-05-01T12:00:00.000Z",
"ErrorCode": "",
"ErrorMessage": ""
}
}
正常返回地址不可用示例
{
"VerifyAddressResponse": {
"Status": "unavailable",
"VerifyTime": "2024-05-01T12:00:00.000Z",
"ErrorCode": "400",
"ErrorMessage": "Invalid argument."
}
}
列举数据地址
以下示例代码用于列举账号下所有数据地址信息。
package main
import (
"encoding/json"
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
// 根据实际填写marker,count。
count := int32(1)
marker := ""
resp, err := client.ListAddress(&userId, &mgwpackage.ListAddressRequest{
Count: &count, Marker: &marker,
})
if err != nil {
fmt.Errorf("list address failed, %v", err)
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
fmt.Println("covert failed,", err)
return
}
// 仅当status 字段值为 available 时表示数据地址有效,其它任何值均表示无效。
fmt.Println(string(jsonBytes))
}
正常返回示例
{
"ImportAddressList": {
"Truncated": true,
"NextMarker": "test_marker",
"ImportAddress": [
{
"Owner": "test_owner",
"Name": "test_name",
"AddressDetail": {
"AddressType": "ossinv",
"Bucket": "test_bucket",
"Domain": "test_domain",
"Prefix": "test_prefix",
"AccessId": "test_access_id",
"AccessSecret": "test_secret_key",
"Role": "test_role",
"InvDomain": "test_inv_domain",
"InvBucket": "test_inv_bucket",
"InvAccessId": "test_inv_access_id",
"InvAccessSecret": "test_inv_secret_key",
"InvPath": "manifest.json",
"InvRole": "test_inv_role",
"InvLocation": "oss",
"AgentList": "agent1,agent2",
"RegionId": "test_region_id",
"InvRegionId": "test_inv_region_id"
},
"CreateTime": "2024-05-01T12:00:00.000Z",
"ModifyTime": "2024-05-01T12:00:00.000Z",
"VerifyTime": "2024-05-01T12:00:00.000Z",
"Version": "test_id",
"Tags": "K1:V1,K2:V2",
"Status": "avaliable",
"VerifyResult": {
"HttpCode": "400",
"ErrorCode": "InvalidArgument",
"ErrorMsg": "Invalid argument."
}
}
]
}
}
更新数据地址
以下示例代码用于更新指定数据地址关联的代理。
重要
至少需要填写一个代理名称,否则更新失败。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
addressName := "exampleaddress";
// 填写代理名称列表,以,分隔, 不可为空。
agentList := "exampleagent1,exampleagent2,exampleagent3...";
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
_, err = client.UpdateAddress(&userId, &addressName, &mgwpackage.UpdateAddressRequest{
ImportAddress: &mgwpackage.UpdateAddressInfo{
AgentList : &agentList,
},
})
if err != nil {
fmt.Errorf("update address failed, %v", err)
}
}
删除数据地址
以下示例代码删除指定数据地址。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
// 填写数据地址名称。
addressName := "exampleaddress";
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
_, err = client.DeleteAddress(&userId, &addressName)
if err != nil {
fmt.Errorf("delete address failed, %v", err)
}
}
后续步骤
数据地址创建后,您可以选择继续执行创建任务操作,详情请参见 任务。
文档内容是否对您有帮助?