将大文件从 Django 上传到云存储

Upload large file from django to cloud storage

我在云 运行 上有一个 django 应用程序,我想创建一个将由另一个 python 脚本调用的端点。此端点应将文件保存到 Google 存储。文件大小最大为 800Mb。

所以当我尝试这样做时,我收到:413 请求实体太大。

因此,通过挖掘互联网,我了解到我应该使用块文件。但是有一点不明白..

据此:https://github.com/django/daphne/issues/126我了解到daphne现在可以收到大号的body了。所以,我认为,即使收到一个大文件,Django 也设法将其分块并逐个发送。

我很好奇,除了手动块之外,还有什么可以做我想做的吗?

现在我将此添加到我的设置中:

GS_BLOB_CHUNK_SIZE = 524288
DATA_UPLOAD_MAX_MEMORY_SIZE = 26214400
FILE_UPLOAD_MAX_MEMORY_SIZE = 26214400

我只是使用 generics.ListCreateAPIView 和文件上传处理程序的默认值。

一般是413 error means a size limit in a request has been exceeded. For Cloud Run, the quota for requests is 32mb. According to the documentation, the recommended way of uploading large files is through providing a Signed URL to the Cloud Storage Bucket, since Signed URLs can be used for resumable uploads:

Resumable uploads are the recommended method for uploading large files, because you do not have to restart them from the beginning if there is a network failure while the upload is underway.

您可以 generate a signed URL from your server backend and use it to upload a file without restrictions from your client side script. There appears to be other related questions 云中的 Django 服务器 运行 有上传限制,建议使用签名 URLS 来处理这些情况。