Skip to content

Instantly share code, notes, and snippets.

@jahio

jahio/sign.rb

Created Sep 4, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.