首页 > 解决方案 > Boto3/S3/Django:在计算文件哈希时避免阻塞事件循环

问题描述

从 gunicorn+gevent 工作人员,通过django- storages 进行(多部分)上传到 S3 ,如何确保在计算文件/部分内容的哈希期间,事件循环不被阻塞?

查看 django-storages的源代码,实际上似乎没有任何传递 md5(/另一个哈希)的范围,并且在botocore 的源代码中似乎没有产生事件循环的范围,而正在计算哈希。

标签: djangoamazon-s3boto3gunicorngevent

解决方案


现在有一个用于 django-storages 的 PR,它计算 MD5 哈希作为应用程序接收到的数据

总之,这改变S3Boto3StorageFile了 init 和 new 部分

self._file_md5 = hashlib.md5()

然后收到content_bytes

self._file_md5.update(content_bytes)

然后在每个part上传时,将ContentMD5参数传递给part upload函数

ContentMD5=base64.b64encode(self._file_md5.digest()).decode('utf-8')

推荐阅读