Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
How do I write data binary to gcs with ruby efficiently?

I want to upload data binary directly to GCP storage, without writing the file to disk. Below is the code snippet I have created to get to the state that I am going to be at.

require 'google/cloud/storage'

bucket_name = '-----'

data = File.open('image_block.jpg', 'rb') {|file| file.read }


storage = Google::Cloud::Storage.new("project_id": "maybe-i-will-tell-u")
bucket  = storage.bucket bucket_name, skip_lookup: true

Now I want to directly put this data into a file on gcs, without having to write a file to disk.

Is there an efficient way we can do that?

I tried the following code

to_send = StringIO.new(data).read

bucket.create_file to_send, "image_inder_11111.jpg"

but this throws an error saying

/google/cloud/storage/bucket.rb:2898:in `file?': path name contains null byte (ArgumentError)
    from /home/inder/.gem/gems/google-cloud-storage-1.36.1/lib/google/cloud/storage/bucket.rb:2898:in `ensure_io_or_file_exists!'
    from /home/inder/.gem/gems/google-cloud-storage-1.36.1/lib/google/cloud/storage/bucket.rb:1566:in `create_file'
    from champa.rb:14:in `<main>'
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As suggested by @stefan, It should be to_send = StringIO.new(data), i.e. without .read (which would return a string again)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!