Last active
July 31, 2020 19:01
-
-
Save pduey/8061d0d50aec98963e77c211373bc475 to your computer and use it in GitHub Desktop.
Google Cloud Service CORS Configuration in the Rails Console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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