改变Django的存储器后端从S3到cloudfiles和处理旧文件 [英] changing django-storages backend from from s3 to cloudfiles and dealing with old files

查看:150
本文介绍了改变Django的存储器后端从S3到cloudfiles和处理旧文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用程序,我移动到Rackspace公司。我有一个使用FileFields和我使用的是Django的存储器库S3 /博托后端的典范。我想用cloudfiles进行存储,而我需要能够来提供老S3的内容。

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 从视图和equals settings.STATIC_URL 设置。显然,这是不会,因为 settings.STATIC_URL 工作是怎么回事,当我从S3切换来改变。

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.

我是否需要写一个脚本来所有的手我的S3文件迁移到cloudfiles也经历并更新我的表中的所有FileFields? (啊)。我宁愿能变化出存储器后端,离开的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?

奖金问题:什么最佳做法我没有跟着这样做时?我只使用Django,现在半年左右了。

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

推荐答案

选项A:您可以使用旧的和新的储量有一点不同的路径,并有东西从那里文件应送达决定。东西可能是定制的存储类,我认为:您从CloudFilesStorage继承存储类,并且在overrided URL方法它要么调用super(如果文件是在新的存储)或S3BotoStorage调用URL方法(如果文件是在老存储)。

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).

选项B:您可以将您的文件cloudfiles。我不认为你真的需要,如果你继续类似的路径后改变DB什么。也许你甚至不需要一个脚本,只需下载文件,然后再上传(s3cmd可用于下载,cloudfiles可能得到了一些工具来上传过)。

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).

的最佳做法:你应该已经使用MEDIA_URL和DEFAULT_FILE_STORAG​​E而不是static_url和静态存储。这是一个好主意,让项目静态文件(CSS / JS /图标),并上传的媒体分开。

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.

这篇关于改变Django的存储器后端从S3到cloudfiles和处理旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