def getCanonicalURI(self, bucket=None, object=None):
     URI = ''
     if self.path_style and bucket is not None and bucket != '':
         URI += '/' + bucket
     if object is not None:
         URI += '/' + object
     if not URI:
         URI = '/'
     return common_util.encode_object_key(URI)
예제 #2
0
    def __make_canonicalstring(self,
                               method,
                               bucket_name,
                               key,
                               path_args,
                               headers,
                               expires=None):

        str_list = []
        str_list.append(method + '\n')

        # 添加所有相关的头部字段(Content-MD5, Content-Type, Date和以x-amz开头的),并排序
        interesting_headers = {}  # 使用字典表示
        content_list = ['content-type', 'content-md5', 'date']
        if isinstance(headers, dict):
            for hash_key in headers.keys():
                lk = hash_key.lower()  # headers的key值的小写

                # 忽略不相关的HTTP头字段
                if lk in content_list or lk.startswith(
                        common_util.AMAZON_HEADER_PREFIX):
                    s = headers.get(hash_key)  # 获得headers中的值列表
                    interesting_headers[lk] = ''.join(s)

        keylist = interesting_headers.keys()
        # 如果有amz的时间标记就无需加入原有的date标记
        if common_util.ALTERNATIVE_DATE_HEADER in keylist:
            interesting_headers.setdefault('date', '')

        # 如果过期时间不为空,则将过期时间填入date字段中
        if expires:
            interesting_headers['date'] = expires

        # 这些字段必须要加入,故即使没有设置也要加入
        if not 'content-type' in keylist:
            interesting_headers['content-type'] = ''

        if not 'content-md5' in keylist:
            interesting_headers['content-md5'] = ''

        # 取出字典中的key并进行排序
        keylist = sorted(interesting_headers.keys())

        # 最后加入所有相关的HTTP头部字段 (例如: 所有以x-amz-开头的)
        for k in keylist:
            header_key = str(k)
            if header_key.startswith(common_util.AMAZON_HEADER_PREFIX):
                str_list.append(header_key + ':' +
                                interesting_headers[header_key])
            else:
                str_list.append(interesting_headers[header_key])
            str_list.append('\n')

        URI = ''
        # 使用存储空间名和对象名构建路径
        if bucket_name is not None and bucket_name != '':
            URI += '/'
            URI += bucket_name
            if not self.path_style:
                URI += '/'

        # 对象名不为空,则添加对象名到待签名的字符串中
        if key is not None:
            # 再加入一个反斜杠
            if not URI.endswith('/'):
                URI += '/'
            URI += common_util.encode_object_key(key)

        str_list.append(URI) if URI else str_list.append('/')

        # 最后检查路径参数里是否有ACL,有则加入
        if path_args:
            e1 = '?'
            e2 = '&'
            for path_key, path_value in path_args.items():
                flag = True
                if path_key not in common_util.ALLOWED_RESOURCE_PARAMTER_NAMES:
                    flag = False
                if flag:
                    path_key = common_util.toString(path_key)
                    if path_value is None:
                        e1 += path_key + '&'
                        continue
                    e2 += path_key + '=' + common_util.toString(
                        path_value) + '&'
            e = (e1 + e2).replace('&&', '&').replace('?&', '?')[:-1]
            str_list.append(e)
        return ''.join(item for item in str_list)  # 返回待签名的字符串
예제 #3
0
    def __make_canonicalstring(self,
                               method,
                               bucket_name,
                               key,
                               path_args,
                               headers,
                               expires=None):

        str_list = []
        str_list.append(method + '\n')

        interesting_headers = {}
        content_list = ['content-type', 'content-md5', 'date']
        if isinstance(headers, dict):
            for hash_key in headers.keys():
                lk = hash_key.lower()

                if lk in content_list or lk.startswith(
                        common_util.AMAZON_HEADER_PREFIX):
                    s = headers.get(hash_key)
                    interesting_headers[lk] = ''.join(s)

        keylist = interesting_headers.keys()

        if common_util.ALTERNATIVE_DATE_HEADER in keylist:
            interesting_headers.setdefault('date', '')

        if expires:
            interesting_headers['date'] = expires

        if 'content-type' not in keylist:
            interesting_headers['content-type'] = ''

        if 'content-md5' not in keylist:
            interesting_headers['content-md5'] = ''

        keylist = sorted(interesting_headers.keys())

        for k in keylist:
            header_key = str(k)
            if header_key.startswith(common_util.AMAZON_HEADER_PREFIX):
                str_list.append(header_key + ':' +
                                interesting_headers[header_key])
            else:
                str_list.append(interesting_headers[header_key])
            str_list.append('\n')

        URI = ''
        if bucket_name is not None and bucket_name != '':
            URI += '/'
            URI += bucket_name
            if not self.path_style:
                URI += '/'

        if key is not None:
            if not URI.endswith('/'):
                URI += '/'
            URI += common_util.encode_object_key(key)

        if URI:
            str_list.append(URI)
        else:
            str_list.append('/')

        if path_args:
            e1 = '?'
            e2 = '&'
            canonMap = {}
            for _key, value in path_args.items():
                canonMap[_key] = value
            cannoList = sorted(canonMap.items(), key=lambda d: d[0])
            for val in cannoList:
                path_key, path_value = val[0], val[1]
                flag = True
                if path_key.lower(
                ) not in common_util.ALLOWED_RESOURCE_PARAMTER_NAMES:
                    flag = False
                if flag:
                    path_key = common_util.encode_item(
                        common_util.toString(path_key), ' ,:?&%')
                    if path_value is None:
                        e1 += path_key + '&'
                        continue
                    e2 += path_key + '=' + common_util.toString(
                        path_value) + '&'
            e = (e1 + e2).replace('&&', '&').replace('?&', '?')[:-1]
            str_list.append(e)
        return ''.join(str_list)