在 Django 中上传文件,然后通过 REST 将其发送到其他地方?

发布于 2024-07-07 21:24:09 字数 1454 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朱染 2024-07-14 21:24:09

这是相当多的问题。

使用 Django 处理文件上传非常简单,请参阅 文件上传文档作为示例。 简而言之,您可以通过 request.FILES['file'] 访问上传的文件。

要调用 scribd api,您可以使用 urllib2; 请参阅此 Hackoarama 页面 以获取说明。 urllib2 可能有点复杂,但是一旦你掌握了它的窍门,它就可以工作了。

您可以直接从 Django 视图中调用 scribd api,但最好将其分开:从 Django 视图中将文件保存在磁盘上的某个位置,并将“上传此”消息放在消息传递系统上(例如beanstalkd)。 让一个单独的进程接收消息并将文件上传到 scribd。 这样,您就可以保护您的 http 进程和用户免受访问 API 的任何问题以及相关的延迟的影响。

That is quite a few questions.

Handling the file upload is pretty straight-forward with Django, see the File Uploads documentation for examples. In short you can access the uploaded file via request.FILES['file'].

To call the scribd api you can use urllib2; see this Hackoarama page for instructions. urllib2 can be a little convoluted but it works once you get a hang of it.

You can call the scribd api directly from within your Django view, but it'd be better practice to separate it out: from within your Django view save the file somewhere on disk and put an "upload this" message on messaging system (eg. beanstalkd). Have a separate process pick up the message and upload the file to scribd. That way you shield your http process and user from any issues accessing the API and the associated delays.

×纯※雪 2024-07-14 21:24:09

你想要做的(至少从我在这里和 Django 文档网站上读到的内容)是创建一个 自定义存储系统。

这应该能够满足您的需求 - 毕竟,这是他们启动示例的动机!

What you want to do (at least from what I read here and on the Django documentation site) is create a custom storage system.

This should give you exactly what you need - it's the motivation they use to start the example, after all!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文