You need to enable JavaScript to run this app.
导航

为安全组添加出方向规则

最近更新时间2023.06.09 11:20:32

首次发布时间2023.06.09 11:20:32

本文介绍如何为安全组添加一条出方向规则。

示例代码

/**
* Example Code generated by Beijing Volcanoengine Technology.
*/
package com.volcengine.vpc.examples;

import com.volcengine.ApiClient;
import com.volcengine.ApiException;
import com.volcengine.sign.Credentials;

import com.volcengine.vpc.VpcApi;
import com.volcengine.vpc.model.*;


public class TestAuthorizeSecurityGroupEgress {
    public static void main(String[] args) throws Exception {
        // 设置您的ak
        String ak = "Your AK";
        // 设置您的sk
        String sk = "Your SK";
        // 设置您要访问的地域
        String region = "Your Region";

				// 初始化apiclient
        ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak, sk))
                .setRegion(region);

        VpcApi api = new VpcApi(apiClient);

        // 为安全组添加一条出方向规则
        AuthorizeSecurityGroupEgressRequest authorizeSecurityGroupEgressRequest = new AuthorizeSecurityGroupEgressRequest();
        authorizeSecurityGroupEgressRequest.setCidrIp("10.XX.XX.0/8"); // 目的端IPv4地址段
        authorizeSecurityGroupEgressRequest.setPolicy("accept"); // 访问策略
        authorizeSecurityGroupEgressRequest.setPortEnd(22);      // 端口范围结束值
        authorizeSecurityGroupEgressRequest.setPortStart(22);    // 端口范围开始值
        authorizeSecurityGroupEgressRequest.setPriority(1);      // 安全组规则优先级
        authorizeSecurityGroupEgressRequest.setProtocol("tcp");  // 传输层协议
        authorizeSecurityGroupEgressRequest.setSecurityGroupId("sg-bp67acfmxazb4p****"); // 安全组的ID

        // 发起请求并处理返回或异常
        try {
            AuthorizeSecurityGroupEgressResponse response = api.authorizeSecurityGroupEgress(authorizeSecurityGroupEgressRequest);
            System.out.println(response);
        } catch (ApiException e) {
            System.out.println(e.getResponseBody());
        }
    }
}

相关文档