如何使用Django配置AWS s3以上传文件AWS ec2实例

我目前正在将RHEL8安装为OS的AWS EC2服务器上运行Django项目。我的AWS环境配置为将静态文件和媒体文件都存储到S3中。我的django应用模型利用ImageField上传图像并存储在S3中。尝试通过django应用程序将图像保存到S3时,我当前收到权限被拒绝的HTTP 500连接错误,问题不是我的django应用程序,因为我在相同的配置下测试了完全相同的应用程序(setting.py)我的本地主机和我可以成功将静态文件和媒体文件都上传到s3。为什么它可以在我的本地主机上运行,​​而不在我的EC2实例中运行?该问题特定于上传映像时通过EC2连接到S3,因为我从我的EC2实例上传静态文件到S3时没有问题。

在对上述问题进行故障排除时,我想排除该问题与我的Django应用程序(也许是我的settings.py文件)有关。我在本地主机(笔记本电脑)上安装了完全相同的django项目,并能够通过django应用程序将静态文件和图像文件上传到S3存储桶。结果是我的EC2实例在从django应用程序上传映像时,特别是在连接到s3存储桶时遇到问题,所以我的问题是我的AWS配置带有S3(权限)或EC2(wsgi)。不知道从哪里开始。

s3桶式测试仪存储桶策略

{
"Version": "2012-10-17","Statement": [
    {
        "Effect": "Allow","Principal": "*","action": "s3:*","Resource": [
            "arn:aws:s3:::testerstorage","arn:aws:s3:::testerstorage/*"
        ]
    }
]
}

s3 CORS配置

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

跟踪错误

  Internal Server Error: /admin/photoapp/photo/add/
  Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/urllib3/connection.py",line 171,in _new_conn
      (self._dns_host,self.port),self.timeout,**extra_kw)
  File "/usr/lib/python3.6/site-packages/urllib3/util/connection.py",line 79,in create_connection
       raise err
  File "/usr/lib/python3.6/site-packages/urllib3/util/connection.py",line 69,in create_connection
       sock.connect(sa)
  PermissionError: [Errno 13] Permission denied

  During handling of the above exception,another exception occurred:

  Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/botocore/httpsession.py",line 263,in send
       chunked=self._chunked(request.headers),File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py",line 638,in urlopen
       _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3.6/site-packages/urllib3/util/retry.py",line 343,in increment
       raise six.reraise(type(error),error,_stacktrace)
  File "/usr/lib/python3.6/site-packages/urllib3/packages/six.py",line 693,in reraise
       raise value
  File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py",line 600,in urlopen
       chunked=chunked)
  File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py",in _make_request
       self._validate_conn(conn)
  File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py",line 849,in _validate_conn
       conn.connect()
  File "/usr/lib/python3.6/site-packages/urllib3/connection.py",line 314,in connect
       conn = self._new_conn()
  File "/usr/lib/python3.6/site-packages/urllib3/connection.py",line 180,in _new_conn
       self,"Failed to establish a new connection: %s" % e)

  urllib3.exceptions.NewConnectionError: <botocore.awsrequest.AWSHTTPSConnection object at 0x7f3a7f847240>: Failed to establish a new connection:

  During handling of the above exception,another exception occurred:

  Traceback (most recent call last):
  File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/exception.py",line 34,in inner
       response = get_response(request)
  File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py",line 115,in _get_response
       response = self.process_exception_by_middleware(e,request)
  File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py",line 113,in _get_response
       response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "/usr/local/lib64/python3.6/site-packages/django/contrib/admin/options.py",line 606,in wrapper
       return self.admin_site.admin_view(view)(*args,**kwargs)
  File "/usr/local/lib64/python3.6/site-packages/django/utils/decorators.py",line 142,in _wrapped_view
       response = view_func(request,*args,**kwargs)
  File "/usr/local/lib64/python3.6/site-packages/django/views/decorators/cache.py",line 44,in _wrapped_view_func
       response = view_func(request,**kwargs)
ljtyhuang 回答:如何使用Django配置AWS s3以上传文件AWS ec2实例

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3155342.html

大家都在问