Retrieving image from Git LFS in Netlify Functions

URL: https://relaxed-euclid-50b68f.netlify.app/
Function URL: https://relaxed-euclid-50b68f.netlify.app/.netlify/functions/generate-og-image?image=%2Fimages%2Fpastrami.jpeg&title=Pastrami%20Sandwich

I’m looking to retrieve images tracked in Git LFS from a serverless function, primarily for using the posts ‘Featured Image’ property while generating an og:image

When requesting the file from the endpoint directly, I receive a 401 response due to it being unauthenticated.

const downloadImage = (url) => {
  request.get(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      const data = "data:" + response.headers["content-type"] + ";base64," + Buffer.from(body).toString('base64');
      console.log('DATA: ', response);
      return data;
    } else {
      console.log('DOWNLOAD IMAGE ERROR: ', error, response);
    }
  });
}

const profileImage = downloadImage(`${GIT_LFS_ENDPOINT}/images/rotated-logo.svg`);
// Returns null error, but the response statusCode is 401 Unauthorized

Can anybody help me with this?

Hi, @mattjschad. The Large Media endpoint you are trying to pull from is designed to be used by Git only. The images themselves are available at the deployed site URLs not at those URLs (the URLs with /.netlify/large-media in the path).

In other words, you should not be using the Large Media URLs to access the images. You should be using the deployed site image URLs to access the images.

If there are other questions about this, please let us know.

1 Like