Example #1
0
 def setUp(self):
     """
     set up
     """
     HOST = b'bcc.bj.baidubce.com'
     AK = b''
     SK = b''
     config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                     endpoint=HOST)
     self.the_client = vpc_client.VpcClient(config)
Example #2
0
 def set_bos_config(self,
                    bos_ak,
                    bos_sk,
                    bos_sts,
                    bos_host="bj.bcebos.com"):
     self.config = BceClientConfiguration(credentials=BceCredentials(
         bos_ak, bos_sk),
                                          endpoint=bos_host,
                                          security_token=bos_sts)
     self.bos_client = BosClient(self.config)
Example #3
0
 def setUp(self):
     """
     set up
     """
     HOST = 'dcc.api-sandbox.baidu.com'
     AK = '4f4b13eda66e42e29225bb02d9193a48'
     SK = '507b4a729f6a44feab398a6a5984304d'
     config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                     endpoint=HOST)
     self.the_client = dcc_client.DccClient(config)
Example #4
0
 def setUp(self):
     """
     set up
     """
     HOST = b''
     AK = b''
     SK = b''
     config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                     endpoint=HOST)
     self.the_client = eip_client.EipClient(config)
Example #5
0
 def __init__(self,
              location=settings.MEDIA_URL,
              base_url=settings.MEDIA_URL):
     super(BOSStorage, self).__init__(location, base_url)
     config = BceClientConfiguration(credentials=BceCredentials(
         BCS_BUCKET['AK'], BCS_BUCKET['SK']),
                                     endpoint=BCS_BUCKET['END_POINT'])
     self.bos_client = BosClient(config)
     # check if bucket exists
     if not self.bos_client.does_bucket_exist(self.bucket_name):
         self.bos_client.create_bucket(self.bucket_name)
    def setUp(self):
        """
        set up
        """
        HOST = b''
        AK = b''
        SK = b''

        config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                        endpoint=HOST)
        self.client = peerconn_client.PeerConnClient(config)
Example #7
0
def get_bos_file_bytes_io(path):
    logger = logging.getLogger("baidubce.http.bce_http_client")
    logger.setLevel(logging.DEBUG)
    logging.info('Getting file from Baidu BOS...')

    bos_config = BceClientConfiguration(credentials=BceCredentials(
        access_key_id, secret_access_key),
                                        endpoint=bos_host)
    bos_client = BosClient(bos_config)
    content = bos_client.get_object_as_string(bucket_name=bucket_name,
                                              key=path)
    audio = io.BytesIO(content)  # this would auto seek(0)
    return audio
Example #8
0
 def up_image(self,file):
     config = BceClientConfiguration(credentials=BceCredentials(self.access_key_id,self.secret_access_key),endpoint=self.bos_host)
     client = BosClient(config)
     self.key_name = '1234.jpg'
     try:
         res = client.put_object_from_string(bucket=self.back_name, key=self.key_name, data=file)
     except Exception as e:
         raise e
     else:
         result = res.__dict__
         if result['metadata']:
             url = 'https://' + self.back_name + '.bj.bcebos.com/'+ self.key_name
             print('图片上传成功')
             return url
Example #9
0
    def _bce_init_connection_sts(self):
        try:
            bce_config = BceClientConfiguration(credentials=BceCredentials(
                access_key_id=self.bce_access_key_id,
                secret_access_key=self.bce_secret_access_key),
                                                endpoint=self.bce_sts_host)
            sts_client = StsClient(bce_config)
            access_dict = {}
            duration_seconds = 3600
            access_dict["service"] = "bce:bos"
            access_dict["region"] = "bj"
            access_dict["effect"] = "Allow"
            resource = ["*"]
            access_dict["resource"] = resource
            permission = ["*"]
            access_dict["permission"] = permission

            access_control_dict = {"accessControlList": [access_dict]}
            response = sts_client.get_session_token(
                acl=access_control_dict, duration_seconds=duration_seconds)

            config = BceClientConfiguration(
                credentials=BceCredentials(str(response.access_key_id),
                                           str(response.secret_access_key)),
                endpoint=self.bce_bos_host,
                security_token=response.session_token)
            bos_client = BosClient(config)
            return bos_client

        except BceError as e:
            self.logger.error('使用BCE当前连接令牌,在连接时发生错误 {}'.format(e))
            return []

        except Exception as e:
            self.logger.exception('使用BCE当前连接令牌,在连接时发生异常错误 {}'.format(e))
            return []
    def up_image(self, key_name, file):
        config = BceClientConfiguration(credentials=BceCredentials(
            self.access_key_id, self.secret_access_key),
                                        endpoint=self.bos_host)
        client = BosClient(config)

        key_name = key_name
        try:
            res = client.put_object_from_string(bucket=self.back_name,
                                                key=key_name,
                                                data=file)
        except Exception as e:
            return None
        else:
            print('put success!')
