pythonswiftpython-3.xopenstackopenstack-swift

Want to upload a sqlite.db file to a swift container using python swiftclient and always get a utf-8 error


i am trying to upload a sqlite.db(binary file) to a swift container using swiftclient in my python code.

import swiftclient swift_conn.put_object

File "/usr/lib/python3.7/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbc in position 43: invalid start byte

the code i am using is:

import swiftclient bmdatabase = "./logs/test.db' with open(bmdatabase, 'r') as bmdatabase_file: #remote correctbmdatabasename = bmdatabase.replace("./logs/", "") swift_conn.put_object(container_name,correctbmdatabasename, contents=bmdatabase_file.read())


Solution

  • I finally found it by myself, if I want to read a binary file I have to read it with 'rb'

    like

    import swiftclient
    bmdatabase = "./logs/test.db'
        with open(bmdatabase, 'rb') as bmdatabase_file:
                    #remote
                    correctbmdatabasename = bmdatabase.replace("./logs/", "")
                    swift_conn.put_object(container_name,correctbmdatabasename,
                                          contents=bmdatabase_file.read())