ruby 使用 open-URI 从 S3 读取文件
我从 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能想要查看 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 useabout
to retrieve the metadataThis solution assumes you have the AWS credentials and permission to access the S3 bucket that contains the files in question.
TagLib2::File.new
不接受文件句柄,这是您在使用open
而不使用read
时传递给它的内容。添加
read
后,您将获得 URL 的内容,但TagLib2::File
也不知道如何处理它,因此您被迫读取URL 的内容,然后保存。我还注意到您使 OpenURI 的使用变得不必要地复杂化。在将 URL 传递给
open
之前,您不必使用URI
解析该 URL。只需传递 URL 字符串即可。我没有安装 TagLib2,但我运行了其余的代码,并将 mp3 文件下载到我的磁盘上并且可以播放。
File.delete
随后会进行清理,这应该会让您处于您想要的状态。TagLib2::File.new
doesn't take a file handle, which is what you are passing to it when you useopen
without aread
.Add on
read
and you'll get the contents of the URL, butTagLib2::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 toopen
. Just pass the URL string.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.这个解决方案不会持续太久。回形针> 3.0.0 已删除 to_file。我正在使用 S3 和赫罗库。我最终所做的是将文件复制到临时位置并从那里解析它。这是我的代码:
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:
这似乎可以代替 open-URI:
Paperclip 有一个 to_file 方法,可以从 S3 下载文件。
This seems to work instead of open-URI:
Paperclip has a to_file method that downloads the file from S3.