Skip to content

Instantly share code, notes, and snippets.

@jahio
Created September 4, 2020 11:01
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jahio/07ce05035ed0eea4d57035826bf5796b to your computer and use it in GitHub Desktop.
Using Google Cloud Storage Signed URLs in Ruby
#!/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