Search code examples
djangoamazon-s3boto3botocorepython-django-storages

Uploading CSV files to S3 directly through Django


I am creating a django application which is connected to Amazon S3 to save user upload files. This is done using django-storages.

During the course of the app a CSV file is generated. I am able to save the file locally. But how can I upload the file directly to S3 without saving it locally and save the file S3 URL in the database at the same time?

I have tried using StringIO but that generates a TypeError: Unicode objects must be encoded before hashing

Any suggestions/solutions will be much appreciated!


Solution

  • // Install boto3 first
    import boto3
    
    s3 = boto3.client('s3',
                      aws_access_key_id=AWS_ACCESS_KEY_ID,
                      aws_secret_access_key=AWS_SECRET_ACCESS_KEY, )
    
    // ...... Get your file or open file ......
    file_name = your_file_here
    // ......
    
    s3.upload_file(file_name, AWS_STORAGE_BUCKET_NAME,
                   path_where_you_want_to_store))
    
    // Ex: s3.upload_file(file_name, AWS_STORAGE_BUCKET_NAME,
    //                           '%s/%s' % ('media/user/file', file_name))