如何使用 rake 任务将图像保存在 Google 存储桶中
How to save images in Google storage bucket with a rake task
我有一个使用 Active Storage 的 Rails 应用程序,其中一些任务应该每天 运行 将图像附加到模型并将它们存储在 Google 云中的存储桶中。
当我运行任务“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.
我关注了“Setting up Rails 5.2 Active Storage, using Google Cloud Storage and Heroku”并认为我做的一切都是对的。
我的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)
这是我的模型field.rb:
class Field < ApplicationRecord
has_one_attached :image
end
这是我的 config.yml:
google:
service: GCS
project: attach-images
credentials: <%= ENV['GOOGLE_APPLICATION_CREDENTIALS'].as_json %>
bucket: fields
google_dev:
service: GCS
project: attach-images
credentials: <%= Rails.root.join("config/secrets/attach_images.json") %>
bucket: fields
这些在我的 development.rb 和 production.rb 环境文件中:
config.active_storage.service = :google_dev
config.active_storage.service = :google
我运行bundle install
与:
gem "google-cloud-storage", "~> 1.8", require: false
我的存储桶和我的服务帐户密钥都已正确创建并且凭据具有所有者规则。凭据是在 Google Cloud 的控制台中创建并在我的开发和 Heroku 生产环境中正确设置时下载的哈希。
我找到了解决此问题的方法。使用 gsutil cors 命令在存储桶上配置 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
}
]
我有一个使用 Active Storage 的 Rails 应用程序,其中一些任务应该每天 运行 将图像附加到模型并将它们存储在 Google 云中的存储桶中。
当我运行任务“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.
我关注了“Setting up Rails 5.2 Active Storage, using Google Cloud Storage and Heroku”并认为我做的一切都是对的。
我的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)
这是我的模型field.rb:
class Field < ApplicationRecord
has_one_attached :image
end
这是我的 config.yml:
google:
service: GCS
project: attach-images
credentials: <%= ENV['GOOGLE_APPLICATION_CREDENTIALS'].as_json %>
bucket: fields
google_dev:
service: GCS
project: attach-images
credentials: <%= Rails.root.join("config/secrets/attach_images.json") %>
bucket: fields
这些在我的 development.rb 和 production.rb 环境文件中:
config.active_storage.service = :google_dev
config.active_storage.service = :google
我运行bundle install
与:
gem "google-cloud-storage", "~> 1.8", require: false
我的存储桶和我的服务帐户密钥都已正确创建并且凭据具有所有者规则。凭据是在 Google Cloud 的控制台中创建并在我的开发和 Heroku 生产环境中正确设置时下载的哈希。
我找到了解决此问题的方法。使用 gsutil cors 命令在存储桶上配置 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
}
]