Example #11
0
    def _bce_init_connection(self):
        try:
            bce_config = BceClientConfiguration(credentials=BceCredentials(
                access_key_id=self.bce_access_key_id,
                secret_access_key=self.bce_secret_access_key),
                                                endpoint=self.bce_bos_host)
            bos_client = BosClient(bce_config)
            return bos_client

        except BceError as e:
            self.logger.error('使用BCE当前凭证,在连接时发生错误 {}'.format(e))
            return []

        except Exception as e:
            self.logger.exception('使用BCE当前凭证,在连接时发生异常错误 {}'.format(e))
            return []
Example #12
0
 def get_bos_config(self):
     bos_host = os.getenv("BOS_HOST")
     if not bos_host:
         raise KeyError('${BOS_HOST} is not found.')
     access_key_id = os.getenv("BOS_AK")
     if not access_key_id:
         raise KeyError('${BOS_AK} is not found.')
     secret_access_key = os.getenv("BOS_SK")
     if not secret_access_key:
         raise KeyError('${BOS_SK} is not found.')
     self.max_contents_count = int(os.getenv('BOS_CACHE_COUNT', 1))
     self.max_contents_time = int(os.getenv('BOS_CACHE_TIME', 1))
     bos_sts = os.getenv("BOS_STS")
     self.config = BceClientConfiguration(
         credentials=BceCredentials(access_key_id, secret_access_key),
         endpoint=bos_host, security_token=bos_sts)
Example #13
0
    def __init__(self,
                 access_key_id,
                 secret_access_key,
                 bucket_name='',
                 endpoint=''):
        """
        初始化
        """
        super(UBosClient, self).__init__()

        # 创建BceClientConfiguration
        config = BceClientConfiguration(credentials=BceCredentials(
            access_key_id, secret_access_key),
                                        endpoint=endpoint)
        # 设置请求超时时间
        config.connection_timeout_in_mills = 3000
        # 新建BosClient
        self.client = BosClient(config)
        self.bucket = bucket_name
Example #14
0
def _getSTSToken():
    """
    Get the token to upload the file

    :return:
    """

    if not Define.hubToken:
        raise Error.ParamError("please provide a valid token")

    config = invokeBackend("circuit/genSTS", {"token": Define.hubToken})

    bosClient = BosClient(
        BceClientConfiguration(
            credentials=BceCredentials(
                str(
                    config['accessKeyId']),
                str(
                    config['secretAccessKey'])),
            endpoint='http://bd.bcebos.com',
            security_token=str(
                config['sessionToken'])))

    return [Define.hubToken, bosClient, config['dest']]
Example #15
0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
"""
Configuration for sms samples.
"""

#!/usr/bin/env python
#coding=utf-8

import logging
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

HOST = 'sms.bj.baidubce.com'
AK = 'Your AK'
SK = 'Your SK'

logger = logging.getLogger('baidubce.services.sms.smsclient')
fh = logging.FileHandler('sms_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(AK, SK), endpoint=HOST)
Example #16
0
HOST = '<database_name>.tsdb.iot.<region>.baidubce.com'
AK = '<your ak>'
SK = '<your sk>'

###########optional config#############
protocol = baidubce.protocol.HTTP
# protcol= baidubce.protocol.HTTPS
connection_timeout_in_mills = None
send_buf_size = None
recv_buf_size = None
retry_policy = None
#######################################

logger = logging.getLogger('baidubce.services.tsdb.tsdbclient')
fh = logging.FileHandler('sample.log')
fh.setLevel(logging.INFO)

formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.setLevel(logging.INFO)
logger.addHandler(fh)

config = BceClientConfiguration(
    credentials=BceCredentials(AK, SK),
    endpoint=HOST,
    protocol=protocol,
    connection_timeout_in_mills=connection_timeout_in_mills,
    send_buf_size=send_buf_size,
    recv_buf_size=recv_buf_size,
    retry_policy=retry_policy)
Example #17
0
#coding=utf-8

#导入Python标准日志模块
import logging

#从Python SDK导入BOS配置管理模块以及安全认证模块
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

#设置BosClient的Host,Access Key ID和Secret Access Key
bos_host = "BOS_HOST"
access_key_id = "AK"
secret_access_key = "SK"

#设置日志文件的句柄和日志级别
logger = logging.getLogger('baidubce.services.bos.bosclient')
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)

