File size: 3,315 Bytes
2abfccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# distutils: language = c++

from libcpp.string cimport string
from libcpp.vector cimport vector
from s3client cimport S3Client, get_error_list
from s3client cimport init_api as _init_api
from s3client cimport shutdown_api as _shutdown_api


ERROR_LIST = get_error_list()
ERROR_MAP = {k:v.decode('utf-8') for k,v in ERROR_LIST}

def init_api(log_level):
    log_level = log_level.lower()
    _init_api(log_level.encode('utf-8'))

def shutdown_api():
    _shutdown_api()

class S3Error(Exception):
    def __init__(self, error_name, error_message):
        self.error_name = error_name
        self.error_message = error_message

cdef class PyS3Client:
    cdef S3Client *client

    def __cinit__(self, string ak, string sk, string endpoint, bint verify_ssl, bint enable_https, bint use_dual_stack, int threads_num):
        self.client = new S3Client(ak, sk, endpoint, verify_ssl, enable_https, use_dual_stack, threads_num)
    
    def __dealloc__(self):
        del self.client

    def get_object(self, string bucket, string key, string range):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.get_object(bucket, key, error_type, error_message, result, range)
        if ret == 0:
            return result
        else:
            error_name = ERROR_MAP.get(error_type, 'Undefined')
            raise S3Error(error_name, error_message)

    def multipart_download_concurrency(self, string bucket, string key, string filename):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.multipart_download_concurrency(bucket, key, filename, error_type, error_message)
        if ret == 0:
            return ret
        else:
            error_name = ERROR_MAP.get(error_type, 'Undefined')
            raise S3Error(error_name, error_message)

    def put_object(self, string bucket, string key, string data):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.put_object(bucket, key, data, error_type, error_message)
        if ret == 0:
            return data.size()
        else:
            error_name = ERROR_MAP.get(error_type, 'Undefined')
            raise S3Error(error_name, error_message)


    def multipart_upload_concurrency(self, string bucket, string key, string filename):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.multipart_upload_concurrency(bucket, key, filename, error_type, error_message)
        if ret == 0:
            return ret
        else:
            error_name = ERROR_MAP.get(error_type, 'Undefined')
            raise S3Error(error_name, error_message)

    def delete(self, string bucket, string key):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.delete_obj(bucket, key, error_type, error_message)
        return ret

    def contains(self, string bucket, string key):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.contains(bucket, key, error_type, error_message)
        return ret

    def list(self, string bucket, string key):
        cdef string error_message, result
        cdef int error_type
        ret = self.client.list(bucket, key, error_type, error_message)
        return ret