Ruby API Client

Ruby integration handles uploads and file operations by wrapping Uploadcare Upload API and REST API. This comprehensive API client lets you use most of the Uploadcare features from within your Ruby app.

Gem → GitHub →

Features

Uploading (Upload API):

  • Upload files from local storage and URLs (up to 5 TB)
  • Multipart uploading for large files
  • Uploading network to speed uploading jobs (like CDN)

File management (REST API):

  • Get file info and perform various operations (store/delete) with them
  • Work with groups of files
  • Get info about account project
  • Manage webhooks
  • Convert documents
  • Encode and transform videos

Image processing (URL API):

  • Compression
  • Geometry
  • Colors
  • Definition
  • Image and text overlays
  • Rotations
  • Recognition
  • File info
  • Proxy (fetch)

Security features:

  • Secure authentication
  • Secure uploads (signed uploads)
  • Secure delivery (signed URLs)
  • Secure webhooks (signing secret)

Requirements

  • Ruby 3.3+

Upgrading from v4.x? See the migration guide.

Install

Add this line to your application’s Gemfile:

1gem 'uploadcare-ruby'

And then execute:

bundle

If already not, create your project in Uploadcare dashboard and copy your API keys from there.

Set your Uploadcare keys in config file or through environment variables:

$export UPLOADCARE_PUBLIC_KEY=$YOUR_PUBLIC_KEY
$export UPLOADCARE_SECRET_KEY=$YOUR_SECRET_KEY

Or configure your app yourself if you are using different way of storing keys. Gem configuration is available in Uploadcare.configuration. Full list of settings can be seen in lib/uploadcare.rb

1# Preferred: explicit client
2client = Uploadcare::Client.new(
3 public_key: ENV.fetch('UPLOADCARE_PUBLIC_KEY'),
4 secret_key: ENV.fetch('UPLOADCARE_SECRET_KEY')
5)
6
7# Alternative: global configuration (still supported)
8Uploadcare.configure do |config|
9 config.public_key = ENV.fetch('UPLOADCARE_PUBLIC_KEY')
10 config.secret_key = ENV.fetch('UPLOADCARE_SECRET_KEY')
11end

Examples

Quick start

1require 'uploadcare'
2
3client = Uploadcare::Client.new(
4 public_key: ENV.fetch('UPLOADCARE_PUBLIC_KEY'),
5 secret_key: ENV.fetch('UPLOADCARE_SECRET_KEY')
6)
7
8file = File.open('image.png', 'rb') do |io|
9 client.files.upload(io, store: 'auto')
10end

Upload API

Upload from URL

1file = client.files.upload_from_url("https://example.com/image.jpg", store: true)
2puts file.cdn_url

View full script: post_from_url.rb

Multipart upload (large files)

1File.open('big_file.bin', 'rb') do |io|
2 file = client.uploads.multipart_upload(file: io, store: true)
3 puts file.uuid
4end

View related scripts: post_multipart_start.rb, post_multipart_complete.rb

REST API

Get file info

1file = client.files.find(uuid: 'YOUR_FILE_UUID')
2puts file.mime_type

View full script: get_files_uuid.rb

Add a webhook

1webhook = client.webhooks.create(
2 target_url: "https://example.com/callback",
3 event: "file.uploaded",
4 is_active: true
5)
6puts webhook.id

View full script: post_webhooks.rb

Note: All examples assume you configured your API keys. Replace placeholders with your actual keys, or set them via environment variables.

Uploading and storing a single file

Using Uploadcare is simple, and here are the basics of handling files.

1@uc_file = File.open('your-file.png', 'rb') do |io|
2 client.files.upload(io, store: true)
3end
4
5puts @uc_file.uuid
6puts @uc_file.cdn_url

You might then want to store or delete the uploaded file. Storing files could be crucial if you aren’t using the “Automatic file storing” option for your Uploadcare project. If not stored manually or automatically, files get deleted within a 24-hour period.

More upload methods

Uploadcare supports multiple ways to upload files:

1# Smart upload: accepts an IO, array of IOs, or a URL string
2client.uploads.upload('https://example.com/image.jpg')
3
4# Multiple files
5client.uploads.upload([file1, file2], store: true)
6
7# URL upload
8client.files.upload_from_url('https://example.com/image.jpg', store: true)
9
10# Multipart upload for large files
11File.open('big_file.bin', 'rb') do |io|
12 client.uploads.multipart_upload(file: io, store: true)
13end

Upload options

You can override global :autostore option for each upload request:

1client.uploads.upload(files, store: true)
2client.files.upload_from_url(url, store: 'auto')

Client

Most methods are available through a Uploadcare::Client instance:

1client.uploads.upload('https://example.com/image.jpg')

Entity object

Entities are representations of objects in Uploadcare cloud.

File

File entity contains its metadata.

1@file = client.files.find(uuid: 'FILE_UUID')
2# => #<Uploadcare::Resources::File ...>
3
4# Metadata is read through accessor methods, not hash keys:
5@file.uuid # => "8f64f313-e6b1-4731-96c0-6751f1e7a50a"
6@file.mime_type # => "image/jpeg"
7@file.size # => 5345
8@file.is_image # => true
9@file.is_ready # => true
10@file.original_filename # => "image.jpeg"
11@file.datetime_uploaded # => "2020-01-16T15:03:14.676902Z"
12@file.datetime_stored # => "2020-01-16T15:03:15.315064Z"
13@file.datetime_removed # => nil
14@file.url # => "https://api.uploadcare.com/files/FILE_UUID/"
15@file.cdn_url # => "https://PROJECT_SUBDOMAIN.ucarecd.net/FILE_UUID/image.jpeg"
16@file.content_info # => { "mime" => { "mime" => "image/jpeg", "type" => "image", "subtype" => "jpeg" },
17 # "image" => { "width" => 190, "height" => 183, "format" => "JPEG",
18 # "color_mode" => "RGB", "dpi" => nil, "orientation" => nil } }
19@file.metadata # => {} custom key/value metadata you attach to the file
20@file.appdata # => {} add-on results (virus scan, moderation, etc.)

Note: content_info replaces the old image_info field and covers image dimensions, video duration/bitrate, and audio info.

Note: copy is no longer available. Use copy_to_local or copy_to_remote instead.

1@file.copy_to_local(options: { store: true })
2@file.copy_to_remote(target: "custom_storage")
3
4@file.store #stores file, returns updated metadata
5
6@file.delete #deletes file. Returns updated metadata

Metadata of deleted files is stored permanently.

FileList

FileList represents the whole collection of files (or a subset) and provides a way to iterate through it, making pagination transparent. FileList objects can be created using client.files.list.

1@list = client.files.list(limit: 100)
2@list.each { |file| puts file.uuid }
3
4# Paginate
5@list.next_page
6@list.previous_page
7
8# Load all pages
9@all_files = @list.all

Full documentation

Read the full documentation on GitHub.