'Generate file from Base64 encoded django file read

During a file upload, i decided to read the file and save as base64 until s3 becomes available to our team. I use the code below to convert the file to bs64.

def upload_file_handler(file):
    """file -> uploaded bytestream from django"""
    bs4 = base64.b64encode(file.read())
    return {'binary': bs4, 'name': file.name}

I store the binary derived from the above in a str to a db. Now the challenge is getting the file back and uploading to s3.

I attempted to run bs64.decode on the file string from the db and write to a file. But when i open the file, it seems broken, I've attempted with breakthrough.

    q = Report.objects.first()
    data = q.report_binary
    f = base64.b64decode(data)
    content_file = ContentFile(f, name="hello.docx")
    instance = TemporaryFile(image=content_file)
    instance.save()

This is one of the files i am trying to recreate from the binary.

https://gist.github.com/saviour123/38300b3ff2c7a0d1a01c15332c583e20

How can i generate the file from the base64 binary?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source