在Django中仅生成s3客户端generate_presigned_post,其内容类型用于mp4文件上传

Anj*_*uri 0 python django video mp4 amazon-s3

上传视频(.mp4)文件后,S3 元数据设置其 内容类型:二进制/八位字节流 ,使我的浏览器强制下载文件而不是播放为了使其在浏览器中播放,我需要更改该内容类型以编程方式到django中的“ video/mp4

Anj*_*uri 6

经过大量时间的努力后,我得出了解决方案:{

def presign_post_S3_url(self, key=None, is_public=False):
    acl = 'private'

    if is_public:
        acl = 'public-read'
    fields = {"acl": acl, "Content-Type": "video/mp4"}
    conditions = [
        {"acl": acl},
        ["starts-with", "$Content-Type", ""]
    ]
    if key is None:
        return ""
   ##s3_client is boto3.client object
    s3_client = self.get_s3_client()
    if s3_client is None:
        return ""
    data = s3_client.generate_presigned_post(
        Bucket=self.bucket,
        Key=key,
        Fields=fields,
        Conditions=conditions

    )
    return data
Run Code Online (Sandbox Code Playgroud)