Rails 二进制流支持

发布于 2024-07-05 04:56:49 字数 536 浏览 4 评论 0原文

我很快就要开始一个需要支持大型二进制文件的项目。 我想在 Web 应用程序中使用 Ruby on Rails,但我担心 BLOB 支持。 根据我使用其他语言、框架和数据库的经验,BLOB 经常被忽视,因此具有较差、困难和/或有缺陷的功能。

RoR 是否充分支持 BLOB? 一旦您已经致力于 Rails,是否会出现任何问题?

顺便说一句:我想使用 PostgreSQL 和/或 MySQL 作为后端数据库。 显然,底层数据库中的 BLOB 支持非常重要。 目前,我想避免关注数据库的 BLOB 功能; 我更感兴趣的是 Rails 本身的反应。 理想情况下,Rails 应该向我隐藏数据库的详细信息,因此我应该能够从一种数据库切换到另一种数据库。 如果情况并非如此(即:将 Rails 与特定数据库一起使用时存在一些问题),那么请务必提及。

更新:另外,我在这里不仅仅谈论 ActiveRecord。 我需要在 HTTP 端处理二进制文件(有效地上传文件)。 这意味着通过 Rails 访问适当的 HTTP 标头和流。 我已更新问题标题和描述以反映这一点。

I'm going to be starting a project soon that requires support for large-ish binary files. I'd like to use Ruby on Rails for the webapp, but I'm concerned with the BLOB support. In my experience with other languages, frameworks, and databases, BLOBs are often overlooked and thus have poor, difficult, and/or buggy functionality.

Does RoR spport BLOBs adequately? Are there any gotchas that creep up once you're already committed to Rails?

BTW: I want to be using PostgreSQL and/or MySQL as the backend database. Obviously, BLOB support in the underlying database is important. For the moment, I want to avoid focusing on the DB's BLOB capabilities; I'm more interested in how Rails itself reacts. Ideally, Rails should be hiding the details of the database from me, and so I should be able to switch from one to the other. If this is not the case (ie: there's some problem with using Rails with a particular DB) then please do mention it.

UPDATE: Also, I'm not just talking about ActiveRecord here. I'll need to handle binary files on the HTTP side (file upload effectively). That means getting access to the appropriate HTTP headers and streams via Rails. I've updated the question title and description to reflect this.

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

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

发布评论

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

评论(5

葬花如无物 2024-07-12 04:56:49

Attachment_fu +1

我在我的一个应用程序中使用attachment_fu,并且必须将文件存储在数据库中(出于恼人的原因,这些原因超出了本次会议的范围)。

我发现处理 BLOB 的(一个?)棘手的事情是你需要一个单独的代码路径来将数据发送给用户 - 你不能像你那样简单地在文件系统上内联一个路径是一个普通的简文件。

例如,如果您要存储头像信息,则不能简单地这样做:

<%= image_tag @youruser.avatar.path %>

您必须编写一些包装逻辑并使用 send_data,例如(下面只是一个带有attachment_fu的示例,实际上您需要将其干燥)

send_data(@youruser.avatar.current_data, :type => @youruser.avatar.content_type, :filename => @youruser.avatar.filename, :disposition => 'inline' )

不幸的是,据我所知attachment_fu(我没有最新版本)并没有为你做聪明的包装——你必须自己写它。

聚苯乙烯
看到你的问题编辑 - Attachment_fu 处理你提到的所有烦人的事情 - 关于需要知道文件路径和所有废话 - 除了存储在数据库中时的一个小问题。 试一试; 它是 Rails 应用程序的标准。 如果您坚持重新发明轮子,attachment_fu 的源代码也应该记录大部分陷阱!

+1 for attachment_fu

I use attachment_fu in one of my apps and MUST store files in the DB (for annoying reasons which are outside the scope of this convo).

The (one?) tricky thing dealing w/BLOB's I've found is that you need a separate code path to send the data to the user -- you can't simply in-line a path on the filesystem like you would if it was a plain-Jane file.

e.g. if you're storing avatar information, you can't simply do:

<%= image_tag @youruser.avatar.path %>

you have to write some wrapper logic and use send_data, e.g. (below is JUST an example w/attachment_fu, in practice you'd need to DRY this up)

send_data(@youruser.avatar.current_data, :type => @youruser.avatar.content_type, :filename => @youruser.avatar.filename, :disposition => 'inline' )

Unfortunately, as far as I know attachment_fu (I don't have the latest version) does not do clever wrapping for you -- you've gotta write it yourself.

P.S.
Seeing your question edit - Attachment_fu handles all that annoying stuff that you mention -- about needing to know file paths and all that crap -- EXCEPT the one little issue when storing in the DB. Give it a try; it's the standard for rails apps. IF you insist on re-inventing the wheel, the source code for attachment_fu should document most of the gotchas, too!

情徒 2024-07-12 04:56:49

至于流媒体,您可以以(至少是内存)有效的方式完成这一切。 在上传方面,表单中的文件参数被抽象为可以读取的IO对象; 在下载方面,查看采用 Proc 参数的 render :text => 形式:

render :content_type => 'application/octet-stream', :text => Proc.new {
    |response, output|
    # do something that reads data and writes it to output
}

不过,如果您的内容位于磁盘上的文件中,则上述解决方案肯定会更好。

As for streaming, you can do it all in an (at least memory-) efficient way. On the upload side, file parameters in forms are abstracted as IO objects that you can read from; on the download side, look in to the form of render :text => that takes a Proc argument:

render :content_type => 'application/octet-stream', :text => Proc.new {
    |response, output|
    # do something that reads data and writes it to output
}

If your stuff is in files on disk, though, the aforementioned solutions will certainly work better.

心病无药医 2024-07-12 04:56:49

您可以在 ActiveRecord 迁移中使用 :binary 类型,并限制最大大小:

class BlobTest < ActiveRecord::Migration
  def self.up
    create_table :files do |t|
      t.column :file_data, :binary, :limit => 1.megabyte
    end
  end
end

ActiveRecord 将 BLOB(或 CLOB)内容公开为 Ruby 字符串。

You can use the :binary type in your ActiveRecord migration and also constrain the maximum size:

class BlobTest < ActiveRecord::Migration
  def self.up
    create_table :files do |t|
      t.column :file_data, :binary, :limit => 1.megabyte
    end
  end
end

ActiveRecord exposes the BLOB (or CLOB) contents as a Ruby String.

灵芸 2024-07-12 04:56:49

也查看插件x_send_file

“XSendFile 插件提供了一个简单的接口,用于通过 X-Sendfile HTTP 标头发送文件。这使您的 Web 服务器能够直接从磁盘提供文件,而不是通过 Rails 进程流式传输。这样速度更快,并且节省大量内存如果您使用的是 Mongrel,则并非每个 Web 服务器都支持此标头。”

我不确定它是否可用于 Blob,它可能仅适用于文件系统上的文件。 但您可能需要一些不会占用网络服务器传输大量数据的东西。

Look into the plugin, x_send_file too.

"The XSendFile plugin provides a simple interface for sending files via the X-Sendfile HTTP header. This enables your web server to serve the file directly from disk, instead of streaming it through your Rails process. This is faster and saves a lot of memory if you‘re using Mongrel. Not every web server supports this header. YMMV."

I'm not sure if it's usable with Blobs, it may just be for files on the file system. But you probably need something that doesn't tie up the web server streaming large chunks of data.

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