title: File upload problem with AWS using Django
body: <p>I have deployed my django app through cpanel. Static and media files are correctly served through AWS S3. But when i try to upload a file/pic it is not uploading and shows no error and redirects to home page with the url of the page on which file was uploaded.
  Home page URL: <a href="http://www.example.com" rel="nofollow noreferrer">www.example.com</a>, Upload page URL:<a href="http://www.example.com/upload" rel="nofollow noreferrer">www.example.com/upload</a>.</p>
  <p>Bucket access is public. IAM user has full access to S3
  I will be very thankful to you for your precious time and help.</p>
  <pre><code>CORS Policy
  
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;CORSConfiguration xmlns=&quot;http://s3.amazonaws.com/doc/2006-03-01/&quot;&gt;
  &lt;CORSRule&gt;
      &lt;AllowedOrigin&gt;*&lt;/AllowedOrigin&gt;
      &lt;AllowedMethod&gt;GET&lt;/AllowedMethod&gt;
      &lt;AllowedMethod&gt;POST&lt;/AllowedMethod&gt;
      &lt;AllowedMethod&gt;PUT&lt;/AllowedMethod&gt;
      &lt;AllowedHeader&gt;*&lt;/AllowedHeader&gt;
  &lt;/CORSRule&gt;
  &lt;/CORSConfiguration&gt;
  
  </code></pre>
  <pre><code>Bucket Policy
  {
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Id&quot;: &quot;Policy1603207554013&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Sid&quot;: &quot;Stmt1603207062804&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Principal&quot;: &quot;*&quot;,
              &quot;Action&quot;: &quot;s3:GetObject&quot;,
              &quot;Resource&quot;: &quot;arn:aws:s3:::django-bucket/*&quot;
          },
          {
              &quot;Sid&quot;: &quot;Stmt1603207552031&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Principal&quot;: {
                  &quot;AWS&quot;: &quot;arn:aws:iam::085459706277:user/django_user&quot;
              },
              &quot;Action&quot;: &quot;s3:*&quot;,
              &quot;Resource&quot;: [
                  &quot;arn:aws:s3:::django-bucket/*&quot;,
                  &quot;arn:aws:s3:::django-bucket&quot;
              ]
          }
      ]
  }
  </code></pre>
  <pre><code>settings.py
  
  AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
  AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
  AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
  AWS_DEFAULT_ACL = None
  AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
  AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
  # s3 static settings
  STATIC_LOCATION = 'static'
  STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
  STATICFILES_STORAGE = 'stock_app.storage_backends.StaticStorage'
  # s3 public media settings
  PUBLIC_MEDIA_LOCATION = 'media'
  MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/'
  DEFAULT_FILE_STORAGE = 'stock_app.storage_backends.PublicMediaStorage'
  </code></pre>
  <pre><code>storage_backends.py
  
  from storages.backends.s3boto3 import S3Boto3Storage
  from django.conf import settings
  
  
  class StaticStorage(S3Boto3Storage):
      location = 'static'
      default_acl = 'public-read'
  
  
  class PublicMediaStorage(S3Boto3Storage):
      location = 'media'
      default_acl = 'public-read'
      file_overwrite = False
  </code></pre>
  <pre><code>models.py
  
  class Upload(models.Model):
      file = models.FileField(upload_to='data base')
  
      def __str__(self):
          return f'{self.file} Success'
  </code></pre>
  <pre><code>forms.py
  
  class UploadFileForm(forms.ModelForm):
      class Meta:
          model = Upload
          fields = ['file']
  </code></pre>
  <pre><code>views.py
  
  @login_required
  def upload_file(request):
  
      if request.method == 'POST':
          form = UploadFileForm(request.POST, request.FILES)
          if form.is_valid():
              file = request.FILES['file'] 
              output = files_to_database(file) 
  
              if not output: #checks to see if file is not already in database 
                  form.save()
              
              return redirect('upload')
      else:
          form = UploadFileForm()
          
  
      return render(request, 'stock_app/upload.html', {'form': form})
  </code></pre>