Roko
Roko

Reputation: 1335

Transferring google bucket file to end user without saving file locally

Right now when client download file from my site, I'm:

  1. Downloading file from google cloud bucket to server (GCP download file, GCP streaming download)
  2. Saving downloaded file to a Ruby Tempfile
  3. sending Tempfile to enduser using Rails 5 send_file

I would like to skip step 2, to somehow transfer/stream file from google cloud to enduser without the file being saved at my server- is that possible? Note the google bucket is private.

Code I'm currently using:

    # 1 getting file from gcp:
    storage = Google::Cloud::Storage.new
    bucket  = storage.bucket bucket_name, skip_lookup: true
    gcp_file    = bucket.file file_name

    # 2a creates tempfile
    temp_file = Tempfile.new('name')
    temp_file_path = temp_file.path
    # 2b populate tempfile with gcp file content:
    gcp_file.download temp_file_path

    # 3 sending tempfile to user
    send_file(temp_file, type: file_mime_type, filename: 'filename.png')

What I would like:


    # 1 getting file from gcp:
    storage = Google::Cloud::Storage.new
    bucket  = storage.bucket bucket_name, skip_lookup: true
    gcp_file    = bucket.file file_name

    # 3 sending/streaming file from google cloud to client:
    send_file(gcp_file.download, type: file_mime_type, filename: 'filename.png')

Upvotes: 1

Views: 457

Answers (1)

Robert G
Robert G

Reputation: 2055

Since making your objects or your bucket publicly readable or accessible is not an option for your project, the best option that I could suggest is using signed URLs so that you can still have control over your objects or bucket and also giving users sufficient permission to perform specific actions like download objects in your GCS bucket.

Upvotes: 1

Related Questions