Created
September 4, 2020 11:01
-
-
Save jahio/07ce05035ed0eea4d57035826bf5796b to your computer and use it in GitHub Desktop.
Using Google Cloud Storage Signed URLs in Ruby
This file contains hidden or 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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'google/cloud/storage' | |
bucket_name = 'dygnostick-test-bucket' | |
file_name = 'my_file.jpg' # This is what it will be named in the bucket | |
exp = 20 * 60 # 20 minutes | |
# Instantiating a new Google::Cloud::Storage object will automatically read your | |
# environment variables for GOOGLE_APPLICATION_CREDENTIALS, find the file, and | |
# load it up to connect with Google Cloud Storage as your service account. | |
storage = Google::Cloud::Storage.new | |
# Here we create a URL to *save* or upload a file using HTTP PUT. If you wanted | |
# to view a file in a non-public bucket, you'd use GET and leave out the | |
# headers part here. | |
url = storage.signed_url bucket_name, file_name, method: 'PUT', expires: exp, | |
version: :v4, headers: { 'Content-Type' => 'image/jpg' } | |
puts "Upload your file to:" | |
puts url | |
puts "To view your file after uploading:" | |
url = storage.signed_url bucket_name, file_name, method: 'GET', expires: exp, | |
version: :v4 | |
puts url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment