[英]Downloading file with carrierwave from Rackspace cloudfiles not working
I am using Carrierwave to upload text files to Rackspace, and have this working. 我正在使用Carrierwave将文本文件上传到Rackspace,并且可以正常工作。 My setup is really standard:
我的设置确实是标准的:
Uploader: 上载者:
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
storage :fog
def store_dir
"/my/local/path"
end
end
Model: 模型:
class UploadedFile < ActiveRecord::Base
attr_accessible :file
mount_uploader :file, FileUploader
end
Carrierwave init file: 载波初始化文件:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => "Rackspace",
:rackspace_username => "xxxxx",
:rackspace_api_key => "yyyyy"
}
config.fog_directory = "my_container"
config.fog_host = "http://c000000.cdn.rackspacecloud.com"
config.fog_public = false
end
I'm not uploading a file from a form, rather I am generating the file in a background process using delayed job, and then uploading it to Rackspace: 我不是从表单上载文件,而是使用后台作业在后台进程中生成文件,然后将其上载到Rackspace:
file = File.new("/my/local/path/some_text_file.txt", "r")
uploaded_file = UploadedFile.new
uploaded_file.file = file
uploaded_file.save
Up to this point, everything is fine. 到目前为止,一切都很好。 I can see my files in my Rackspace account.
我可以在我的Rackspace帐户中看到我的文件。 I also see that my model saved and I have the record in my uploaded_files table.
我还看到我的模型已保存,并且在我的upload_files表中有记录。
My issue is retrieving the file. 我的问题是检索文件。 I've attempted the following:
我尝试了以下操作:
downloader = FileUploader.new
downloader.retrieve_from_store!("some_text_file.txt")
I realize that retrieve_from_store! 我意识到retrieve_from_store! does not actually download the file.
实际不下载文件。 However, what should come next is:
但是,接下来应该是:
downloader.cache_stored_file!
But that line breaks because nothing was retrieved from the prior call. 但是该行中断了,因为从先前的调用中未检索到任何内容。
I think I'm doing something fundamentally wrong on my download, as the following produces odd output: 我认为下载时发生了根本上的错误,因为以下内容会产生奇怪的输出:
f = UploadedFile.first
f.file.url
"/my/local/path/some_text_file.txt"
Based on all of the examples I've seen, I would expect f.file.url to return a Rackspace CDN url. 基于我所看到的所有示例,我希望f.file.url返回Rackspace CDN URL。 But its not, it returns the store_dir path instead.
但不是,它将返回store_dir路径。
EDIT 编辑
I should note that the underlying table for the UploadedFile model has a column named "file", which contains only the base name of the file, not a url or full path to Rackspace cloud files, which may be part of the issue... 我应该注意,UploadedFile模型的基础表有一个名为“文件”的列,该列仅包含文件的基本名称,而不包含Rackspace云文件的URL或完整路径,这可能是问题的一部分...
Make sure you configured your RackSpace account to enable public access to your files. 确保已将RackSpace帐户配置为启用对文件的公共访问。 Under Cloud / FilesContainers , edit the settings to enable CDN.
在Cloud / FilesContainers下 ,编辑设置以启用CDN。 After that, the public link will be available through the CarrierWave::Uploader.url
之后,公共链接将通过CarrierWave :: Uploader.url提供
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.