Upload Files via URL to Your Site in Ruby

You can easily upload files via URL on your back end in Ruby; here’s a simple recipe you can drop in to your code today.

First, make sure you’ve got the handy-dandy Filestack Ruby SDK gem on the system upon which you’ll be running this code:

gem install filestack

Or add this requirement to your Gemfile and run bundle in that directory if you want it only installed local to your project.

Ruby SDK installation instructions for the Filestack Ruby Gem to prepare to upload files via URL

Next, create a test file to try this out. I’ll call mine upload_url.rb. This is essentially the same code you’d use to upload a file from disk, but we’ll be using a different argument to client.upload:

require 'filestack'
client = FilestackClient.new('YourApiKeyGoesHere')
filelink = client.upload(external_url: 'https://bit.ly/caterpillarpic')

That’s it! Notice that the URL upload survives the link shortener’s redirect just fine. Also, you’re not just creating a reference to that link; the file there is actually being copied in to your Filestack app and stored. As a bonus, you can feed in URLs during file transformation as well. These transformations are not stored, but they are cached in the CDN upon first call. For example, if you want to upload files via URL to resize an image that exists elsewhere on the web, you can do something like this:

imgurl = "https://bit.ly/caterpillarpic"
transform = client.transform_external(imgurl).resize(width: 100, height: 100)
puts transform.url

This doesn’t store the image; it just constructs the transformation URL programmatically. If you look at the output of puts transform.url, you’ll see it.

You know what? All of that was too easy. Let’s drop this whole thing in a Docker container and make it run with environment variables for our APIKEY and the URL:

FROM filestack/ruby:latest
WORKDIR /usr/src/app
COPY ./upload-url.rb ./upload-url.rb
CMD ruby upload-url.rb

Let’s modify our Ruby code to read named environment variables:

require 'filestack'
client = FilestackClient.new(ENV['APIKEY'])
filelink = client.upload(external_url: ENV['URL'])

We’ll build the Docker image with the Dockerfile and upload-url.rb files in the same directory with the docker build command:

docker build -t filestackruby .

That dot at the end of the command is not a typo; you need it to specify that this directory is the context of the build. We can name that image anything we want. Then we can set the ENV variables temporarily in our shell:

export APIKEY="PasteYourAPIKEYhere"
export URL="https://bit.ly/caterpillarpic"

After that we can run this with:

docker run -it -e APIKEY=$APIKEY -e URL=$URL filestackruby

Magic! Any valid file URL that you passed via the ENV variable to the Docker container just got uploaded to your Filestack app!

But wait, there’s more! I already did that for you and pushed to the Docker Hub, so you can just set your ENV variables and run this one command:

docker run -it -e APIKEY=$APIKEY -e URL=$URL filestack/ruby:upload-url

Ok, ok… last bonus command for this recipe, I promise! Let’s clean up after the container runs with –rm and give it a name:

docker run -it --rm -e APIKEY=$APIKEY -e URL=$URL --name fsrbupurl filestack/ruby:upload-url

Nice. Now we can upload files via URL by passing variables to a simple container. You can use this same approach to build up a complete back end to handle all of your file ingest needs.

Read More →

Ready to get started?

Create an account now!