def test_crypto_get_compact_deprecated_rsa(self, do_request):
        utils.silently_remove('./rsa-test.public_key.pem')
        utils.silently_remove('./rsa-test.private_key.pem')

        with open("./rsa-test.private_key.pem", 'wb') as f:
            f.write(oss2.to_bytes(private_key_compact))

        with open("./rsa-test.public_key.pem", 'wb') as f:
            f.write(oss2.to_bytes(public_key_compact))

        content = b'a' * 1024 * 1024
        encrypted_rsa_path = "tests/deprecated_encrypted_1MB_a_rsa"
        encrypted_meta_rsa_path = "tests/deprecated_encrypted_1MB_a_meta_rsa.json"

        with open(encrypted_rsa_path, 'rb') as f:
            encrypted_content = f.read()

        with open(encrypted_meta_rsa_path, 'r') as f:
            meta = json.loads(f.read())

        key = random_string(10)
        provider = oss2.LocalRsaProvider(dir='./', key='rsa-test')

        request_text, response_text = make_get_encrypted_object_compact_deprecated(
            key, encrypted_content, meta)

        req_info = unittests.common.mock_response(do_request, response_text)
        result = unittests.common.bucket(provider).get_object(key)

        self.assertRequest(req_info, request_text)
        self.assertEqual(result.read(), content)

        utils.silently_remove('./rsa-test.public_key.pem')
        utils.silently_remove('./rsa-test.private_key.pem')
Пример #2
0
    def assertXmlEqual(self, a, b):
        normalized_a = minidom.parseString(
            oss2.to_bytes(a)).toxml(encoding='utf-8')
        normalized_b = minidom.parseString(
            oss2.to_bytes(b)).toxml(encoding='utf-8')

        self.assertEqual(normalized_a, normalized_b)
Пример #3
0
    def assertXmlEqual(self, a, b):
        a = a.translate(None, b'\r\n')
        b = b.translate(None, b'\r\n')

        normalized_a = minidom.parseString(oss2.to_bytes(a)).toxml(encoding='utf-8')
        normalized_b = minidom.parseString(oss2.to_bytes(b)).toxml(encoding='utf-8')

        self.assertEqual(normalized_a, normalized_b)
Пример #4
0
    def assertXmlEqual(self, a, b):
        a = a.translate(None, b'\r\n')
        b = b.translate(None, b'\r\n')

        normalized_a = minidom.parseString(
            oss2.to_bytes(a)).toxml(encoding='utf-8')
        normalized_b = minidom.parseString(
            oss2.to_bytes(b)).toxml(encoding='utf-8')

        self.assertEqual(normalized_a, normalized_b)
Пример #5
0
    def test_part_iterator(self):
        for key in [random_string(16), "中文+_)(*&^%$#@!前缀", u"中文+_)(*&^%$#@!前缀"]:
            upload_id = self.bucket.init_multipart_upload(key).upload_id

            # 准备分片
            part_list = []
            for part_number in [1, 3, 6, 7, 9, 10]:
                content = random_string(128 * 1024)
                etag = hashlib.md5(oss2.to_bytes(content)).hexdigest().upper()
                part_list.append(oss2.models.PartInfo(part_number, etag, len(content)))

                self.bucket.upload_part(key, upload_id, part_number, content)

            # 验证
            parts_got = []
            for part_info in oss2.PartIterator(self.bucket, key, upload_id):
                parts_got.append(part_info)

            self.assertEqual(len(part_list), len(parts_got))

            for i in range(len(part_list)):
                self.assertEqual(part_list[i].part_number, parts_got[i].part_number)
                self.assertEqual(part_list[i].etag, parts_got[i].etag)
                self.assertEqual(part_list[i].size, parts_got[i].size)

            self.bucket.abort_multipart_upload(key, upload_id)
Пример #6
0
    def test_part_iterator(self):
        for key in [random_string(16), '中文+_)(*&^%$#@!前缀', u'中文+_)(*&^%$#@!前缀']:
            upload_id = self.bucket.init_multipart_upload(key).upload_id

            # 准备分片
            part_list = []
            for part_number in [1, 3, 6, 7, 9, 10]:
                content = random_string(128 * 1024)
                etag = hashlib.md5(oss2.to_bytes(content)).hexdigest().upper()
                part_list.append(oss2.models.PartInfo(part_number, etag, len(content)))

                self.bucket.upload_part(key, upload_id, part_number, content)

            # 验证
            parts_got = []
            for part_info in oss2.PartIterator(self.bucket, key, upload_id):
                parts_got.append(part_info)

            self.assertEqual(len(part_list), len(parts_got))

            for i in range(len(part_list)):
                self.assertEqual(part_list[i].part_number, parts_got[i].part_number)
                self.assertEqual(part_list[i].etag, parts_got[i].etag)
                self.assertEqual(part_list[i].size, parts_got[i].size)

            self.bucket.abort_multipart_upload(key, upload_id)
