4

I'm trying to migrate some models ImageFields to using the S3BotoStorage storage backend from django-storages. As part of this process I've changed my Model's ImageField declaration to include the storage=instance_of_s3botostorage argument, and new instances of my Model that save an image to the ImageField attribute now get stored in S3 - as intended.

I tried to move existing model instances over to storing their data in S3, too, so wrote a South DataMigration like this:

def forwards(self, orm):
    "upload ImageField file to S3 if it's not already in there"
    for mymodel in orm.MyModel.objects.all():
        if mymodel.logo_image and not isinstance(mymodel.logo_image.storage, S3BotoStorage):
            print "uploading %s to S3" % mymodel.logo_image
            file_contents = ContentFile(mymodel.logo_image.read())
            mymodel.logo_image.save(mymodel.logo_image.name, file_contents)
            mymodel.save()

but this clearly doesn't have the intended effect because the image file is simply saved using the old storage backend - which makes sense considering save() is actually a method of the FieldFile belonging to the FileField

So, how to move/change file storage on an instance of a model?

Armando Pérez Marqués
  • 5,661
  • 4
  • 28
  • 45
markhellewell
  • 24,390
  • 1
  • 21
  • 21

2 Answers2

4

So, turns out the particular storage used for files is not stored in the database. 'migration' is simply a matter of changing the Model definition then, outside of using the storage subsystem API, simply upload files into the new storage locations.

markhellewell
  • 24,390
  • 1
  • 21
  • 21
  • 1
    Go ahead and accept this answer. It'll help those who come after you. – Manoj Govindan Aug 23 '10 at 06:24
  • Could you please expand your answer a bit? I am facing exactly same issue. – chhantyal Sep 27 '13 at 21:29
  • @neokya so, change the model definition to reference the new storage backend you wish to use, then (using some other tool) copy data from your original file storage to the new file storage (preserving paths, of course). – markhellewell Oct 04 '13 at 00:10
  • Thanks. I did exactly what you are saying. I wrote a small script to copy files here and there, which looks fine :) – chhantyal Oct 04 '13 at 07:16
0

I would look at a system more like this for your problem. http://github.com/seanbrant/django-queued-storage

kkubasik
  • 3,684
  • 6
  • 25
  • 23