django和boto3的AWS S3图像上传问题

时间:2019-05-03 13:25:22

标签: python django amazon-web-services amazon-s3

我想上传图像并将上传的图像重新加载到我的webapp中,先前在S3存储桶(我手动上传)中加载的图像在页面中呈现良好,但是我在S3中上传图像时遇到问题。

下面是我的settings.py配置

INSTALLED_APPS = [


    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'storages',
]

AWS_ACCESS_KEY_ID = "given id"
AWS_SECRET_ACCESS_KEY = "given secret key"
AWS_STORAGE_BUCKET_NAME = "young-minds-files"
AWS_S3_URL = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_REGION_NAME = "us-west-2"

AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None

DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"

views.py

class PostCreateView(LoginRequiredMixin,CreateView):
    model = Post
    fields = ["title","content","image1","image2","image3"]
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    category = models.CharField(max_length=100, default='Others')
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    image1 = models.ImageField(default='default_content.jpg', upload_to='content_pic')
    image2 = models.ImageField(default='default_content.jpg', upload_to='content_pic')
    image3 = models.ImageField(default='default_content.jpg', upload_to='content_pic')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk':self.pk})

模板

{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% load static %}
<section class="site-section">
  <div class="container">
    <div class="row mb-4">
      <div class="col-md-6">

      </div>
    </div>
    <div class="row blog-entries">
      <div class="col-md-12 col-lg-8 main-content">

        <form method="post" enctype="multipart/form-data">
          <fieldset class="form-group">
            <legend class="border-bottom mb-4"> Create Your Blog</legend>
            {% csrf_token %}
            {{ form|crispy }}
          </fieldset>
          <div class="row">
            <div class="col-md-6 form-group">
              <button type="submit" class="btn btn-primary">Post</button>
            </div>
          </div>

        </form>

      </div>
    </section >
    <!-- END main-content -->

    {% endblock content %}

以下是我尝试上传图片时的错误消息

enter image description here

控制台错误:

botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://young-minds-california.s3.us-west-1.amazonaws.com/content_pic/img_5.jpg"

更新:

我已经解决了这个问题,唯一的问题是我网络中的防火墙。

0 个答案:

没有答案