def make_put_encrypted_object(content):

    cipher = oss2.utils.AESCipher(key=unittests.common.fixed_aes_key, start=unittests.common.fixed_aes_start)
    encrypted_cont = cipher.encrypt(content)

    request_text = '''PUT /sjbhlsgsbecvlpbf.txt HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
Content-Type: text/plain
Content-Length: {0}
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
authorization: OSS ZCDmm7TPZKHtx77j:W6whAowN4aImQ0dfbMHyFfD0t1g=
Accept: */*

'''.format(len(content))

    io = BytesIO()
    io.write(oss2.to_bytes(request_text))
    io.write(encrypted_cont)

    request_text = io.getvalue()

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Length: 0
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
x-oss-hash-crc64ecma: {0}
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"'''.format(unittests.common.calc_crc(encrypted_cont))

    return request_text, response_text
Пример #8
0
    def __init__(self, status, headers, body):
        self.status = status
        self.headers = oss2.CaseInsensitiveDict(headers)
        self.body = oss2.to_bytes(body)
        self.request_id = headers.get('x-oss-request-id', '')

        self.offset = 0
    def test_rsa_provider_init_invalid_keys(self):
        private_key = RSA.generate(2048)
        public_key = private_key.publickey()
        # 这个地方修改private_key的内容
        private_key = random_string(2048)

        with open('./rsa-test.private_key.pem', 'wb') as f:
            f.write(oss2.to_bytes(private_key))

        with open('./rsa-test.public_key.pem', 'wb') as f:
            f.write(public_key.exportKey())

        self.assertRaises(ClientError,
                          LocalRsaProvider,
                          dir='./',
                          key='rsa-test')
        silently_remove('./rsa-test.public_key.pem')
        silently_remove('./rsa-test.private_key.pem')

        self.assertRaises(ClientError,
                          RsaProvider,
                          key_pair={
                              'private_key': private_key,
                              'public_key': public_key
                          })
Пример #10
0
    def make_tempfile(self, content):
        fd, pathname = tempfile.mkstemp(suffix='test-upload')

        os.write(fd, oss2.to_bytes(content))
        os.close(fd)

        self.temp_files.append(pathname)
        return pathname
Пример #11
0
    def __fake_response(self, status, error_body):
        key = self.random_key()

        self.bucket.put_object(key, oss2.to_bytes(error_body))
        resp = self.bucket.get_object(key).resp
        resp.status = status

        return resp
Пример #12
0
    def __fake_response(self, status, error_body):
        key = self.random_key()

        self.bucket.put_object(key, oss2.to_bytes(error_body))
        resp = self.bucket.get_object(key).resp
        resp.status = status

        return resp
Пример #13
0
    def make_tempfile(self, content):
        fd, pathname = tempfile.mkstemp(suffix='test-upload')

        os.write(fd, oss2.to_bytes(content))
        os.close(fd)

        self.temp_files.append(pathname)
        return pathname
Пример #14
0
def random_file(n):
    filename = (n or random_string(32)) + '.txt'
    content = oss2.to_bytes(random_string(1024 * 1024))

    with open(filename, 'wb') as fileobj:
        fileobj.write(content)

    return filename
    def test_put_get_list_delete(self):
        for key in ['中文!@#$%^&*()-=文件\x0C-1.txt', u'中文!@#$%^&*()-=文件\x0C-1.txt']:
            content = '中文内容'

            self.bucket.put_object(key, content)
            self.assertEqual(self.bucket.get_object(key).read(), to_bytes(content))

            self.assertTrue(to_string(key) in list(info.key for info in oss2.ObjectIterator(self.bucket, prefix='中文')))

            self.bucket.delete_object(key)
def make_get_encrypted_object(key,
                              content,
                              content_crypto_material,
                              invalid_cek_alg='',
                              ranges=None):
    request_text = '''GET /{0} HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:PAedG7U86ZxQ2WTB+GdpSltoiTI='''.format(key)

    encrypted_key = utils.b64encode_as_string(
        content_crypto_material.encrypted_key)
    encrypted_iv = utils.b64encode_as_string(
        content_crypto_material.encrypted_iv)
    wrap_alg = content_crypto_material.wrap_alg
    cek_alg = content_crypto_material.cek_alg
    if invalid_cek_alg:
        cek_alg = invalid_cek_alg
    cipher = content_crypto_material.cipher

    encrypted_content = cipher.encrypt(content)

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Type: text/plain
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
Accept-Ranges: bytes
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"
x-oss-meta-client-side-encryption-wrap-alg: {1}
x-oss-meta-client-side-encryption-cek-alg: {2}
x-oss-meta-client-side-encryption-key: {3}
x-oss-meta-client-side-encryption-start: {4}
x-oss-meta-unencrypted-content-length: {5}
Last-Modified: Sat, 12 Dec 2015 00:35:53 GMT
x-oss-object-type: Normal{6}