#创建BceClientConfiguration
config = BceClientConfiguration(credentials=BceCredentials(access_key_id,secret_access_key),endpoint = bos_host)
                                                           secret_access_key),
                                endpoint = bos_host)''""''""""""
Example #18
0
# 导入Python标准日志模块
import logging

# 从Python SDK导入SMS配置管理模块以及安全认证模块
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

# 设置SmsClient的Host、AK、SK
# 注意:如果是gz区域的BCC用户,host应该使用 sms.gz.baidubce.com
#      如果是其他区域的BCC用户,或者非BCC用户,则使用 sms.bj.baidubce.com
host = "sms.bj.baidubce.com"
AK = ""
SK = ""

# 设置日志文件的句柄和日志级别
logger = logging.getLogger("baidubce.services.sms.smsclient")
fh = logging.FileHandler("sms_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)

# 创建BceClientConfiguration
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                endpoint=host)
Example #19
0
#导入Python标准日志模块
"""
Media Test
"""
import logging

#从Python SDK导入BOS配置管理模块以及安全认证模块
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

#设置BosClient的Host,Access Key ID和Secret Access Key
bos_host = b"http://multimedia.bce-testinternal.baidu.com"
access_key_id = b""
secret_access_key = b""

#设置日志文件的句柄和日志级别
logger = logging.getLogger('baidubce.services.media.mediaclient')
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)

#创建BceClientConfiguration
config = BceClientConfiguration(credentials=BceCredentials(
    access_key_id, secret_access_key),
                                endpoint=bos_host)
Example #20
0
 def createBosClient(self):
     auth = BceCredentials(self.access_key, self.secret_key)
     config = BceClientConfiguration(auth, self._host)
     return BosClient(config)
Example #21
0
import os
from django.conf import settings
from baidubce.auth.bce_credentials import BceCredentials



"""
        Baidu AK and SK should be placed inside settings.py. If they are absent, then it tries to read the value
    inside "except block" which is read from "keys.json"
"""

try:
    access_key_id = settings.BAIDU_AK
    secret_access_key = settings.BAIDU_SK
except:
    import json
    path = os.path.join(os.getcwd(), "docreaderxblock/docreaderxblock/doc/keys.json")
    if not os.path.isfile(path):
        path = os.path.join(os.path.dirname(os.getcwd()), "docreaderxblock/docreaderxblock/doc/keys.json")
    assert os.path.isfile(path)
    baidu_key_string = open(path, "r").read()

    baidu_dict = json.loads(baidu_key_string)

    access_key_id = str(baidu_dict["access_key_id"])
    secret_access_key = str(baidu_dict["secret_access_key"])

credentials = BceCredentials(access_key_id, secret_access_key)
Example #22
0
def push_messenger(message_body):
    global worker_config
    try:
        from baidubce.services.sms import sms_client as sms
        from baidubce import exception as ex
        from baidubce.bce_client_configuration import BceClientConfiguration
        from baidubce.auth.bce_credentials import BceCredentials
    except ImportError:
        print "Path error!!"
        print sys.path
        sys.exit()

    if worker_config is not None:
        app_path = get_app_path()
        worker_config = get_worker_config("SMSWorker", app_path['conf'])

    HOST = worker_config['app']['HOST']
    AK = worker_config['app']['AK']
    SK = worker_config['app']['SK']

    logger = logging.getLogger('baidubce.services.sms.smsclient')
    fh = logging.FileHandler('sms_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)

    bce_config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                        endpoint=HOST)
    logging.basicConfig(level=logging.DEBUG,
                        filename='./sms.log',
                        filemode='w')
    LOG = logging.getLogger(__name__)

    sms_client = sms.SmsClient(bce_config)
    try:
        """
        # query template list
        LOG.debug('Query template list')
        response = sms_client.get_template_list()
        valid_template_id = None
        valid_template_content = None
        for temp in response.template_list:
            print temp.template_id, temp.name, temp.content, \
                    temp.status, temp.create_time, temp.update_time
            if temp.status == u'VALID':
                valid_template_id = temp.template_id
                valid_template_content = temp.content
        """

        LOG.debug('Send Message')
        response = sms_client.send_message(
            'smsTpl:8504139f-2b8e-49f3-adc4-957b8b3bfa67', [message_body],
            {'username': "******"})
        message_id = response.message_id
        return message_id
        """
        #query message
        LOG.debug('query Message')
        response = sms_client.query_message_detail(message_id)

        print response.message_id, response.receiver, response.content, response.send_time
        """

    except ex.BceHttpClientError as e:
        if isinstance(e.last_error, ex.BceServerError):
            LOG.error('send request failed. Response %s, code: %s, msg: %s' %
                      (e.last_error.status_code, e.last_error.code,
                       e.last_error.message))
        else:
            LOG.error('send request failed. Unknown exception: %s' % e)
