Esempio n. 1
0
def up2cloud(filename, abs_filepath, need_rm=True, pic_host=""):
    ckey = "up2cloud"
    thekey = filename
    rcli = RedisClient().get_cli()
    thetoken = rcli.get(ckey)
    if thetoken:
        thetoken = json.loads(thetoken)
    else:
        q = Auth(access_key, secret_key)
        # 上传后保存的文件名
        # 生成上传 Token,可以指定过期时间等
        ttl = 3600
        thetoken = q.upload_token(g_bucket_name, None, ttl)
        rcli.set(ckey, json.dumps(thetoken), ex=ttl - 600)
    # 要上传文件的本地路径
    ret, info = put_file(thetoken, thekey, abs_filepath)
    uri = ""
    if ret and ret["key"] == thekey and ret['hash'] == etag(abs_filepath):
        uri = json.loads(info.text_body)["key"]
    if not uri:
        g_stdlogging.error("[up2cloud]%s %s" % (ret, info))
        raise unknown_err
    # print(abs_filepath)
    if need_rm:
        os.remove(abs_filepath)
    if not pic_host:
        pic_host = app.config["PIC_HOST"]
    return pic_host + "/" + uri
Esempio n. 2
0
def get_dict8xml(xmldata):
    res_dict = {}
    try:
        root = ET.fromstring(xmldata)
    except Exception as e:
        g_stdlogging.error("[get_dict8xml_err]%s" % e)
        return res_dict
    for item in root:
        res_dict[item.tag] = item.text
    return res_dict
Esempio n. 3
0
def send2mq(info, push_type=0, ttl=1):
    try:
        credentials = pika.PlainCredentials(RABBITMQ_USER,
                                            RABBITMQ_USER_PASSWORD)
        connection = pika.BlockingConnection(
            pika.ConnectionParameters('localhost', credentials=credentials))
        # Create normal 'Hello World' type channel.
        channel = connection.channel()
        channel.confirm_delivery()
        channel.queue_declare(queue=push_main_queue, durable=True)

        # We need to bind this channel to an exchange, that will be used to transfer
        # messages from our delay queue.
        channel.queue_bind(exchange=t_exchange, queue=push_main_queue)

        # Create our delay channel.
        delay_channel = connection.channel()
        delay_channel.confirm_delivery()

        # This is where we declare the delay, and routing for our delay channel.
        delay_channel.queue_declare(
            queue=push_delay_queue,
            durable=True,
            arguments={
                # 'x-message-ttl' :3000, # Delay until the message is transferred in milliseconds.
                'x-dead-letter-exchange':
                t_exchange,  # Exchange used to transfer the message from A to B.
                'x-dead-letter-routing-key':
                push_main_queue  # Name of the queue we want the message transferred to.
            })
        if ttl <= 0:
            raise Exception("ttl less than 1")
        pt = pika.BasicProperties(delivery_mode=2, expiration=str(ttl))
        new_info = {"info": json.loads(info), "push_type": push_type}
        g_stdlogging.info("[send2mq] %s %s" % (new_info, ttl))
        res = delay_channel.basic_publish(exchange='',
                                          routing_key=push_delay_queue,
                                          body=json.dumps(new_info),
                                          properties=pt)
    except Exception as e:
        g_stdlogging.error("[send2mq]err %s " % e)
Esempio n. 4
0
def up2cloud_video_part(filename, abs_filepath, need_rm=True, pic_host=""):
    ckey = "up2cloud_video_part1"
    thekey = filename
    rcli = RedisClient().get_cli()
    thetoken = rcli.get(ckey)
    if False and thetoken:
        thetoken = json.loads(thetoken)
    else:
        q = Auth(access_key, secret_key)
        # 上传后保存的文件名
        # 生成上传 Token,可以指定过期时间等
        ttl = 3600
        pat = urlsafe_base64_encode("videopart-$(count)")
        policy = {
            # "persistentOps": "vsample/jpg/ss/1/t/4/s/480x360/interval/1/pattern/dmZyYW1lLSQoY291bnQp",
            "persistentOps":
            "segment/mp4/segtime/5/pattern/%s" % pat,
            "persistentNotifyUrl":
            "https://balimiao.cn/front/qiniu/video_part_callback"
        }
        thetoken = q.upload_token(g_bucket_name, None, ttl, policy)
        rcli.set(ckey, json.dumps(thetoken), ex=ttl - 600)
    # 要上传文件的本地路径
    ret, info = put_file(thetoken, thekey, abs_filepath)
    uri = ""
    if ret and ret["key"] == thekey and ret['hash'] == etag(abs_filepath):
        uri = json.loads(info.text_body)["key"]
    if not uri:
        g_stdlogging.error("[up2cloud_video]%s %s" % (ret, info))
        raise unknown_err
    # print(abs_filepath)
    if need_rm:
        os.remove(abs_filepath)
    if not pic_host:
        pic_host = app.config["PIC_HOST"]
    return pic_host + "/" + uri