数据地址

更新时间:

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

创建数据地址

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

重要

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.AddressDetail;
import com.aliyun.hcs_mgw20240626.models.CreateAddressInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAddressRequest;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	static String accessKeyToken = System.getenv("OSS_SESSION_TOKEN");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写数据地址名称。
		String addressName = "exampleaddress";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			config.setSecurityToken(accessKeyToken);
			Client client = new Client(config);
			AddressDetail detail = new AddressDetail();
			detail.addressType = "oss";
			// 以下参数请根据实际值填写。
			detail.bucket = "examplebucket";
			detail.prefix = "***/";
			detail.regionId = "oss-cn-beijing";
			detail.role = "rolename_xxxxx";
			CreateAddressRequest request = new CreateAddressRequest();
			CreateAddressInfo info = new CreateAddressInfo();
			info.name = addressName;
			info.setAddressDetail(detail);
			request.setImportAddress(info);
			client.createAddress(userId, request);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

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

重要

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.AddressDetail;
import com.aliyun.hcs_mgw20240626.models.CreateAddressInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAddressRequest;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写数据地址名称。
      String addressName = "exampleaddress";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         AddressDetail detail = new AddressDetail();
         detail.addressType = "ossinv";
         // 以下参数请根据实际值填写。
         detail.bucket = "examplebucket";
         detail.prefix = "***/";
         detail.regionId = "oss-cn-beijing";
         detail.role = "rolename_*******";
         detail.invLocation = "oss";
         detail.invAccessId = "***";
         detail.invAccessSecret = "******";
         detail.invBucket = "exampleinvbucket";
         detail.invRegionId = "oss-cn-beijing";
         detail.invRole = "invrolename_*******";
         detail.invPath = "dir/manifest.json";
         CreateAddressRequest request = new CreateAddressRequest();
         CreateAddressInfo info = new CreateAddressInfo();
         info.name = addressName;
         info.setAddressDetail(detail);
         request.setImportAddress(info);
         client.createAddress(userId, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.AddressDetail;
import com.aliyun.hcs_mgw20240626.models.CreateAddressInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAddressRequest;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写数据地址名称。
      String addressName = "exampleaddress";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         AddressDetail detail = new AddressDetail();
         detail.addressType = "s3";
         // 以下参数请根据实际值填写。
         detail.accessId = "***";
         detail.accessSecret = "******";
         detail.domain = "s3.ap-southeast-1.amazonaws.com";
         detail.bucket = "examplebucket";
         detail.prefix = "***/";
         CreateAddressRequest request = new CreateAddressRequest();
         CreateAddressInfo info = new CreateAddressInfo();
         info.name = addressName;
         info.setAddressDetail(detail);
         request.setImportAddress(info);
         client.createAddress(userId, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

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

重要

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.AddressDetail;
import com.aliyun.hcs_mgw20240626.models.CreateAddressInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAddressRequest;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写数据地址名称。
		String addressName = "exampleaddress";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			AddressDetail detail = new AddressDetail();
			detail.addressType = "http";
			// 以下参数请根据实际值填写。
			detail.invLocation = "oss";
			detail.invBucket = "exampleinvbucket";
			detail.invDomain = "oss-cn-beijing.aliyuncs.com";
			detail.invRole = "invrolename_*******";
			detail.invPath = "dir/manifest.json";
			CreateAddressRequest request = new CreateAddressRequest();
			CreateAddressInfo info = new CreateAddressInfo();
			info.name = addressName;
			info.setAddressDetail(detail);
			request.setImportAddress(info);
			client.createAddress(userId, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.AddressDetail;
import com.aliyun.hcs_mgw20240626.models.CreateAddressInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAddressRequest;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写数据地址名称。
		String addressName = "exampleaddress";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			AddressDetail detail = new AddressDetail();
			detail.addressType = "local";
			// 以下参数请根据实际值填写。
			detail.prefix = "/***/";
			CreateAddressRequest request = new CreateAddressRequest();
			CreateAddressInfo info = new CreateAddressInfo();
			info.name = addressName;
			info.setAddressDetail(detail);
			request.setImportAddress(info);
			client.createAddress(userId, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

获取数据地址详情

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.GetAddressResponse;
import com.google.gson.Gson;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写数据地址名称。
		String addressName = "exampleaddress";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			GetAddressResponse resp = client.getAddress(userId, addressName);
			System.out.println(new Gson().toJson(resp.getBody().getImportAddress()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

正常返回示例

{
  "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": "available",
    "VerifyResult": {
      "HttpCode": "400",
      "ErrorCode": "InvalidArgument",
      "ErrorMsg": "Invalid argument."
    }
  }
}

校验数据地址

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.VerifyAddressResponse;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写数据地址名称。
      String addressName = "exampleaddress";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         VerifyAddressResponse resp = client.verifyAddress(userId, addressName);
         // 仅当status 字段值为 available 时表示数据地址有效,其它任何值均表示无效。
         System.out.println(new Gson().toJson(resp.getBody().verifyAddressResponse.status));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回地址可用示例

{
  "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 sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.ListAddressRequest;
import com.aliyun.hcs_mgw20240626.models.ListAddressResponse;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         ListAddressRequest request = new ListAddressRequest();
         // 根据实际填写marker,count。
         String marker = "";
         int count = 1;
         request.setMarker(marker);
         request.setCount(count);
         ListAddressResponse resp = client.listAddress(userId, request);
         System.out.println(new Gson().toJson(resp.getBody().getImportAddressList()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回示例

{
  "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": "available",
        "VerifyResult": {
          "HttpCode": "400",
          "ErrorCode": "InvalidArgument",
          "ErrorMsg": "Invalid argument."
        }
      }
    ]
  }
}

更新数据地址

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

重要

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写数据地址名称。
      String addressName = "exampleaddress";
      // 填写代理名称列表,以,分隔, 不可为空。
      String agentList = "exampleagent1,exampleagent2,exampleagent3...";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         UpdateAddressRequest request = new UpdateAddressRequest();
         UpdateAddressInfo info = new UpdateAddressInfo();
         info.setAgentList(agentList);
         request.setImportAddress(info);
         client.updateAddress(userId, addressName, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

删除数据地址

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

package sample;

import com.aliyun.hcs_mgw20240626.Client;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   static String accessKeyToken = System.getenv("OSS_SESSION_TOKEN");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写数据地址名称。
      String addressName = "exampleaddress";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         config.setSecurityToken(accessKeyToken);
         Client client = new Client(config);
         client.deleteAddress(userId, addressName);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

后续步骤

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