百度智能云

All Product Document

          Elastic IP

          Initialization

          Confirm Endpoint

          When confirming the Endpoint configured when using SDK, you can read the part in API reference regarding API Service Domain Name, and understand Endpoint related concept. Baidu AI Cloud enables multi-region support currently, please see the description of Region Selection.

          The corresponding information is:

          Access region Corresponding Endpoint
          Beijing eip.bj.baidubce.com
          Guangzhou eip.gz.baidubce.com
          Suzhou eip.su.baidubce.com
          HongKong eip.hkg.baidubce.com
          WuHan eip.fwh.baidubce.com
          BaoDing eip.bd.baidubce.com

          Get the Key

          To use Baidu AI Cloud EIP, you need to have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK is assigned by the system to users, both of which are strings, used to identify users, and perform signature authentication for accessing EIP. You can obtain and understand your AK/SK information through the following steps: Register Baidu AI Cloud Account Create AK/SK

          Create EipClient/EipGroupClient

          EipClient/EipGroupClient is the Python client of Eip service, which provides a series of methods for the developer to interact with Eip service.

          Create EipClient/EipGroupClient/EipBpClient with AK/SK

          1.Before creating EipClient/EipGroupClient, you need to create a configuration file to configure EipClient/EipGroupClient/EipBpClient, and in the following, this configuration is named as eip_sample_conf.py, with specific configuration information as follows.

          #!/usr/bin/env python
          #coding=utf-8
          
          # Import Python standard log module 
          import logging
          
          # Import EIP configuration management module and security authentication module from Python SDK 
          from baidubce.bce_client_configuration import BceClientConfiguration
          from baidubce.auth.bce_credentials import BceCredentials
          
          # Set Host, Access Key ID and Secret Access Key of EipClient 
          eip_host = "eip.bj.baidubce.com"
          access_key_id = "AK"
          secret_access_key = "SK"
          
          # Set handle and log level of log file 
          logger = logging.getLogger('baidubce.http.bce_http_client')
          fh = logging.FileHandler("sample.log")
          fh.setLevel(logging.DEBUG)
          
          # Set output sequence, structure and contents of log file. 
          formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
          fh.setFormatter(formatter)
          logger.setLevel(logging.DEBUG)
          logger.addHandler(fh)
          
          # Create BceClientConfiguration 
          config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint = eip_host)

          Note: For the log file, Logging has the following levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.

          In the codes above, ACCESS_KEY_ID corresponds to "Access Key ID" in the console, SECRET_ACCESS_KEY corresponds to "Access Key Secret" in the console, for the way of obtaining, please see "Operation Guideline Manage ACCESSKEY".

          For the method above, you need to specify the domain name of EIP service by yourself, and you can specify it by assigning a value to variable eip_host. If not configured, it is not needed to introduce endpoint parameter, and the default is Beijing region http://eip.bj.baidubce.com.

          2.Before completing the configuration above, create an EipClient/EipGroupClient by reference to the following codes.

          # Import EipClientt configuration file 
          import eip_sample_conf 
          		
          # Import Eip related module 
          from baidubce import exception
          from baidubce.services import eip
          from baidubce.services.eip.eip_client import EipClient
          from baidubce.services.eip.eip_group_client import EipGroupClient
          	
          # Create EipClient 
          eip_client = EipClient(eip_sample_conf.config)
          # Create EipGroupClient 
          eip_group_client = EipGroupClient(eip_sample_conf.config)
          # Create EipBpClient
          eip_bp_client = EipBpClient(eip_sample_conf.config)

          Create EipClient/EipGroupClient/EipBpClient with STS

          EIP can implement temporary authorized access by a third party through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. With STS, you can issue a third-party user with a custom time-based and privileged access credential. The third-party user can use the access credential to directly call API or SDK of Baidu AI Cloud to access its resources.

          To access to EIP via STS, you need to apply for an authentication string via client of STS, for the application method, please see Introduction to use of Baidu AI Cloud STS.

          1.Obtain a temporary token, create configuration file "sts_sample_conf.py" to configure EipClient/EipGroupClient.

          import logging
          from baidubce.bce_client_configuration import BceClientConfiguration
          from baidubce.auth.bce_credentials import BceCredentials
          
          sts_host = "STS_HOST"
          access_key_id = "AK"
          secret_access_key = "SK"
          
          logger = logging.getLogger('baidubce.services.sts.stsclient')
          fh = logging.FileHandler("sample.log")
          fh.setLevel(logging.DEBUG)
          
          formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
          fh.setFormatter(formatter)
          logger.setLevel(logging.DEBUG)
          logger.addHandler(fh)
          
          config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint=sts_host)

          Note: When you configure client with STS currently, wherever the endpoint in the corresponding EIP service is, endpoint needs to be configured as http://sts.bj.baidubce.com.

          2.Create StsClient, and configure BceClientConfiguration with the temporary token obtained. Create file "sts_sample.py", and apply for a set of AK, S and token via StsClient.

          import sts_sample_conf
          from baidubce.services.sts.sts_client import StsClient
          from baidubce.bce_client_configuration import BceClientConfiguration
          from baidubce.auth.bce_credentials import BceCredentials
          
          sts_client = StsClient(sts_sample_conf.config)
          
          duration_seconds = 3600
          # you can specify limited permissions with ACL
          access_dict = {}
          access_dict["service"] = "your service words"
          access_dict["region"] = "bj"   
          access_dict["effect"] = "Allow"
          resource = ["*"]
          access_dict["resource"] = resource
          permission = ["READ"]
          access_dict["permission"] = permission
          
          access_control_list = {"accessControlList": [access_dict]}
          # Create StsClient 
          response = sts_client.get_session_token(acl=access_control_list, duration_seconds=duration_seconds)
          
          sts_ak = str(response.access_key_id)
          sts_sk = str(response.secret_access_key)
          token = response.session_token
          eip_host = "EIP_HOST"
          # Configure BceClientConfiguration 
          config = BceClientConfiguration(credentials=BceCredentials(sts_ak, sts_sk), endpoint = eip_host, security_token=token)

          3.Before completing the configuration above, create an EipClient/EipGroupClient by reference to the following codes.

           import sts_sample 
          		
           # Import EIP related module 
           from baidubce import exception
           from baidubce.services import eip
           from baidubce.services.eip.eip_client import EipClient
           from baidubce.services.eip.eip_group_client import EipGroupClient
          	
           # Create EipClient 
           eip_client = EipClient(sts_sample.config)
           # Create EipGroupClient 
           eip_group_client = EipGroupClient(sts_sample.config)
           # Create EipBpClient
           eip_bp_client = EipBpClient(sts_sample.config)

          Configure HTTPS Protocol to Access to EIP/EipGroup/EipBp

          EIP supports HTTPS transport protocol, and you can access to EIP service with HTTPS in EIP Python SDK in the following 2 ways.

          • Indicate https in endpoint
           config = bce_client_configuration.BceClientConfiguration(
              credentials = bce_credentials.BceCredentials(
                  access_key_id = 'your-ak',
            	    secret_access_key = 'your-sk'
              ),
              endpoint = 'https://eip.bj.baidubce.com'
           )
           eip_client = eip_client.EipClient(config)
           eip_group_client = eip_group_client.EipGroupClient(config)
           eip_bp_client = eip_bp_client.EipBpClient(config)
          • Set HTTPS protocol by specifying https in protocol
          config = bce_client_configuration.BceClientConfiguration(
              credentials = bce_credentials.BceCredentials(
                  access_key_id = 'your-ak',
          	    secret_access_key = 'your-sk'
              ),
              endpoint = 'eip.bj.baidubce.com',
              protocol = baidubce.protocol.HTTPS
          )
          eip_client = eip_client.EipClient(config)
          eip_group_client = eip_group_client.EipGroupClient(config)
          eip_bp_client = eip_bp_client.EipBpClient(config)

          Note: If you specify scheme of endpoint while specifying protocol parameter, endpoint should prevail.

          config = bce_client_configuration.BceClientConfiguration(
              credentials = bce_credentials.BceCredentials(
                  access_key_id = 'your-ak',
          	    secret_access_key = 'your-sk'
              ),
              endpoint = 'http://bj.bcebos.com',
              protocol = baidubce.protocol.HTTPS
          )
          # Both are of HTTP type 
          eip_client = eip_client.EipClient(config)
          eip_group_client = eip_group_client.EipGroupClient(config)
          eip_bp_client = eip_bp_client.EipBpClient(config)

          Configure EipClient/EipGroupClient/EipBpClient

          Set Network Parameters

          You can set some network parameters.

          # Set request timeout period 
          eip_sample_conf.config.connection_timeout_in_mills = TIMEOUT
          	    
          # Set size of receiving buffer region 
          eip_sample_conf.config.recv_buf_size(BUF_SIZE)
          	
          # Set size of sending buffer region 
          eip_sample_conf.config.send_buf_size(BUF_SIZE)
          	
          # Set connection retry policy 
          # Cubic exponential backoff retry 
          eip_sample_conf.config.retry_policy = BackOffRetryPolicy()
          # No retry 
          eip_sample_conf.config.retry_policy = NoRetryPolicy()
          Previous
          SDK Installation
          Next
          EIP Instance