Ruby on rails 如何使用rake任务在Google存储桶中保存图像
我有一个使用活动存储的Rails应用程序,其中有些任务应该每天运行,以便将图像附加到模型上,并将其存储在Google云中的一个存储桶中 当我运行任务“Ruby on rails 如何使用rake任务在Google存储桶中保存图像,ruby-on-rails,ruby,google-cloud-platform,rails-activestorage,rake-task,Ruby On Rails,Ruby,Google Cloud Platform,Rails Activestorage,Rake Task,我有一个使用活动存储的Rails应用程序,其中有些任务应该每天运行,以便将图像附加到模型上,并将其存储在Google云中的一个存储桶中 当我运行任务“rake attach_image:attach_image_to_cloud”以附加图像时,它显示: Google::Cloud::PermissionDeniedError: forbidden: attach-images-app@attach-images.iam.gserviceaccount.com does not have stor
rake attach_image:attach_image_to_cloud
”以附加图像时,它显示:
Google::Cloud::PermissionDeniedError: forbidden: attach-images-app@attach-images.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket.
Caused by:
Google::Apis::ClientError: forbidden: attach-images-app@attach-images.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket.
我遵循“”并认为我做的一切都是对的
我的Ruby版本是2.5.3,Rails是5.2.2
我的任务是:
def download_data(download_url, datetime, json_data)
puts "Downloading fields"
@field = Field.new(datetime: datetime, json_data: json)
puts "Saving field, datetime: #{datetime}"
attach_image(download_url, datetime, @field)
@field.save
puts "Finished downloading #{datetime} field"
end
def attach_image(download_url, datetime, field)
link = download_url
field.image.attach(io: open(link), filename: "global_#{datetime}.png")
end
download_data(download_url, some_datetime, json_data)
这是my model field.rb:
class Field < ApplicationRecord
has_one_attached :image
end
我运行了捆绑安装
:
gem "google-cloud-storage", "~> 1.8", require: false
我的bucket和服务帐户密钥都已正确创建,并且凭据具有所有者规则。凭证是在Google Cloud控制台中创建并在我的开发和Heroku生产环境中正确设置时下载的哈希。我找到了解决此问题的方法。通过使用gsutil cors命令在bucket上配置cors,问题得到了解决
gsutil cors set [JSON_FILE_NAME].json gs://[BUCKET_NAME]
json文件的内容是:
[
{
"origin": ["*"],
"responseHeader": ["Content-Type", "Content-Md5", "Origin", "X-Requested-With", "Accept", "Authorization"],
"method": ["PUT", "GET", "HEAD", "DELETE", "OPTIONS"],
"maxAgeSeconds": 3600
}
]
我已经找到了解决这个问题的办法。通过使用gsutil cors命令在bucket上配置cors,问题得到了解决
gsutil cors set [JSON_FILE_NAME].json gs://[BUCKET_NAME]
json文件的内容是:
[
{
"origin": ["*"],
"responseHeader": ["Content-Type", "Content-Md5", "Origin", "X-Requested-With", "Accept", "Authorization"],
"method": ["PUT", "GET", "HEAD", "DELETE", "OPTIONS"],
"maxAgeSeconds": 3600
}
]
您通过
GOOGLE\u APPLICATION\u credentials
提供的凭据在开发过程中是否正常工作?您确定在Heroku中,您在env变量中设置了正确的值吗?您通过GOOGLE\u APPLICATION\u credentials
提供的凭据在开发中是否正常工作?您确定在Heroku中,您在env变量中设置了正确的值吗?