我正在使用sitemap_generator来创建站点地图。 我有一个rake任务来创建s站点地图并将其上传到s3。
sitemap.rb
SitemapGenerator::Sitemap.default_host = "https://www.ezpoisk.com"
SitemapGenerator::Sitemap.create_index = true
SitemapGenerator::Sitemap.public_path = 'tmp/'
SitemapGenerator::Sitemap.sitemaps_path = 'sitemaps/'
SitemapGenerator::Sitemap.create do
# generating links ...
rake任务
require "aws"
namespace :sitemap do
desc "Upload the sitemap files to S3"
task upload_to_s3: :environment do
puts "Starting sitemap upload to S3..."
s3 = AWS::S3.new(access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"])
bucket = s3.buckets[ENV["S3_BUCKET_NAME"]]
Dir.entries(File.join(Rails.root, "tmp", "sitemaps")).each do |file_name|
next if ['.', '..', '.DS_Store'].include? file_name
path = "sitemaps/#{file_name}"
file = File.join(Rails.root, "tmp", "sitemaps", file_name)
begin
object = bucket.objects[path]
object.write(file: file)
rescue Exception => e
raise e
end
puts "Saved #{file_name} to S3"
end
end
desc 'Create the sitemap, then upload it to S3 and ping the search engines'
task create_upload_and_ping: :environment do
Rake::Task["sitemap:create"].invoke
Rake::Task["sitemap:upload_to_s3"].invoke
url = "https://www.ezpoisk.com/sitemaps/sitemap.xml.gz"
SitemapGenerator::Sitemap.ping_search_engines(url)
end
end
我希望能够通过我的网站从s3到路径
get "sitemaps/sitemap(:id).:format.:compression" => "sitemap#show"
和sitemaps_controller
def show
data = open("https://s3.amazonaws.com/#{ENV['S3_BUCKET_NAME']}/sitemaps/sitemap#{params[:id]}.xml.gz")
send_data data.read, :type => data.content_type
end
现在。问题。
当我运行rake任务并尝试通过链接访问文件时,我得到403禁止。然后我转到s3控制台并在“sitemaps”文件夹上手动执行“Make it public”。现在,当我尝试访问文件时,它已正确下载... 问题是 - 当我再次运行任务时(我有一个每天做一次的sidekiq工作)我再次得到403 ...我的假设是我的写操作改变了对此的权限。
存储桶本身具有“允许每个人列出”权限。
我试过
bucket = s3.buckets[ENV["S3_BUCKET_NAME"]]
bucket.acl = :public_read
在rake任务中,但它似乎没有生效。 我遗漏了一些东西,必须要么在写入时设置一个标志以使其公开,或者可能,我没有正确初始化它。
答案 0 :(得分:1)
确定。它非常简单/明显(通常) 在rake任务中它应该是
object.write(file:file,acl :: public_read)
https://www.codefellows.org/blog/tutorial-how-to-upload-files-using-the-aws-sdk-gem