[✅ Solution] Automation: Load Image From URL and Save it

I try to load an image from an external URL and then save it in a image field of the same entry.

How would you do that?

1 Like

Hi @Roman,

thanks for your post - and welcome!

Let me get back to you with a working example asap, will share it here once I have it available.

Cheers & have a great day
Tim

1 Like

Hi @Roman,

excuse the delay - we had to deploy a little update fix to Tape this evening, that makes this even smoother for you.

Here’s the drill - use a simple execute script action inside a workflow to

  1. download the external image via the URL
  2. upload the file to the Tape API to get a file reference
  3. attach the file to the current record.

The script to use would look similar to this one:

// download your image via the external URL
const url = image_field_via_url_field_url_value;
const { data: file_content } = await http.get(url, {responseEncoding: 'binary'});

// upload the downloaded image to Tape
const encoded_file_content = Buffer.from(file_content, 'binary');
const { data: uploaded_file } = await tape.File.upload({filename: 'image.png', source: encoded_file_content});

// update current record using the uploaded file reference
const uploaded_image_file_id = uploaded_file.file_id;
await tape.Record.update(current_record_id, {fields: {image:uploaded_image_file_id }})

This script assumes that image_field_via_url_field_url_value holds your external image URL. You can replace it with your field variable name, or use a static image URL for testing. Also, ensure that in the last line you provide the proper external ID of your image field, in my case it is simply image.
Usually, it corresponds to the field name in lowercase, but you can look it up in the developer section of the app:

Ultimately, the workflow may look similar to this:

Just tested this and it works like a charm and adds external images to my record quickly and reliably:

Let me know if that works for you!

Happy building & Cheers
Tim

2 Likes

Hey Tim, This is a really great explanation, but what if there is more than one image?

@DB_Looper can you explain your situation in a bit more detail so we can help you. Do you have multiple image links? a field in Tape with multiple images? Something else?

Hey, Sorry I don’t know how I missed this notification.

I have multiple image links in a tapeapp field. All separated by commas.

1 Like

If you have a comma separated list of n image URLs, you will need a loop to accomplish that.

Your code might look something like this:

// build an array of your image URLs (use your field variable name here!)
const image_urls = image_field_via_url_field_url_value.split(',');

// for each URL, download image and collect Tape file ref
const uploaded_file_ids = [];

for (const image_url of image_urls) {
  // download image via the external URL
  const { data: file_content } = await http.get(image_url, {responseEncoding: 'binary'});

  // upload the downloaded image to Tape
  const encoded_file_content = Buffer.from(file_content, 'binary');
  const { data: uploaded_file } = await tape.File.upload({filename: 'image.png', source: 
  encoded_file_content});

  // add file IDs to array for later use
  uploaded_file_ids.push(uploaded_file.file_id);
}


// update current record using the uploaded file references
await tape.Record.update(current_record_id, { fields: { image:uploaded_file_ids }})

This worked first try in my dummy scenario :rocket:

Happy building - let me know if it works @DB_Looper :eyes:

2 Likes

@Tim - The above is really helpful, I’ve found myself using it a lot!
Is there a way to make the upload silent? It’s not a big deal but I’m just trying to limit notifications to only what needs to be taken note of at as much as possible

Hi @Martin_E,
I think you already have this option.
Just click “…” next to action block and then select silent option.

@tomaz - The files are uploaded via a script. The silent option you’ve shown in the screenshot doesn’t apply to script bricks

1 Like

@martin thanks for your question - fully understood. You might want to look into the advanced option that the Tape developer API, and i.e. it’s client that we expose inside workflow automation script action bricks. One of the options is the silent flag that you can pass along as follows:

(in the first screenshot I used the CMD+SPACE of our code editor to explore possible values of the object - quite handy)

That could do the trick. Let me know if it worked :rocket:

2 Likes

Perfect. That did the trick. Thank you!

1 Like