changing django-storages backend from from s3 to cloudfiles and dealing with old files

£可爱£侵袭症+ 提交于 2019-12-12 15:07:11

问题


I've got a django app that I'm moving to rackspace. I have a model that uses FileFields and I'm using the django-storages library s3/boto backend. I want to use cloudfiles for storage, and I need to be able to serve up the old s3 content.

On a template page where I provide links to the files, I do this:

href="{{ static_url }}{{ article.code_archive_file}}"

static_url is set from the view and equals settings.STATIC_URL. Clearly this isn't going to work since settings.STATIC_URL is going to change when I switch from s3.

Do I need to write a script to migrate all of my s3 files by hand to cloudfiles and also go through and update all the FileFields in my tables? (ugh). I'd rather be able to change out the storages backend and leave the old material in the s3 bucket.

If I do need to migrate the files and the fields, has someone already written a script for that?

bonus question: What best-practices didn't I follow when doing this? I've only been using django for about half a year now.


回答1:


Option A: You can use a bit different paths on old and new storages and have something to decide from where file should be served. Something could be custom storage class, I think: you create storage class inherited from CloudFilesStorage, and in overrided url method it will either call super (if file is in the new storage) or call url method from S3BotoStorage (if file is in the old storage).

Option B: You can move your files to cloudfiles. I don't think you actually need to change anything in DB after that if you keep paths similar. Maybe you even don't need a script, just download files and then upload then (s3cmd can be used for downloading, cloudfiles probably got some tool for uploading too).

About best practices: you should've used media_url and DEFAULT_FILE_STORAGE instead of static_url and static storage. It's a good idea to keep project's static files (css/js/icons) and uploaded media separated.



来源:https://stackoverflow.com/questions/20912041/changing-django-storages-backend-from-from-s3-to-cloudfiles-and-dealing-with-old

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!