数据地址

更新时间:

本文介绍如何使用SDK调用数据地址相关的方法。

创建数据地址

以下示例代码用于创建OSS类型的数据地址。

重要

创建OSS类型的数据地址前需要先进行角色配置,并获取到角色(rolename)。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAddressRequest, CreateAddressInfo, AddressDetail
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        # 填写数据地址名称。
        address_name = "exampleaddress"
        address_type = "oss"
        # 以下参数请根据实际值填写。
        region_id = "oss-cn-beijing"
        bucket = "examplebucket"
        prefix = "***/"
        role_name = "rolename_xxxxx"

        client.create_address(userid, CreateAddressRequest(CreateAddressInfo(
            name=address_name,
            address_detail=AddressDetail(
                address_type=address_type,
                bucket=bucket,
                region_id=region_id,
                prefix=prefix,
                role=role_name,
            )
        )))
    except Exception as e:
        print(e)

以下示例代码用于创建Oss Inventory类型的数据地址。

重要

创建Oss Inventory类型的数据地址前需要先进行角色配置, 并获取到角色和清单角色 (rolename 和 invrolename)。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAddressRequest, CreateAddressInfo, AddressDetail
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        address_type = "ossinv"
        # 以下参数请根据实际值填写。
        prefix = "***/"
        region_id = "oss-cn-beijing"
        bucket = "examplebucket"
        role_name = "rolename_xxxxx"
        inv_location = "oss"
        inv_bucket = "exampleinvbucket"
        inv_region_id = "oss-cn-beijing"
        inv_role = "invrolename_*******"
        inv_path = "***/manifest.json"

        client.create_address(userid, CreateAddressRequest(CreateAddressInfo(
            name=address_name,
            address_detail=AddressDetail(
                address_type=address_type,
                bucket=bucket,
                region_id=region_id,
                prefix=prefix,
                role=role_name,
                inv_role=inv_role,
                inv_location=inv_location,
                inv_bucket=inv_bucket,
                inv_path=inv_path,
                inv_region_id=inv_region_id
            )
        )))
    except Exception as e:
        print(e)

以下示例代码用于创建S3类型的数据地址。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAddressRequest, CreateAddressInfo, AddressDetail
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        address_type = "s3"
        # 以下参数请根据实际值填写。
        access_id = "***"
        access_secret = "******"
        domain = "s3.ap-southeast-1.amazonaws.com"
        bucket = "examplebucket"
        prefix = "***/"

        client.create_address(userid, CreateAddressRequest(CreateAddressInfo(
            name=address_name,
            address_detail=AddressDetail(
                address_type=address_type,
                access_id=access_id,
                access_secret=access_secret,
                bucket=bucket,
                domain=domain,
                prefix=prefix
            )
        )))
    except Exception as e:
        print(e)

以下示例代码用于创建HTTP类型的数据地址。

重要

创建HTTP类型的数据地址前需要先进行角色配置,并获取到清单角色 (invrolename)。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAddressRequest, CreateAddressInfo, AddressDetail
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        address_type = "http"
        # 以下参数请根据实际值填写。
        inv_domain = "oss-cn-beijing.aliyuncs.com"
        inv_bucket = "exampleinvbucket"
        inv_location = "oss"
        inv_path = "dir/manifest.json"
        inv_role = "invrolename_*******"

        client.create_address(userid, CreateAddressRequest(CreateAddressInfo(
            name=address_name,
            address_detail=AddressDetail(
                address_type=address_type,
                inv_domain=inv_domain,
                inv_location=inv_location,
                inv_path=inv_path,
                inv_role=inv_role,
                inv_bucket=inv_bucket
            )
        )))
    except Exception as e:
        print(e)

以下示例代码用于创建LocalFS类型的数据地址。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAddressRequest, CreateAddressInfo, AddressDetail
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        address_type = "local"
        # 以下参数请根据实际值填写。
        prefix = "/***/"

        client.create_address(userid, CreateAddressRequest(CreateAddressInfo(
            name=address_name,
            address_detail=AddressDetail(
                address_type=address_type,
                prefix=prefix
            )
        )))
    except Exception as e:
        print(e)

获取数据地址详情

以下示例代码用于获取数据地址详情信息。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        response = client.get_address(userid, address_name)
        print(response.body.import_address)
    except Exception as e:
        print(e)

正常返回示例

{
  "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."
    }
  }
}

校验数据地址

以下示例代码用于验证指定的数据地址是否可用。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        response = client.verify_address(userid, address_name)
        print(response.body)
    except Exception as e:
        print(e)
               

正常返回地址可用示例

{
  "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."
  }
}

列举数据地址

以下示例代码用于列举账号下所有数据地址信息。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import ListAddressRequest
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        # 根据实际填写marker, count。
        count = 1
        marker = ""
        response = client.list_address(userid, ListAddressRequest(
            marker=marker,
            count=count,
        ))
        for import_address in response.body.import_address_list.import_address:
            print(import_address)
    except Exception as e:
        print(e)

正常返回示例

{
  "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."
        }
      }
    ]
  }
}

更新数据地址

以下示例代码用于更新指定数据地址关联的代理。

重要

至少需要填写一个代理名称,否则更新失败。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import UpdateAddressRequest, UpdateAddressInfo
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        # 填写代理名称列表,以,分隔, 不可为空。
        agent_list = "exampleagent1,exampleagent2"
        client.update_address(userid, address_name, UpdateAddressRequest(UpdateAddressInfo(
            agent_list=agent_list
        )))
    except Exception as e:
        print(e)

删除数据地址

以下示例代码删除指定数据地址。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        address_name = "exampleaddress"
        client.delete_address(userid, address_name)
    except Exception as e:
        print(e)        

后续步骤

数据地址创建完成后,您可以选择继续执行创建任务操作。详情请参见任务