Home > OS >  How to upload a CSV file that is created in Django to AWS S3?
How to upload a CSV file that is created in Django to AWS S3?

Time:03-15

I have a Django model that has a FileField, the file has to be saved in AWS S3.
models.py:

class Search(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='searches')
type = models.CharField(max_length=150)
keyword = models.CharField(max_length=150)
amount = models.IntegerField()
date = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=50)
results = models.FileField(blank=True, storage=PrivateMediaStorage())

The upload to AWS S3 works when I create a new object in the admin area and upload a file there. However, I want to create a CSV file in the backend and upload it to S3 from there, not using any form.
Inside the backend function:

    with open(f'results{search_id}.csv', 'w', newline='') as f:
    writer = csv.writer(f)

    header = ['name', 'email', 'follower_count', 'play count', 'share count', 'comment count', 'has email']
    writer.writerow(header)

    for result in results:
        data = [result['author_name'], result['author_email'], result['follower_count'], result['play_count'], result['share_count'], result['comment_count'], result['has_email']]
        writer.writerow(data)

    # Updating database data
    search = Search.objects.get(id=search_id)

    search.status = 'Finished'
    search.results = ContentFile(f)
    search.save()

However, right now this just creates a results.csv file in my Django directory and doesn't upload any file to S3. How can I fix this?

CodePudding user response:

In your current approach, I think you need to do something similar to what's suggested in this post.

Maybe try something like this?

    # replace this line
    # search.results = ContentFile(f)
    search.results.save(new_name, ContentFile(f.read()))
    search.save()

CodePudding user response:

If you want to upload a file to s3 directly from the script you'd need to use boto3:

  1. Start a boto3 session
  2. Create an object for the s3 object
  3. Access the bucket in the s3 resource
  4. Upload the file

Obviously, that's greatly simplified but all of the documentation can be found here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

  • Related