'''.format(len(encrypted_content), wrap_alg, cek_alg, encrypted_key,
           encrypted_iv, len(content),
           '\nContent-Range: {0}'.format(ranges) if ranges else '')

    io = BytesIO()
    io.write(oss2.to_bytes(response_text))
    io.write(encrypted_content)

    response_text = io.getvalue()

    return request_text, response_text
Пример #17
0
    def test_make_exception(self):
        body = 'bad body'
        e = make_exception(self.__fake_response(400, body))
        self.assertTrue(isinstance(e, oss2.exceptions.ServerError))
        self.assertEqual(e.status, 400)
        self.assertEqual(e.body, oss2.to_bytes(body))

        body = '<Error><Code>NoSuchKey</Code><Message>中文和控制字符&#12;</Message></Error>'
        e = make_exception(self.__fake_response(404, body))
        self.assertTrue(isinstance(e, oss2.exceptions.NoSuchKey))
        self.assertEqual(e.status, 404)
        self.assertEqual(e.code, 'NoSuchKey')
Пример #18
0
    def test_make_exception(self):
        body = 'bad body'
        e = make_exception(self.__fake_response(400, body))
        self.assertTrue(isinstance(e, oss2.exceptions.ServerError))
        self.assertEqual(e.status, 400)
        self.assertEqual(e.body, oss2.to_bytes(body))

        body = '<Error><Code>NoSuchKey</Code><Message>中文和控制字符&#12;</Message></Error>'
        e = make_exception(self.__fake_response(404, body))
        self.assertTrue(isinstance(e, oss2.exceptions.NoSuchKey))
        self.assertEqual(e.status, 404)
        self.assertEqual(e.code, 'NoSuchKey')
Пример #19
0
    def __init__(self, response_text):
        fields = re.split('\n\n', response_text, 1)
        head_fields = re.split('\n', fields[0])
        response_line_fields = head_fields[0].split(' ', 2)

        self.status = int(response_line_fields[1])
        self.headers = head_fields_to_headers(head_fields[1:])

        if len(fields) == 2:
            self.body = oss2.to_bytes(fields[1])
        else:
            self.body = b''

        self.__io = io.BytesIO(self.body)
Пример #20
0
    def __init__(self, response_text):
        fields = re.split('\n\n', response_text, 1)
        head_fields = re.split('\n', fields[0])
        response_line_fields = head_fields[0].split(' ', 2)

        self.status = int(response_line_fields[1])
        self.headers = head_fields_to_headers(head_fields[1:])

        if len(fields) == 2:
            self.body = oss2.to_bytes(fields[1])
        else:
            self.body = b''

        self.__io = io.BytesIO(self.body)
Пример #21
0
    def test_crc64_combine(self):
        _POLY = 0x142F0E1EBA9EA3693
        _XOROUT = 0XFFFFFFFFFFFFFFFF

        string_a = oss2.to_bytes('12345')
        string_b = oss2.to_bytes('67890')

        combine_fun = oss2.crc64_combine.mkCombineFun(_POLY, 0, True, _XOROUT)

        crc64_a = crcmod.Crc(_POLY, initCrc=0, xorOut=_XOROUT)
        crc64_a.update(string_a)
        crc1 = crc64_a.crcValue

        crc64_b = crcmod.Crc(_POLY, initCrc=0, xorOut=_XOROUT)
        crc64_b.update(string_b)
        crc2 = crc64_b.crcValue

        crc_combine = combine_fun(crc1, crc2, len(string_b))

        crc64_c = crcmod.Crc(_POLY, initCrc=0, xorOut=_XOROUT)
        crc64_c.update(string_a + string_b)
        crc_raw = crc64_c.crcValue

        self.assertEqual(crc_combine, crc_raw)
Пример #22
0
    def test_compat(self):
        # from unicode
        u = u'中文'

        self.assertEqual(u, oss2.to_unicode(u))
        self.assertEqual(u.encode('utf-8'), oss2.to_bytes(u))

        if is_py2:
            self.assertEqual(u.encode('utf-8'), oss2.to_string(u))

        if is_py3:
            self.assertEqual(u, oss2.to_string(u))

        # from bytes
        b = u.encode('utf-8')

        self.assertEqual(b.decode('utf-8'), oss2.to_unicode(b))
        self.assertEqual(b, oss2.to_bytes(b))

        if is_py2:
            self.assertEqual(b, oss2.to_string(b))

        if is_py3:
            self.assertEqual(b.decode('utf-8'), oss2.to_string(b))
Пример #23
0
def do4response(req, timeout, req_info=None, payload=None):
    if req_info:
        req_info.req = req

        if req.data is None:
            req_info.data = b''
            req_info.size = 0
        elif is_string_type(req.data):
            req_info.data = oss2.to_bytes(req.data)
            req_info.size = len(req_info.data)
        else:
            req_info.data = read_data(req.data, DT_FILE)
            req_info.size = get_length(req.data)

    return MockResponse2(payload)
Пример #24
0
def do4response(req, timeout, req_info=None, payload=None):
    if req_info:
        req_info.req = req

        if req.data is None:
            req_info.data = b''
            req_info.size = 0
        elif is_string_type(req.data):
            req_info.data = oss2.to_bytes(req.data)
            req_info.size = len(req_info.data)
        else:
            req_info.data = read_data(req.data, DT_FILE)
            req_info.size = get_length(req.data)

    return MockResponse2(payload)
Пример #25
0
    def test_compat(self):
        # from unicode
        u = u'中文'

        self.assertEqual(u, oss2.to_unicode(u))
        self.assertEqual(u.encode('utf-8'), oss2.to_bytes(u))

        if is_py2:
            self.assertEqual(u.encode('utf-8'), oss2.to_string(u))

        if is_py3:
            self.assertEqual(u, oss2.to_string(u))

        # from bytes
        b = u.encode('utf-8')

        self.assertEqual(b.decode('utf-8'), oss2.to_unicode(b))
        self.assertEqual(b, oss2.to_bytes(b))

        if is_py2:
            self.assertEqual(b, oss2.to_string(b))

        if is_py3:
            self.assertEqual(b.decode('utf-8'), oss2.to_string(b))
Пример #26
0
    def test_get_with_progress(self, do_request):
        content = random_bytes(1024 * 1024 + 1)

        request_text, response_text = make_get_object(content)
        req_info = mock_response(do_request, response_text)

        self.previous = -1
        result = bucket().get_object('sjbhlsgsbecvlpbf', progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        content_read = read_file(result)

        self.assertEqual(self.previous, len(content))
        self.assertEqual(len(content_read), len(content))
        self.assertEqual(content_read, oss2.to_bytes(content))
    def test_get_with_progress(self, do_request):
        content = random_bytes(1024 * 1024 + 1)

        request_text, response_text = make_get_object(content)
        req_info = mock_response(do_request, response_text)

        self.previous = -1
        result = bucket().get_object('sjbhlsgsbecvlpbf',
                                     progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        content_read = read_file(result)

        self.assertEqual(self.previous, len(content))
        self.assertEqual(len(content_read), len(content))
        self.assertEqual(content_read, oss2.to_bytes(content))
Пример #28
0
    def _prepare_temp_file_with_size(self, size):
        fd, pathname = tempfile.mkstemp(suffix='test-upload')

        block_size = 8 * 1024 * 1024
        num_written = 0

        while num_written < size:
            to_write = min(block_size, size - num_written)
            num_written += to_write

            content = 's' * to_write
            os.write(fd, oss2.to_bytes(content))

        os.close(fd)

        self.temp_files.append(pathname)
        return pathname
Пример #29
0
    def _prepare_temp_file_with_size(self, size):
        fd, pathname = tempfile.mkstemp(suffix='test-upload')

        block_size = 8 * 1024 * 1024
        num_written = 0

        while num_written < size:
            to_write = min(block_size, size - num_written)
            num_written += to_write

            content = 's' * to_write
            os.write(fd, oss2.to_bytes(content))

        os.close(fd)

        self.temp_files.append(pathname)
        return pathname
def make_put_encrypted_object(key, content, content_crypto_material):
    cipher = content_crypto_material.cipher
    encrypted_key = utils.b64encode_as_string(
        content_crypto_material.encrypted_key)
    encrypted_iv = utils.b64encode_as_string(
        content_crypto_material.encrypted_iv)
    encrypted_content = cipher.encrypt(content)
    wrap_alg = content_crypto_material.wrap_alg
    cek_alg = content_crypto_material.cek_alg

    request_text = '''PUT /{0} HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
