简体   繁体   English

直接上传到 S3

[英]Uploading directly to S3

I tried very hard to implement this tutorial on my Django project but didn't succeed.我非常努力地在我的 Django 项目上实施本教程,但没有成功。 I think that the function I use to sign the post is not working properly.我认为我用来签署帖子的功能无法正常工作。 This my function :这是我的功能:

def sign_s3(request,*args, **kwargs):
  S3_BUCKET = getattr(settings, 'FILEMANAGER_AWS_S3_BUCKET_NAME')

  file_name = request.GET.get('file_name')
  file_type = request.GET.get('file_type')

  s3 = boto3.client('s3', 
                    config = S3ClientCfg(signature_version = 's3v4'),
                    aws_access_key_id=AWS_ACCESS_KEY_ID,
                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                    )
  # s3 = get_aws_s3_client()

  presigned_post = s3.generate_presigned_post(
    Bucket = S3_BUCKET,
    Key = file_name,
    Fields = {"acl": "public-read", "Content-Type": file_type},
    Conditions = [
      {"acl": "public-read"},
      {"Content-Type": file_type}
    ],
    ExpiresIn = 3600
  )
  response_dict = {
    'data': presigned_post,
    'url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, file_name)
  }
  mimetype = 'application/json'
  return HttpResponse(json.dumps(response_dict), mimetype)

Actually, when I look into the web console I see :实际上,当我查看 Web 控制台时,我看到:

Cross-Origin Request Blocked. (Reason: CORS request did not succeed)

Actually I found out what is the problem.其实我发现了问题所在。 In my signature I mentioned that the URL will expire in 3600s.在我的签名中,我提到 URL 将在 3600 秒后过期。 You have to signal this in the CORS as well.您还必须在 CORS 中发出信号。

My Bucket CORS should be as bellow :我的桶 CORS 应该如下:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3600</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Check the CORS configuration of your S3 bucket, CORS Configuration can be found in the permissions tab of your S3 bucket in the AWS console.检查 S3 存储桶的 CORS 配置,可以在 AWS 控制台中 S3 存储桶的权限选项卡中找到 CORS 配置。

You will probably see the default:您可能会看到默认值:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
</CORSRule>
</CORSConfiguration>

You will need to update this to allow your website (www.example.com) to POST/PUT: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTcors.html您需要更新它以允许您的网站 (www.example.com) 发布/放置: https : //docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTcors.html

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
</CORSRule>
<CORSRule>
    <AllowedOrigin>http://www.example.com</AllowedOrigin>

    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>

    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM