MediaClient
Configure MediaClient
MediaClient is a Python client of Media service, which provides a series of methods for callers to interact with Media service.
Before create MediaClient, first create a configuration file to configure MediaClient, and in the following, configuration file is named as conf.py
, with specific configuration information as follows:
#!/usr/bin/env python
#coding=utf-8
#Import Python standard log module
import logging
#Import Media 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 MediaClient
media_host = "http://media.bj.baidubce.com"
access_key_id = "your-access-key-id"
secret_access_key = "your-secret-access-key"
#Set handle and log level of log file
logger = logging.getLogger('baidubce.services.media.mediaclient')
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 = media_host)
Note
1.In the above code, the variables AK and SK are assigned to user by the system to identify the user and perform signature verification for accessing Media. Among them, AK corresponds to the “Access Key ID” in the console, and SK corresponds to “Access Key Secret” in the console. Please refer to [Operation GuideManage ACCESSKEY].
2.ENDPOINT parameter can only be defined by the assigned domain name containing the Region; currently, the Media only provides one region, Beijing, so the ENDPOINT only supports domain name http://media.bj.baidubce.com
, and other domain names will be supported with the increasing of Regions.
Create MediaClient
After completing the above configuration, refer to the following code to create a MediaClient.
import conf
import sys
from baidubce import exception
from baidubce.services import media
from baidubce.services.media.media_client import MediaClient
# create new MediaClient
client = MediaClient(conf.config)
reload(sys)
sys.setdefaultencoding('utf-8')
print client.list_pipelines()
Parameter Description
Python SDK sets some parameters in baidubce\bce_client_configuration.py
by default; to modify the parameter value, you can refer to this file to create their own parameter configuration function, and introduce it when constructing MediaClient; please see the following for introduced codes:
from baidubce.retry_policy import BackOffRetryPolicy
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.protocol import HTTP
from baidubce.region import BEIJING
my_policy = BackOffRetryPolicy(max_error_retry = 3,
max_delay_in_millis=20 * 1000,
base_interval_in_millis=300)
my_config = BceClientConfiguration(
credentials = BceCredentials('your-access-key-id', 'your-secret-access-key'),
endpoint = media_host,
protocol = baidubce.protocol.HTTP,
region = baidubce.region.BEIJING,
connection_timeout_in_mills = 50 * 1000,
send_buf_size = 1024 * 1024,
recv_buf_size = 10 * 1024 * 1024,
retry_policy = my_policy)
# create MediaClient with my config
my_client = MediaClient(my_config)
pipelines = client.list_pipelines()
for pipeline in pipelines.pipelines:
print pipeline
The parameters are described as follows:
Parameter | Description | Default value |
---|---|---|
PROTOCOL | Protocol | baidubce.protocol.HTTP |
REGION | Region | baidubce.region.BEIJING(Only support Beijing currently) |
CONNECTION_TIMEOUT_IN_MILLIS | Request timeout (unit: millisecond) | 120 *1000 |
SOCKET_TIMEOUT_IN_MILLIS | Timeout for transmitting data over open connections (unit: millisecond) | 300 *1000(0 refers to infinite waiting; if you set a non-zero value, you need to evaluate the file size and network speed, otherwise timeouts will occur when large files are uploaded) |
SEND_BUF_SIZE | Send buffer size | 5 1024 1024 |
RECV_BUF_SIZE | Receive buffer size | 5 1024 1024 |
retry_policy | Retry logic | Retry can be made for 3 times at most, with the timeout of 20 * 1000, and a retry interval of 300ms |
Related Description
MediaClient encapsulates optional parameters in config
, see introduction to specific interface use method for optional parameters of each method; with create_pipeline
method as an example, the setting optional parameters are realized by reference to the following codes:
#Use options to pass in specified optional parameters when creating Pipeline
my_config = BceClientConfiguration(
credentials = BceCredentials('your-access-key-id', 'your-secret-access-key'),
endpoint = media_host,
send_buf_size = 5 * 1024 * 1024)
client.create_pipeline('your_pipeline', 'your_source_bucket', 'your_source_bucket', config=my_config);