Content-Length: {1}
x-oss-meta-client-side-encryption-wrap-alg: {2}
x-oss-meta-client-side-encryption-cek-alg: {3}
x-oss-meta-client-side-encryption-key: {4}
x-oss-meta-client-side-encryption-start: {5}
x-oss-meta-unencrypted-content-length: {6}
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
authorization: OSS ZCDmm7TPZKHtx77j:W6whAowN4aImQ0dfbMHyFfD0t1g=
Accept: */*

'''.format(key, len(content), wrap_alg, cek_alg, encrypted_key, encrypted_iv,
           len(encrypted_content))

    io = BytesIO()
    io.write(oss2.to_bytes(request_text))
    io.write(encrypted_content)

    request_text = io.getvalue()

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
x-oss-hash-crc64ecma: {1}
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"'''.format(
        len(content), unittests.common.calc_crc(encrypted_content))

    return request_text, response_text
Пример #31
0
    def test_get_to_file_with_progress(self, do_request):
        size = 1024 * 1024 + 1
        content = random_bytes(size)

        request_text, response_text = make_get_object(content)
        req_info = mock_response(do_request, response_text)

        filename = self.tempname()

        self.previous = -1
        bucket().get_object_to_file('sjbhlsgsbecvlpbf', filename, progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        self.assertEqual(self.previous, size)
        self.assertEqual(os.path.getsize(filename), size)
        with open(filename, 'rb') as f:
            self.assertEqual(oss2.to_bytes(content), f.read())
    def test_crc_and_cipher_adapter(self):

        crc_adapter = oss2.utils.make_crc_adapter('sss')
        cipher_adapter = oss2.utils.make_cipher_adapter(crc_adapter,
                    partial(oss2.utils.AESCipher.encrypt ,oss2.utils.AESCipher(b'1' * 32, 1)))

        content = cipher_adapter.read()

        self.assertEqual(cipher_adapter.crc, 10301458956098309249)

        with io.BytesIO(oss2.to_bytes('sss')) as f:
            crc_adapter = oss2.utils.make_crc_adapter(f)
            cipher_adapter = oss2.utils.make_cipher_adapter(crc_adapter,
                     partial(oss2.utils.AESCipher.encrypt, oss2.utils.AESCipher(b'1' * 32, 1)))

            content = cipher_adapter.read()

            self.assertEqual(cipher_adapter.crc, 10301458956098309249)
Пример #33
0
    def __init__(self, response_text):
        if isinstance(response_text, bytes):
            fields = re.split(b'\n\n', response_text, 1)
        else:
            fields = re.split('\n\n', response_text, 1)
        head_fields = re.split('\n', oss2.to_string(fields[0]))
        response_line_fields = head_fields[0].split(' ', 2)

        self.status = int(response_line_fields[1])
        self.headers = head_fields_to_headers(head_fields[1:])
        self.request_id = self.headers.get('x-oss-request-id', '')

        if len(fields) == 2:
            self.body = oss2.to_bytes(fields[1])
        else:
            self.body = b''

        self.__io = io.BytesIO(self.body)
    def test_crypto_get_to_file_with_progress(self, do_request):
        size = 1024 * 1024 + 1
        content = unittests.common.random_bytes(size)

        request_text, response_text = make_get_encrypted_object(content)
        req_info = unittests.common.mock_response(do_request, response_text)

        filename = self.tempname()

        self.previous = -1
        unittests.common.bucket(oss2.LocalRsaProvider(key='oss-test')).get_object_to_file('sjbhlsgsbecvlpbf', filename, progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        self.assertEqual(self.previous, size)
        self.assertEqual(os.path.getsize(filename), size)
        with open(filename, 'rb') as f:
            self.assertEqual(oss2.to_bytes(content), f.read())
    def test_crypto_get_to_file(self, do_request):
        content = unittests.common.random_bytes(1023)

        request_text, response_text = make_get_encrypted_object(content)
        req_info = unittests.common.mock_response(do_request, response_text)

        filename = self.tempname()

        result = unittests.common.bucket(oss2.LocalRsaProvider(key='oss-test')).get_object_to_file('sjbhlsgsbecvlpbf', filename)

        self.assertRequest(req_info, request_text)

        self.assertEqual(result.request_id, '566B6BE93A7B8CFD53D4BAA3')
        self.assertEqual(result.content_length, len(content))
        self.assertEqual(os.path.getsize(filename), len(content))

        with open(filename, 'rb') as f:
            self.assertEqual(content, oss2.to_bytes(f.read()))
Пример #36
0
    def test_crc_and_cipher_adapter(self):

        crc_adapter = oss2.utils.make_crc_adapter('sss')
        cipher_adapter = oss2.utils.make_cipher_adapter(crc_adapter,
                    partial(oss2.utils.AESCipher.encrypt ,oss2.utils.AESCipher(b'1' * 32, 1)))

        content = cipher_adapter.read()

        self.assertEqual(cipher_adapter.crc, 10301458956098309249)

        with io.BytesIO(oss2.to_bytes('sss')) as f:
            crc_adapter = oss2.utils.make_crc_adapter(f)
            cipher_adapter = oss2.utils.make_cipher_adapter(crc_adapter,
                     partial(oss2.utils.AESCipher.encrypt, oss2.utils.AESCipher(b'1' * 32, 1)))

            content = cipher_adapter.read()

            self.assertEqual(cipher_adapter.crc, 10301458956098309249)
Пример #37
0
    def __init__(self, request_text):
        fields = re.split('\n\n', request_text, 1)
        head_fields = re.split('\n', fields[0])
        request_line_fields = head_fields[0].split()

        uri_query_fields = request_line_fields[1].split('?')
        if len(uri_query_fields) == 2:
            self.params = query_to_params(uri_query_fields[1])
        else:
            self.params = {}

        if len(fields) == 2:
            self.body = oss2.to_bytes(fields[1])
        else:
            self.body = b''

        self.method = request_line_fields[0]
        self.headers = head_fields_to_headers(head_fields[1:])
        self.url = 'http://' + self.headers['host'] + uri_query_fields[0]
Пример #38
0
    def __init__(self, request_text):
        fields = re.split('\n\n', request_text, 1)
        head_fields = re.split('\n', fields[0])
        request_line_fields = head_fields[0].split()

        uri_query_fields = request_line_fields[1].split('?')
        if len(uri_query_fields) == 2:
            self.params = query_to_params(uri_query_fields[1])
        else:
            self.params = {}

        if len(fields) == 2:
            self.body = oss2.to_bytes(fields[1])
        else:
            self.body = b''

        self.method = request_line_fields[0]
        self.headers = head_fields_to_headers(head_fields[1:])
        self.url = 'http://' + self.headers['host'] + uri_query_fields[0]
def make_get_encrypted_object_compact_deprecated(key, encrypted_content,
                                                 encrypted_meta):
    request_text = '''GET /{0} HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:PAedG7U86ZxQ2WTB+GdpSltoiTI='''.format(key)

    encrypted_key = encrypted_meta['x-oss-meta-oss-crypto-key']
    encrypted_iv = encrypted_meta['x-oss-meta-oss-crypto-start']
    wrap_alg = encrypted_meta['x-oss-meta-oss-wrap-alg']
    cek_alg = encrypted_meta['x-oss-meta-oss-cek-alg']

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Type: text/plain
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
Accept-Ranges: bytes
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"
x-oss-meta-oss-wrap-alg: {1}
x-oss-meta-oss-cek-alg: {2}
x-oss-meta-oss-crypto-key: {3}
x-oss-meta-oss-crypto-start: {4}
x-oss-meta-unencrypted-content-length: {5}
Last-Modified: Sat, 12 Dec 2015 00:35:53 GMT
x-oss-object-type: Normal

'''.format(len(encrypted_content), wrap_alg, cek_alg, encrypted_key,
           encrypted_iv, len(encrypted_content))

    io = BytesIO()
    io.write(oss2.to_bytes(response_text))
    io.write(encrypted_content)

    response_text = io.getvalue()

    return request_text, response_text
    def test_get_to_file(self, do_request):
        content = unittests.common.random_bytes(1023)

        request_text, response_text = make_get_object(content)
        req_info = unittests.common.mock_response(do_request, response_text)

        filename = self.tempname()

        result = unittests.common.bucket().get_object_to_file(
            'sjbhlsgsbecvlpbf', filename)

        self.assertRequest(req_info, request_text)

        self.assertEqual(result.request_id, '566B6BE93A7B8CFD53D4BAA3')
        self.assertEqual(result.content_length, len(content))
        self.assertEqual(os.path.getsize(filename), len(content))

        with open(filename, 'rb') as f:
            self.assertEqual(content, oss2.to_bytes(f.read()))
    def test_get_to_file_with_progress(self, do_request):
        size = 1024 * 1024 + 1
        content = random_bytes(size)

        request_text, response_text = make_get_object(content)
        req_info = mock_response(do_request, response_text)

        filename = self.tempname()

        self.previous = -1
        bucket().get_object_to_file('sjbhlsgsbecvlpbf',
                                    filename,
                                    progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        self.assertEqual(self.previous, size)
        self.assertEqual(os.path.getsize(filename), size)
        with open(filename, 'rb') as f:
            self.assertEqual(oss2.to_bytes(content), f.read())
def make_get_encrypted_object(content, ranges=None, missing_alg=False):
    request_text = '''GET /sjbhlsgsbecvlpbf HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:PAedG7U86ZxQ2WTB+GdpSltoiTI='''

    cipher = oss2.utils.AESCipher(key=unittests.common.fixed_aes_key,
                                  start=unittests.common.fixed_aes_start)
    encrypted_cont = cipher.encrypt(content)

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Type: text/plain
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
Accept-Ranges: bytes
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"{1}
x-oss-meta-oss-crypto-key: BPNkLSi+htvzrU/d52S9SVHmDFYwKZoepPGRBZ8flm/Wh8oXaRSyVx9vsrQR2aNfy2m5DuNpkIcRP8GUmO7g/RBpl7kU/xJiYjZSjY0dMDjjSB2K0iuX0iHjACFqYsGq7jqYQnSzL03kYPP1Nu8XJFCAax/KLRr8lf0eAPoDLgqo/rIqyI8RkSPgJO29X4Dm2XBayShmvjR5O/TF95N8ql3g+iit4JQovPmlFvu6RFtp8FJpDbS41+VVP7s6c4yfyZoio9VfYJKSKhEZBvlCo44PbY9MFUUH4rYvrwJXCgWmVbfo35gzSF1fRXsaYhB8OIBTJlHxeFUUr/lZv0kuNw==
x-oss-meta-oss-crypto-start: HkRvX7uxN+ASy8eu2mNVayUqElwhde+cp9zw1F4ywBku7sgsphksItRHFySxAwesbAsy7U0cMcQvBt0hHIXIVDJl47GXubbGk/oHuwv58Rry/POdjJ+hbFOoTS0is5GNfyNw7ZjmgxuQ54Yv9gzYCxMpyZ1g35miKi8slikyzkFnkrOQSCHm3T7BF+hDylYQVvrPximdvW6UmHYZWXMzwx4gD43YvXlpfiafqXnkY7lBliIYmP0Ty6a7kwpeKHdkvwIa6wER+Cv0+qiq+KPJFf+iJ+9hx+Z6HWCyV6S+blQqK4ZNbKHApAkQJHAeQb5Cf4SZAQehU8ewwxwzxYn5bw==
x-oss-meta-unencrypted-content-length: {2}
Last-Modified: Sat, 12 Dec 2015 00:35:53 GMT
x-oss-object-type: Normal{3}

'''.format(
        len(encrypted_cont),
        '' if missing_alg else '\nx-oss-meta-oss-cek-alg: AES/GCM/NoPadding',
        len(content), '\nContent-Range: {0}'.format(ranges) if ranges else '')

    io = BytesIO()
    io.write(oss2.to_bytes(response_text))
    io.write(encrypted_cont)

    response_text = io.getvalue()

    return request_text, response_text
    def test_crypto_get_to_file_with_progress(self, do_request):
        size = 1024 * 1024 + 1
        content = unittests.common.random_bytes(size)

        request_text, response_text = make_get_encrypted_object(content)
        req_info = unittests.common.mock_response(do_request, response_text)

        filename = self.tempname()

        self.previous = -1
        unittests.common.bucket(
            oss2.LocalRsaProvider(key='oss-test')).get_object_to_file(
                'sjbhlsgsbecvlpbf',
                filename,
                progress_callback=self.progress_callback)

        self.assertRequest(req_info, request_text)

        self.assertEqual(self.previous, size)
        self.assertEqual(os.path.getsize(filename), size)
        with open(filename, 'rb') as f:
            self.assertEqual(oss2.to_bytes(content), f.read())
def make_get_encrypted_object(content, ranges=None, missing_alg=False):
    request_text = '''GET /sjbhlsgsbecvlpbf HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:PAedG7U86ZxQ2WTB+GdpSltoiTI='''

    cipher = oss2.utils.AESCipher(key=unittests.common.fixed_aes_key, start=unittests.common.fixed_aes_start)
    encrypted_cont = cipher.encrypt(content)

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Type: text/plain
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
Accept-Ranges: bytes
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"{1}
x-oss-meta-oss-crypto-key: BPNkLSi+htvzrU/d52S9SVHmDFYwKZoepPGRBZ8flm/Wh8oXaRSyVx9vsrQR2aNfy2m5DuNpkIcRP8GUmO7g/RBpl7kU/xJiYjZSjY0dMDjjSB2K0iuX0iHjACFqYsGq7jqYQnSzL03kYPP1Nu8XJFCAax/KLRr8lf0eAPoDLgqo/rIqyI8RkSPgJO29X4Dm2XBayShmvjR5O/TF95N8ql3g+iit4JQovPmlFvu6RFtp8FJpDbS41+VVP7s6c4yfyZoio9VfYJKSKhEZBvlCo44PbY9MFUUH4rYvrwJXCgWmVbfo35gzSF1fRXsaYhB8OIBTJlHxeFUUr/lZv0kuNw==
x-oss-meta-oss-crypto-start: HkRvX7uxN+ASy8eu2mNVayUqElwhde+cp9zw1F4ywBku7sgsphksItRHFySxAwesbAsy7U0cMcQvBt0hHIXIVDJl47GXubbGk/oHuwv58Rry/POdjJ+hbFOoTS0is5GNfyNw7ZjmgxuQ54Yv9gzYCxMpyZ1g35miKi8slikyzkFnkrOQSCHm3T7BF+hDylYQVvrPximdvW6UmHYZWXMzwx4gD43YvXlpfiafqXnkY7lBliIYmP0Ty6a7kwpeKHdkvwIa6wER+Cv0+qiq+KPJFf+iJ+9hx+Z6HWCyV6S+blQqK4ZNbKHApAkQJHAeQb5Cf4SZAQehU8ewwxwzxYn5bw==
x-oss-meta-unencrypted-content-length: {2}
Last-Modified: Sat, 12 Dec 2015 00:35:53 GMT
x-oss-object-type: Normal{3}

'''.format(len(encrypted_cont), '' if missing_alg else '\nx-oss-meta-oss-cek-alg: AES/GCM/NoPadding',
           len(content), '\nContent-Range: {0}'.format(ranges) if ranges else '')

    io = BytesIO()
    io.write(oss2.to_bytes(response_text))
    io.write(encrypted_cont)

    response_text = io.getvalue()

    return request_text, response_text
def make_put_encrypted_object(content):

    cipher = oss2.utils.AESCipher(key=unittests.common.fixed_aes_key,
                                  start=unittests.common.fixed_aes_start)
    encrypted_cont = cipher.encrypt(content)

    request_text = '''PUT /sjbhlsgsbecvlpbf.txt HTTP/1.1
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
Content-Type: text/plain
Content-Length: {0}
date: Sat, 12 Dec 2015 00:35:53 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
authorization: OSS ZCDmm7TPZKHtx77j:W6whAowN4aImQ0dfbMHyFfD0t1g=
Accept: */*

