[Python] Problem uploading images to S3 Bucket from Django App [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    My problem is that images stored in media folder are not transferring to S3 Bucket. I tested with other file from request and the file did transfer, so I assume settings.py must be OK.

    From views.py ->

    This works:

    if request.method == 'POST':
    imageFile = request.FILES['images']
    upload = Upload(file=imageFile)
    upload.save()
    image_url = upload.file.url
    print(image_url)


    This does not work:

    for i in os.listdir(folder):
    f = os.path.join(conf_settings.MEDIA_ROOT,company, i)
    upload = Upload(file=f)
    upload.save()


    No error but it just does not work.
    ​

    This also does not work:

    for i in os.listdir(folder):
    with open(os.path.join(folder, i)) as f:
    upload = Upload(file=f)
    upload.save()



    The error I am getting is:


    Exception Value:
    '_io.TextIOWrapper' object has no attribute '_committed'

    at upload.save()
    ​

    This is my storage_backend.py

    from django.conf import settings
    from storages.backends.s3boto3 import S3Boto3Storage



    class MediaStorage(S3Boto3Storage):
    location = 'media'
    default_acl = 'public-read'
    file_overwrite = True


    This is my model.py

    class Upload(models.Model):

    uploaded_at = models.DateTimeField(auto_now_add=True)
    file = models.FileField()


    I am uploading a .ZIP file with images. Unzipping it and saving them to media folder, then I want to upload from media folder to S3 Bucket. This operation fails.

    The file in request.FILES is the Zip file, which I am using to test that all settings.py for AWS should be correct because it does transfer correctly.

    I believe my issue has to do with the way I am reading the file and passing it.

    Continue reading...

Compartilhe esta Página