Skip to content

Instantly share code, notes, and snippets.

@pduey
Last active July 31, 2020 19:01
Show Gist options
  • Save pduey/8061d0d50aec98963e77c211373bc475 to your computer and use it in GitHub Desktop.
Save pduey/8061d0d50aec98963e77c211373bc475 to your computer and use it in GitHub Desktop.
Google Cloud Service CORS Configuration in the Rails Console
# In order to use Rails Active Storage Direct Upload to a cloud service, e.g., using Rails
# Action Text rich text editor image upload button, the cloud service will need CORS
# configured. The GCS CORS doc at https://cloud.google.com/storage/docs/configuring-cors
# describes 3 ways of doing it. I wanted to use the Ruby code method, since I already had
# GCS configured for my app and could avoid configuring some other utility. The key was
# getting a handle on a client from my rails app. So, assuming you have already uploaded
# something via your app (not via direct upload):
client = ActiveStorage::Blob.last.service.send :client
bucket = client.buckets.first
bucket.update do |bucket|
bucket.cors do |cors|
cors.add_rule ["http://localhost:3000"], "*", headers: ["Origin", "Content-Type", "Content-MD5", "Content-Disposition"], max_age: 3600
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment