我正在为Rails 4.0应用程序进行rake任务,以便将一些旧的图像URL从我的网站外部或S3复制到S3。我可以验证我的凭据是否正确并加载了printenv
。但出于某种原因,每次运行任务时都会收到403错误。我的系统时间与可靠的时间服务器同步,我尝试在S3中保存的头像目录设置为公共。关于可能导致这种情况的任何想法?完整的佣金任务代码如下:
namespace :arthackday do
desc "Grabs old profile pics from participants and saves them to S3."
task :update_participant_photos => :environment do
require 'open-uri'
require 'aws/s3'
s3_base_url = "https://s3-us-west-1.amazonaws.com/arthackday/participants/avatars/"
Participant.all.each do |participant|
if participant.photo_url?
file_name = "#{participant.name.gsub(/ /,"-")}.jpg"
File.open("#{Rails.root}/public/img/temp/#{file_name}", 'wb') do |fo|
fo.write open(participant.photo_url).read
end
s3 = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
s3.buckets[ENV['S3_BUCKET_NAME']].objects["participants/avatars/#{file_name}"].write(:file => "#{Rails.root}/public/img/temp/#{file_name}")
participant.photo_url = s3_base_url + file_name
participant.save!
puts "New URL for #{participant.name}'s photo: #{participant.photo_url}"
else
puts "#{participant.name} does not have a photo_url"
end
end
end
end