'''.format(len(content))

    io = BytesIO()
    io.write(oss2.to_bytes(request_text))
    io.write(encrypted_cont)

    request_text = io.getvalue()

    response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Length: 0
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
x-oss-hash-crc64ecma: {0}
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"'''.format(
        unittests.common.calc_crc(encrypted_cont))

    return request_text, response_text
result = bucket.get_object('motto.txt')

assert result.read() == content

# 生成一个签名的URL,将在60秒后过期
url = bucket.sign_url('GET', 'motto.txt', 60)

print(url)


# 人工构造一个使用V2签名的请求
key = 'object-from-post.txt'

boundary = 'arbitraryboundaryvalue'
headers = {'Content-Type': 'multipart/form-data; boundary=' + boundary}
encoded_policy = oss2.utils.b64encode_as_string(oss2.to_bytes('{ "expiration": "%s","conditions": [["starts-with", "$key", ""]]}'
         % oss2.date_to_iso8601(datetime.datetime.utcfromtimestamp(int(time.time()) + 60))))

digest = hmac.new(oss2.to_bytes(access_key_secret), oss2.to_bytes(encoded_policy), hashlib.sha256).digest()
signature = oss2.utils.b64encode_as_string(digest)

form_fields = {
    'x-oss-signature-version': 'OSS2',
    'x-oss-signature': signature,
    'x-oss-access-key-id': access_key_id,
    'policy': encoded_policy,
    'key': key,
}

