You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过编程方式删除GCP私有服务连接下的Endpoint

GCP Private Service Connection Endpoint 删除代码示例(Java/C#)

Java 实现

首先引入GCP Service Directory的Java客户端依赖(Maven示例):

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-servicedirectory</artifactId>
    <version>2.21.0</version> <!-- 使用最新稳定版本 -->
</dependency>

删除Endpoint的代码示例:

import com.google.cloud.servicedirectory.v1.EndpointName;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import java.io.IOException;

public class DeleteEndpointExample {
    public static void main(String[] args) throws IOException {
        // 替换为你的实际资源参数
        String projectId = "your-project-id";
        String location = "your-region";
        String serviceConnectionName = "your-service-connection-name";
        String endpointId = "your-endpoint-id";

        // 构建Endpoint的完整资源名称
        EndpointName endpointName = EndpointName.of(projectId, location, serviceConnectionName, endpointId);

        try (RegistrationServiceClient client = RegistrationServiceClient.create()) {
            // 调用删除接口
            client.deleteEndpoint(endpointName);
            System.out.println("Endpoint 删除成功");
        } catch (Exception e) {
            System.err.println("删除失败: " + e.getMessage());
        }
    }
}

C# 实现

首先安装对应的NuGet包:

Install-Package Google.Cloud.ServiceDirectory.V1 -Version 2.21.0

删除Endpoint的代码示例:

using Google.Cloud.ServiceDirectory.V1;
using System;
using System.Threading.Tasks;

public class DeleteEndpointSample
{
    public static async Task Main(string[] args)
    {
        // 替换为你的实际资源参数
        string projectId = "your-project-id";
        string location = "your-region";
        string serviceConnectionName = "your-service-connection-name";
        string endpointId = "your-endpoint-id";

        // 构建Endpoint的完整资源名称
        EndpointName endpointName = EndpointName.FromProjectLocationServiceEndpoint(projectId, location, serviceConnectionName, endpointId);

        // 创建客户端实例
        RegistrationServiceClient client = await RegistrationServiceClient.CreateAsync();

        try
        {
            // 调用删除接口
            await client.DeleteEndpointAsync(endpointName);
            Console.WriteLine("Endpoint 删除成功");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"删除失败: {ex.Message}");
        }
    }
}

关键注意事项

  • 确保执行代码的服务账号拥有 servicedirectory.endpoints.delete 权限,可通过IAM角色roles/servicedirectory.editor或自定义角色授予
  • 确认Endpoint的资源名称参数完全正确,尤其是区域、服务连接名称和Endpoint ID,路径错误会导致404
  • 认证配置:本地运行时可设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向服务账号密钥文件;GCP内部环境(如GKE、Cloud Functions)会自动使用默认服务账号认证

内容的提问来源于stack exchange,提问作者JeevaRP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:33:15