odoo 13通过Python的minio api上传视频文件

问题描述

我正在尝试开发一个模块,以通过Python的MinIO API上传视频文件

文件可以上传到MinIO,但是无法通过url进行查看,例如:http:// localhost:9000 / lms-videos / video / output.mp4。另外,通过MinIO上传文件应为19.39Mb,通过API上传文件应为25 + MB,不知道是什么原因.....

以下是我的代码的一部分:

# minio client
@api.model
def _get_minio_client(self):
    host = '192.168.1.102:9000'
    access_key = 'minioadmin'
    secret_key = 'minioadmin'
    if not all((host,access_key,secret_key)):
        raise exceptions.UserError('Incorrect configuration of MinIO')
    return Minio(
    host,access_key = access_key,secret_key = secret_key,secure = False
    )
# upload
@api.model
def _store_file_write(self):
    client = self._get_minio_client()
    bin_data = self.datas_minio
    fname = "output_test"
    #client.put_object('lms-videos','videos/'+ fname + '.mp4',io.BytesIO(self.datas_minio),len(bin_data),'video/mp4')
    with io.BytesIO(self.datas_minio) as bin_data_io:
        client.put_object('lms-videos',bin_data_io,'video/mp4')

@api.depends('document_id','slide_type','mime_type','external_url')
def _compute_embed_code(self):
    res = super(Slide,self)._compute_embed_code()
    for record in self:
        if record.slide_type == 'miniovideo':
            
            self._store_file_write()
            
            content_url = 'http://localhost:9000/lms-videos/videos/' + record.name + '.mp4'
            record.embed_code = '<video class="miniovideo" controls controlsList="nodownload"><source src="' + content_url + '" type=MPEG-4/></video>'
        
@api.onchange('datas_minio')
def _on_change_datas(self):
    res = super(Slide,self)._on_change_datas()
    if self.datas_minio:
        #fname = self.datas_minio.decode("utf-8")
        #bin_data = self.datas_minio
        self._store_file_write()
        #self._get_minio_client().put_object('lms-videos','/videos/'+ fname + '.mp4',io.BytesIO(bin_data),'video/mp4')
    return res

解决方法

通过添加b64decode解决的问题

@api.model
    def _store_file_write(self):
        client = self._get_minio_client()
        bin_data = base64.b64decode(self.datas_minio)
        fsize = len(bin_data)
        fname = "output_test"
       
        with io.BytesIO(bin_data) as bin_data_io:
            client.put_object('lms-videos',#'videos/'+ fname + '.mp4','videos/output_test.mp4',bin_data_io,fsize,'video/mp4')