# 对象的内容
content = 'file content for post object request'
Пример #47
0
<WebsiteConfiguration>
    <IndexDocument>
        <Suffix>index2.html</Suffix>
    </IndexDocument>

    <ErrorDocument>
        <Key>error2.html</Key>
    </ErrorDocument>
</WebsiteConfiguration>
'''
bucket.put_bucket_website(xml)

# 方法三:可以从本地文件读取XML配置
# oss2.to_bytes()可以把unicode转换为bytes
with open('website_config.xml', 'wb') as f:
    f.write(oss2.to_bytes(xml))

with open('website_config.xml', 'rb') as f:
    bucket.put_bucket_website(f)

os.remove('website_config.xml')


# 获取配置
# 因为是分布式系统,所以配置刚刚设置好,可能还不能立即获取到,先等几秒钟
time.sleep(5)

result = bucket.get_bucket_website()
assert result.index_file == 'index2.html'
assert result.error_file == 'error2.html'
Пример #48
0
def random_bytes(n):
    return oss2.to_bytes(random_string(n))
Пример #49
0
# 生成一个签名的URL,将在60秒后过期
url = bucket.sign_url('GET', 'motto.txt', 60)

print(url)


# 人工构造一个使用V2签名的请求
key = 'object-from-post.txt'

boundary = 'arbitraryboundaryvalue'
headers = {'Content-Type': 'multipart/form-data; boundary=' + boundary}
encoded_policy = oss2.utils.b64encode_as_string('{ "expiration": "%s","conditions": [["starts-with", "$key", ""]]}'
         % oss2.date_to_iso8601(datetime.datetime.utcfromtimestamp(int(time.time()) + 60)))

digest = hmac.new(oss2.to_bytes(access_key_secret), oss2.to_bytes(encoded_policy), hashlib.sha256).digest()
signature = oss2.utils.b64encode_as_string(digest)

form_fields = {
    'x-oss-signature-version': 'OSS2',
    'x-oss-signature': signature,
    'x-oss-access-key-id': access_key_id,
    'policy': encoded_policy,
    'key': key,
}

# 对象的内容
content = 'file content for post object request'

body = ''
Пример #50
0
    def assertXmlEqual(self, a, b):
        normalized_a = minidom.parseString(oss2.to_bytes(a)).toxml(encoding='utf-8')
        normalized_b = minidom.parseString(oss2.to_bytes(b)).toxml(encoding='utf-8')

        self.assertEqual(normalized_a, normalized_b)
Пример #51
0
    def __init__(self, status, headers, body):
        self.status = status
        self.headers = oss2.CaseInsensitiveDict(headers)
        self.body = oss2.to_bytes(body)

        self.offset = 0
Пример #52
0
def random_bytes(n):
    return oss2.to_bytes(random_string(n))
access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '<你的AccessKeyId>')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '<你的AccessKeySecret>')
bucket_name = os.getenv('OSS_TEST_BUCKET', '<你的Bucket>')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '<你的访问域名>')


# 确认上面的参数都填写正确了
for param in (access_key_id, access_key_secret, bucket_name, endpoint):
    assert '<' not in param, '请设置参数:' + param


# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

key = 'motto.txt'
content = oss2.to_bytes('a' * 1024 * 1024)
filename = 'download.txt'

# 上传文件
bucket.put_object(key, content, headers={'content-length': str(1024 * 1024)})

"""
文件下载
"""

# 下载文件
result = bucket.get_object(key)

# 验证一下
content_got = b''
for chunk in result:
Пример #54
0
<WebsiteConfiguration>
    <IndexDocument>
        <Suffix>index2.html</Suffix>
    </IndexDocument>

    <ErrorDocument>
        <Key>error2.html</Key>
    </ErrorDocument>
</WebsiteConfiguration>
'''
bucket.put_bucket_website(xml)

# 方法三:可以从本地文件读取XML配置
# oss2.to_bytes()可以把unicode转换为bytes
with open('website_config.xml', 'wb') as f:
    f.write(oss2.to_bytes(xml))

with open('website_config.xml', 'rb') as f:
    bucket.put_bucket_website(f)

os.remove('website_config.xml')

# 获取配置
# 因为是分布式系统,所以配置刚刚设置好,可能还不能立即获取到,先等几秒钟
time.sleep(5)

result = bucket.get_bucket_website()
assert result.index_file == 'index2.html'
assert result.error_file == 'error2.html'

# 取消静态网站托管模式
Пример #55
0
# 确认上面的参数都填写正确了
for param in (access_key_id, access_key_secret, bucket_name, endpoint):
    assert '<' not in param, '请设置参数:' + param


# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)


def random_string(n):
    return ''.join(random.choice(string.ascii_lowercase) for i in range(n))

# 生成一个本地文件用于测试。文件内容是bytes类型。
filename = random_string(32) + '.txt'
content = oss2.to_bytes(random_string(1024 * 1024))

with open(filename, 'wb') as fileobj:
    fileobj.write(content)

# 断点续传一:因为文件比较小(小于oss2.defaults.multipart_threshold),
# 所以实际上用的是oss2.Bucket.put_object
oss2.resumable_upload(bucket, 'remote-normal.txt', filename)

# 断点续传二:为了展示的需要,我们指定multipart_threshold可选参数,确保使用分片上传
oss2.resumable_upload(bucket, 'remote-multipart.txt', filename, multipart_threshold=100 * 1024)


# 也可以直接调用分片上传接口。
# 首先可以用帮助函数设定分片大小,设我们期望的分片大小为128KB
total_size = os.path.getsize(filename)