Can I create a resource from an external URL during build?

I have a rather odd use-case where I need to serve a few short mp4 videos on my Hugo site and I need them not to be external resources (for caching purposes). Now normally I’d just drop the videos into my assets folder and call it a day, but I also don’t want the videos in my git repository since they are big and we already made a big deal about removing all media from our repo a few months ago. Unfortunately it turns out the solution we used (which is geared toward images) won’t cache videos and it’s causing a bunch of problems for us.

So what I’d really like to do is just grab the three videos from our public S3 bucket during builds so that we can have them hosted on the same server as the rest of the site, but we don’t have to store them in our git repo.

I looked at using [[module.mounts]] to grab the videos from the URL during build and put them into the assets folder at that time but that (unsurprisingly) didn’t work. I also know I could use getJSON if this was pure data, but that doesn’t work either since it’s a video.

I can think of a few other solutions like using a pre-build script or implementing git-lfs, but I’d rather not bother with all that for just 3 files if I don’t have to. So I would love to hear if there is in fact a way to just do this with Hugo (or if anyone has a better idea).

Thanks!

What is the largest file size?

355 KB (although to be honest I’ve just realized I was trying to use the 13 MB high quality version this whole time, so if module.mounts works for smaller file sizes then that may have been it).

OK, so they aren’t that big…

Then create a separate repository for these videos; that’s probably the easiest way to mount them as assets.

Here’s a test repository with some videos:
https://github.com/jmooring/hugo-content

Follow the instructions in the README to mount the assets directory, then test it with:

{{ with resources.Get "videos/a.mp4" }}
  <video controls width="640" height="360">
    <source src="{{ .RelPermalink }}" type="video/mp4">
  </video>
{{ end }}

{{ with resources.Get "videos/b.mp4" }}
  <video controls width="1280" height="720">
    <source src="{{ .RelPermalink }}" type="video/mp4">
  </video>
{{ end }}
1 Like

@jmooring is right that the above is currently probably your best option. We do plan to add a resources.GetRemote function …

3 Likes

Thanks @jmooring! Ended up not needing to do this as the underlying issue was different than we originally thought, but this is nonetheless a great solution. Hopefully it will help someone else (or myself) in the future.

I am also very excited for the day we get resources.GetRemote!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.