S3 存储桶 CORS 策略 Django [英] S3 buckets CORS policy Django

查看:62
本文介绍了S3 存储桶 CORS 策略 Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个 s3 存储桶,以便我的 Django 应用程序可以上传和抓取用户媒体文件.我已经参与了此视频 https://www.youtube 中的部分.com/watch?v=kt3ZtW9MXhw&t=224s 设置 cors 策略的位置.

在本教程中,他使用 XML,但 aws 现在需要 json.我不知道该放什么.我希望我的网站能够进行 GET、PUT、POST、PATCH 和 DELETE.

我的应用是一个托管在 Digital Ocean 液滴上的 Django 应用

我找到了一个例子,我已经编辑并添加到我的配置中

<预><代码>[{允许的标题":[*"],允许的方法":[放置",邮政",删除"],允许的起源":[http://www.example1.com"],ExposeHeaders":[]},{允许的标题":[*"],允许的方法":[放置",邮政",删除"],允许的起源":[http://www.example2.com"],ExposeHeaders":[]},{允许的标题":[],允许的方法":[获取"],允许的起源":[*"],ExposeHeaders":[]}]

解决方案

用于配置 cors:-

  1. 您需要授予 对存储桶的公共访问权限.(通过存储桶策略和控制台) https://stackoverflow.com/a/4709391/13126651(用于公共访问存储桶策略)
  2. 确保将您的存储桶用作网站,然后在您的存储桶 cors 设置下附加此策略.

<预><代码>[{允许的标题":[授权"],允许的方法":[获取"],允许的起源":[<第一个桶的 url,带有 http://...末尾没有斜线>";],ExposeHeaders":[],MaxAgeSeconds":3000}]

I'm trying to set up an s3 bucket so my Django app can upload and grab user media files. I've come to the part in this video https://www.youtube.com/watch?v=kt3ZtW9MXhw&t=224s where you set the cors policy.

In this tutorial he uses XML but aws now wants json. I don't know what to put. I want my website to be able to GET, PUT, POST, PATCH and DELETE.

My app is a Django app hosted on a Digital Ocean droplet

Edit: I've found an example which I've edited and added to my config

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "http://www.example1.com"
        ],
        "ExposeHeaders": []
    },
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "http://www.example2.com"
        ],
        "ExposeHeaders": []
    },
    {
        "AllowedHeaders": [],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

解决方案

For configuring cors:-

  1. you need to give public access to bucket.( through bucket policy and console) https://stackoverflow.com/a/4709391/13126651 ( for public access bucket policy)
  2. make sure to enable your bucket to be used as website and then attach this policy, under your bucket cors setting.

[
    {
        "AllowedHeaders": [
            "Authorization"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "<url of first bucket with http://...without slash at the end>"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

这篇关于S3 存储桶 CORS 策略 Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