ruby 使用 open-URI 从 S3 读取文件

发布于 2024-10-06 22:51:35 字数 716 浏览 3 评论 0原文

我从 S3 读取文件时遇到一些问题。我希望能够远程加载 ID3 标签,但是使用 open-URI 不起作用,它会给我以下错误:

ruby-1.8.7-p302 > c=TagLib2::File.new(open(URI.parse("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514")))
TypeError: can't convert Tempfile into String
    from (irb):8:in `initialize'
    from (irb):8:in `new'
    from (irb):8

但是,如果我下载相同的文件并将其放在桌面上(即不需要 open-URI) URI),它工作得很好。

c=TagLib2::File.new("/Users/momofwombie/Desktop/blah.mp3")

我还应该做些什么来读取远程文件吗?

更新:我刚刚找到这个链接,它可能会解释一点,但肯定有某种方法可以做到这一点...

从远程服务器上的文件读取标头数据

I'm having some problems reading a file from S3. I want to be able to load the ID3 tags remotely, but using open-URI doesn't work, it gives me the following error:

ruby-1.8.7-p302 > c=TagLib2::File.new(open(URI.parse("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514")))
TypeError: can't convert Tempfile into String
    from (irb):8:in `initialize'
    from (irb):8:in `new'
    from (irb):8

However, if i download the same file and put it on my desktop (ie no need for open-URI), it works just fine.

c=TagLib2::File.new("/Users/momofwombie/Desktop/blah.mp3")

is there something else I should be doing to read a remote file?

UPDATE: I just found this link, which may explain a little bit, but surely there must be some way to do this...

Read header data from files on remote server

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

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

发布评论

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

评论(4

我做我的改变 2024-10-13 22:51:35

可能想要查看 AWS::S3,一个用于 Amazon 简单存储服务的 Ruby 库

执行 AWS ::S3:S3Object.find 查找文件,然后使用 about 检索元数据

此解决方案假设您拥有 AWS 凭证和权限来访问包含以下文件的 S3 存储桶问题。

Might want to check out AWS::S3, a Ruby Library for Amazon's Simple Storage Service

Do an AWS::S3:S3Object.find for the file and then an use about to retrieve the metadata

This solution assumes you have the AWS credentials and permission to access the S3 bucket that contains the files in question.

╰つ倒转 2024-10-13 22:51:35

TagLib2::File.new 不接受文件句柄,这是您在使用 open 而不使用 read 时传递给它的内容。

添加 read 后,您将获得 URL 的内容,但 TagLib2::File 也不知道如何处理它,因此您被迫读取URL 的内容,然后保存。

我还注意到您使 OpenURI 的使用变得不必要地复杂化。在将 URL 传递给 open 之前,您不必使用 URI 解析该 URL。只需传递 URL 字符串即可。

require 'open-uri'

fname = File.basename($0) << '.' << $.to_s
File.open(fname, 'wb') do |fo|
  fo.print open("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514").read
end

c = TagLib2::File.new(fname)

# do more processing...

File.delete(fname)

我没有安装 TagLib2,但我运行了其余的代码,并将 mp3 文件下载到我的磁盘上并且可以播放。 File.delete 随后会进行清理,这应该会让您处于您想要的状态。

TagLib2::File.new doesn't take a file handle, which is what you are passing to it when you use open without a read.

Add on read and you'll get the contents of the URL, but TagLib2::File doesn't know what to do with that either, so you are forced to read the contents of the URL, and save it.

I also noticed you are unnecessarily complicating your use of OpenURI. You don't have to parse the URL using URI before passing it to open. Just pass the URL string.

require 'open-uri'

fname = File.basename($0) << '.' << $.to_s
File.open(fname, 'wb') do |fo|
  fo.print open("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514").read
end

c = TagLib2::File.new(fname)

# do more processing...

File.delete(fname)

I don't have TagLib2 installed but I ran the rest of the code and the mp3 file downloaded to my disk and is playable. The File.delete would clean up afterwards, which should put you in the state you want to be in.

著墨染雨君画夕 2024-10-13 22:51:35

这个解决方案不会持续太久。回形针> 3.0.0 已删除 to_file。我正在使用 S3 和赫罗库。我最终所做的是将文件复制到临时位置并从那里解析它。这是我的代码:

dest = Tempfile.new(upload.spreadsheet_file_name)
dest.binmode
upload.spreadsheet.copy_to_local_file(:default_style, dest.path)
file_loc = dest.path 

...

CSV.foreach(file_loc, :headers => true, :skip_blanks => true) do |row|}

This solution isn't going to work much longer. Paperclip > 3.0.0 has removed to_file. I'm using S3 & Heroku. What I ended up doing was copying the file to a temporary location and parsing it from there. Here is my code:

dest = Tempfile.new(upload.spreadsheet_file_name)
dest.binmode
upload.spreadsheet.copy_to_local_file(:default_style, dest.path)
file_loc = dest.path 

...

CSV.foreach(file_loc, :headers => true, :skip_blanks => true) do |row|}
凤舞天涯 2024-10-13 22:51:35

这似乎可以代替 open-URI:

Mp3Info.open(mp3.to_file.path) do |mp3info|
  puts mp3info.tag.artist
end

Paperclip 有一个 to_file 方法,可以从 S3 下载文件。

This seems to work instead of open-URI:

Mp3Info.open(mp3.to_file.path) do |mp3info|
  puts mp3info.tag.artist
end

Paperclip has a to_file method that downloads the file from S3.

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