django - 如何使用 Django Rest Framework 将上传进度条与 s3 存储桶

我正在研究 REST API(使用 Django Rest Framework)。我正在尝试通过向我创建的端点发送发布请求来上传视频。

问题

视频确实上传到 s3 存储桶,但上传进度仅在几秒钟内显示 100%,无论我上传的文件很大。

为什么会发生这种情况,我该如何解决?

PS:之前我是在本地上传,上传进度正常。 我正在使用 React。

最佳答案

首先你要确保你已经安装了这些库:boto3==1.14.53, botocore==1.17.53, s3transfer== 0.3.3, django-storages==1.10

设置.py:

INSTALLED_APPS = [
 
    'storages',
]


AWS_ACCESS_KEY_ID = 'your-key-id'
AWS_SECRET_ACCESS_KEY = 'your-secret-key'
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
DEFAULT_FILE_STORAGE = 'your_project-name.storage_backends.MediaStorage'

MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN

#File upload setting
BASE_URL = 'http://example.com'
FILE_UPLOAD_PERMISSIONS = 0o640
DATA_UPLOAD_MAX_MEMORY_SIZE = 500024288000

然后在 settings.py 文件所在的项目文件夹中创建一个 storage_backends python 文件。

存储后端.py:

import os
from tempfile import SpooledTemporaryFile

from storages.backends.s3boto3 import S3Boto3Storage


class MediaStorage(S3Boto3Storage):
    bucket_name = 'your-bucket-name'
    file_overwrite = False

    def _save(self, name, content):
        """
        We create a clone of the content file as when this is passed to
        boto3 it wrongly closes the file upon upload where as the storage
        backend expects it to still be open
        """
        # Seek our content back to the start
        content.seek(0, os.SEEK_SET)

        # Create a temporary file that will write to disk after a specified
        # size. This file will be automatically deleted when closed by
        # boto3 or after exiting the `with` statement if the boto3 is fixed
        with SpooledTemporaryFile() as content_autoclose:
            # Write our original content into our copy that will be closed by boto3
            content_autoclose.write(content.read())

            # Upload the object which will auto close the
            # content_autoclose instance
            return super(MediaStorage, self)._save(name, content_autoclose)


关于django - 如何使用 Django Rest Framework 将上传进度条与 s3 存储桶上的上传同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64503327/

相关文章:

assembly - grub 似乎没有正确加载我的内核

python - Vscode - .env 文件中的变量扩展

spring-boot - 线程 "main"java.lang.NoSuchMethodError

ios - Fabric Crashlytics 不工作。 iOS 14 应用/XCode 12

python - 如何使用 nordvpn 服务器作为 python 请求的代理?

google-sheets - 如何让条形图数据标签在谷歌表格中显示值和百分比(总数)?

amazon-web-services - 连接到 appsync 的 Lambda 总是返回 La

angular - 如何限制angular app在iframe中加载

html - 打破 PDF 中的 html 表格

ansible - 错误!操作中的意外参数类型 :