Example #23
0
 def setUp(self):
     config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
                                     endpoint=HOST)
     self.client = bcc_client.BccClient(config)
Example #24
0
from baidubce.services.bos.bos_client import BosClient
from django.utils import http
import exceptions as E, helper as H
from photo import globalConfig

# 配置BOS变量及BOS Client
_bucket = 'adventure030-image'
_bos_host = "http://bj.baidubos.com/"
_access_key = globalConfig.config['django-photosite']['baidu-access-key']
_secret_access_key = globalConfig.config['django-photosite'][
    'baidu-secret-key']

H.ensure(_access_key != '' and _secret_access_key != '', E.AssertFailed,
         '百度 Access Key 未提供.')

_bos_config = BceClientConfiguration(credentials=BceCredentials(
    _access_key, _secret_access_key),
                                     endpoint=_bos_host)
_bos_client = BosClient(_bos_config)

_ensure_path = lambda path: H.ensure(path.startswith('/'), E.AssertFailed,
                                     '路径必须以"/"开头.') or True
_ensure_folder_path = lambda folderPath: _ensure_path(folderPath) and H.ensure(
    folderPath.endswith('/'), E.AssertFailed, '文件夹路径必须以"/"结尾.')
_ensure_file_path = lambda filePath: _ensure_path(filePath) and H.ensure(
    not filePath.endswith('/'), E.AssertFailed, '文件路径不能"/"结尾.')

PREFIX = 'http://adventure030-image.bceimg.com'

ensure_file_path = _ensure_file_path

Example #25
0
#!/usr/bin/env python
#coding=utf8

import logging
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

SMS_HOST = b'HOST:PORT'
SMS_AK = b'ak'
SMS_SK = b'sk'

logger = logging.getLogger('baidubce.services.sms.smsclient')
fh = logging.FileHandler('sms_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(SMS_AK, SMS_SK),
                                endpoint=SMS_HOST)
Example #26
0
def main():
    """
    main函数
    :return: 
    """
    if len(sys.argv) != 3:
        print "参数个数不正确,请执行 sh test.sh 文件路径 Token"
        exit(0)
    path = sys.argv[1]
    token = sys.argv[2]
    if not os.path.exists(path):
        print "文件:" + path + " 不存在,请确认文件路径参数是否正确"
        exit(0)
    code, acl = bos_acl()
    if code != 200:
        print "服务异常,请稍候再试"
        exit(0)
    acl = json.loads(acl)
    if acl['errorCode'] != 0:
        print "服务异常,请稍候再试"
        exit(0)
    ak = unicode2str(acl['result']['accessKeyId'])
    sk = unicode2str(acl['result']['secretAccessKey'])
    bos_token = unicode2str(acl['result']['sessionToken'])
    config = BceClientConfiguration(
        credentials=BceCredentials(ak, sk),
        endpoint=unicode2str("http://bj.bcebos.com"),
        security_token=bos_token)
    bos_client = BosClient(config)
    bucket_name = unicode2str(acl['result']['bucketName'])
    object_key = unicode2str(acl['result']['fileKey'])
    token_status_code, token_data = check_token(token)
    if token_status_code != 200:
        print "服务异常,请稍候再试"
        exit(0)
    token_data = json.loads(token_data)
    if token_data['errorCode'] != 0:
        print token_data['errorMsg']
        exit(0)
    print "开始提交"
    upload_id = bos_client.initiate_multipart_upload(bucket_name,
                                                     object_key).upload_id
    left_size = os.path.getsize(path)
    # left_size用于设置分块开始位置
    # 设置分块的开始偏移位置
    offset = 0
    part_number = 1
    part_list = []
    total_size = left_size
    while left_size > 0:
        # 设置每块为5MB
        part_size = 10 * 1024 * 1024
        if left_size < part_size:
            part_size = left_size
        print total_size - left_size, "/", total_size
        response = bos_client.upload_part_from_file(bucket_name, object_key,
                                                    upload_id, part_number,
                                                    part_size, path, offset)

        left_size -= part_size
        offset += part_size
        part_list.append({
            "partNumber": part_number,
            "eTag": response.metadata.etag
        })
        part_number += 1
    print total_size, "/", total_size
    bos_client.complete_multipart_upload(bucket_name, object_key, upload_id,
                                         part_list)
    file_name = os.path.basename(path)
    status_code, data = submit(object_key, token, file_name)
    if status_code != 200:
        print "服务异常,请稍候再试"
    data = json.loads(data)
    if data['errorCode'] == 0:
        print data['result']
    else:
        print data['errorMsg']