gpt4 book ai didi

ruby-on-rails - Carrierwave 和 Amazon S3 文件下载/上传

转载 作者:行者123 更新时间:2023-12-04 02:19:39 27 4
gpt4 key购买 nike

我有一个带有 UploadsUploader 的 rails 3 应用程序和一个安装它的资源模型。我最近改用 s3 存储,这破坏了我使用 send_to 方法下载文件的能力。我可以使用 redirect_to 方法启用下载,该方法只是将用户转发到经过身份验证的 s3 url。我需要验证文件下载,我希望 URL 为 http://mydomainname.com/the_file_pathhttp://mydomainname.com/controller_action_name/id_of_resource所以我假设我需要使用send_to,但是有没有办法使用redirect_to 方法来做到这一点?我当前的代码如下。资源 Controller .rb

def download
resource = Resource.find(params[:id])
if resource.shared_items.find_by_shared_with_id(current_user) or resource.user_id == current_user.id
filename = resource.upload_identifier
send_file "#{Rails.root}/my_bucket_name_here/uploads/#{filename}"
else
flash[:notice] = "You don't have permission to access this file."
redirect_to resources_path
end
end

carrierwave.rb 初始化程序:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxxx', # copied off the aws site
:aws_secret_access_key => 'xxxx', #
}

config.fog_directory = 'my_bucket_name_here' # required
config.fog_host = 'https://localhost:3000' # optional, defaults to nil
config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end

upload_uploader.rb
class UploadUploader < CarrierWave::Uploader::Base
storage :fog

def store_dir
"uploads"
end
end

所有这些都会引发错误:

Cannot read file /home/tom/Documents/ruby/rails/circlshare/My_bucket_name_here/uploads/Picture0024.jpg



我曾尝试阅读有关carrierwave、fog、send_to 和所有这些的信息,但我所尝试的一切都还没有取得成果。上传工作正常,我可以看到 s3 存储桶中的文件。使用 re_direct 会很棒,因为文件不会通过我的服务器。任何帮助表示赞赏。谢谢。

最佳答案

看起来您想上传到 S3,但没有公开的 URL。您可以将用户重定向到经过 S3 验证的 URL,而不是从 S3 下载文件并使用 send_file。此 URL 将过期且仅在一段时间内有效(供用户下载)。

查看此线程:http://groups.google.com/group/carrierwave/browse_thread/thread/2f727c77864ac923

由于您已经将fog_public 设置为false,您在调用resource.upload_url 时是否会获得经过身份验证(即签名)的url

关于ruby-on-rails - Carrierwave 和 Amazon S3 文件下载/上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com