Пример #1
0
def get_log_file(ak, sk, file_name, bucket_name, object_key, host):
    resource = '/%s/%s' % (bucket_name, object_key)
    date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
    string_to_sign = 'GET\n\n\n%s\n%s' % (date, resource)
    signature = base64.b64encode(hmac.new(sk, string_to_sign, hashlib.sha1).digest())

    t1 = datetime.datetime.now()
    resp = requests.get(
            'https://%s%s' % (host, resource),
            headers={'Host': host,
                     'Date': date,
                     'Authorization': 'AWS %s:%s' % (ak, signature)},
            timeout=timeout,
            verify=cert_verify)
    t2 = datetime.datetime.now()
    time_diff = (t2 - t1).total_seconds()
    if resp.status_code >= 300:
        msg = get_xml_error(resp.text, args=(bucket_name, object_key))
        if 'NoSuchKey' in msg:
            msg += '\n\033[31mTips: The log file may have NOT been generated, or have been Deleted or Moved.\033[0m'
        raise HttpException(resp.status_code, resp.reason, msg)

    with open(file_name, 'wb') as f:
        f.write(resp.content)

    return (resp.status_code, resp.reason, len(resp.content), time_diff)
Пример #2
0
def put_dcp_file(ak, sk, file_name, bucket_name, object_key, host):
    expand_file_name = os.path.expanduser(file_name)
    with open(expand_file_name, 'rb') as f:
        m = hashlib.md5()
        while True:
            b = f.read(10*1024*1024)
            if not b:
                break
            m.update(b)
        f.seek(0)

        content_md5 = base64.b64encode(m.digest())
        content_length = os.stat(expand_file_name).st_size
        resource = '/%s/%s' % (bucket_name, object_key)
        date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
        string_to_sign = 'PUT\n%s\n\n%s\n%s' % (content_md5, date, resource)
        signature = base64.b64encode(hmac.new(sk, string_to_sign, hashlib.sha1).digest())

        t1 = datetime.datetime.now()
        resp = requests.put(
                'https://%s%s' % (host, resource),
                headers={'Host': host,
                         'Date': date,
                         'Authorization': 'AWS %s:%s' % (ak, signature),
                         'Content-MD5': content_md5,
                         'Content-Length': str(content_length)},
                data=f,
                timeout=timeout,
                verify=cert_verify)
        t2 = datetime.datetime.now()
        time_diff = (t2 - t1).total_seconds()
        if resp.status_code >= 300:
            raise HttpException(resp.status_code, resp.reason, get_xml_error(resp.text, args=(bucket_name, object_key)))
        return (resp.status_code, resp.reason, content_length, time_diff)
Пример #3
0
def resp_with_text_error(resp):
    if resp.status_code >= 300:
        func = sys._getframe().f_back.f_code.co_name
        raise HttpException(resp.status_code, resp.reason, get_text_error(resp.text, func))
    try:
        return resp.json()
    except Exception:
        return dict()
Пример #4
0
def resp_with_json_error(resp):
    if resp.status_code >= 300:
        func = sys._getframe().f_back.f_code.co_name
        raise HttpException(resp.status_code, resp.reason, get_json_error(resp.text, func))
    try:
        resp_body = resp.json(object_pairs_hook=collections.OrderedDict)
    except Exception:
        resp_body = resp.text
    return resp.status_code, resp.reason, resp_body
Пример #5
0
def _get_bucket_info(ak, sk, resource, host):
    date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
    string_to_sign = 'GET\n\n\n%s\n%s' % (date, resource)
    signature = base64.b64encode(hmac.new(sk, string_to_sign, hashlib.sha1).digest())

    resp = requests.get(
            'https://%s%s' % (host, resource),
            headers={'Host': host,
                     'Date': date,
                     'Authorization': 'AWS %s:%s' % (ak, signature)},
            timeout=timeout,
            verify=cert_verify)
    if resp.status_code >= 300:
        func = sys._getframe().f_back.f_code.co_name
        raise HttpException(resp.status_code, resp.reason, get_xml_error(resp.text, func, args=resource))
    try:
        return getDictFromXml(resp.text)
    except Exception:
        return dict()
Пример #6
0
def make_bucket(ak, sk, bucket_name, location, host):
    resource = '/' + bucket_name
    date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
    content_type = 'application/xml'
    string_to_sign = 'PUT\n\n%s\n%s\n%s' % (content_type, date, resource)
    signature = base64.b64encode(hmac.new(sk, string_to_sign, hashlib.sha1).digest())
    body = '<CreateBucketConfiguration><LocationConstraint>%s</LocationConstraint></CreateBucketConfiguration>' % location

    resp = requests.put(
            'https://%s%s' % (host, resource),
            headers={'Host': host,
                     'Date': date,
                     'Authorization': 'AWS %s:%s' % (ak, signature),
                     'Content-Type': content_type},
            data=body,
            timeout=timeout,
            verify=cert_verify)
    if resp.status_code >= 300:
        raise HttpException(resp.status_code, resp.reason, get_xml_error(resp.text, args=(bucket_name, location)))
    return (resp.status_code, resp.reason)
Пример #7
0
def get_project(ak, sk, region, host):
    headers = sign_request_v4(ak,
                              sk,
                              'GET',
                              host,
                              '/v3/projects',
                              region,
                              'iam',
                              params={'name': region})
    headers['Content-Type'] = 'application/json;charset=utf8'
    resp = requests.get('https://%s/v3/projects?name=%s' % (host, region),
                        headers=headers,
                        timeout=timeout,
                        verify=cert_verify)
    if resp.status_code >= 300:
        raise HttpException(resp.status_code, resp.reason,
                            get_text_error(resp.text))
    try:
        return resp.json()
    except Exception:
        return dict()
Пример #8
0
def resp_without_body(resp):
    if resp.status_code >= 300:
        func = sys._getframe().f_back.f_code.co_name
        raise HttpException(resp.status_code, resp.reason, get_json_error(resp.text, func))
    return resp.status_code, resp.reason