'Downloading and Storing Images in a Server Database

So currently my website is loading an JSON File from my server, looking for the data it needs and then showing it up on the screen as well as downloading and showing an image related to that info from a public API. I was wondering how would I go about storing the API downloaded image to my own servers database so before every image fetch it would check for the image in my server and if it doesn't exist, it would go to the public API and download and store it in my servers database?


This code below is a sample, when the site is loaded it pulls a random card from the JSON and displays its info along with the image. I want to download the image from the api, store it in my servers database and then check for said image before downloading as to reduce API calls to a minimum.

function RandomCard() {
const RC = Math.floor(Math.random() * cardAPIListings.data.length); 
var cardShowcase = document.getElementById("randomcardshowcase"); 
var randomCardImage = document.getElementById('randomImage'); 
  switch (cardAPIListings.data[RC].type) { 
    case "Spell Card": 
    case "Trap Card":

    cardShowcase.innerHTML = '\"' +cardAPIListings.data[RC].name + '\"' + '<br><br>' +  '[' +
    cardAPIListings.data[RC].race + ' / ' + cardAPIListings.data[RC].type + ']' + '<br>' + 
    'Attri: ' + cardAPIListings.data[RC].attribute + '<br><br>' + 'Effect: ' + '<br>' + 
    cardAPIListings.data[RC].desc;
      
    randomCardImage.src = cardAPIListings.data[RC].card_images[0].image_url;
    break;

Edit: The PUBLIC API that I'm using says this:

[ Card Images Users who pull our card images directly from our server instead of our google cloud repository will be immediately blacklisted.

Card images can be pulled from our Google Cloud server but please only pull an image once and then store it locally(I Assume this means Store it in your Own Server/API Database). If we find you are pulling a very high volume of images per second then your IP will be blacklisted and blocked.]


So I need a way to store the images in my own location as they are being downloaded from the Public API so I Dont get blacklisted.



Solution 1:[1]

Unless I'm misunderstanding your question, only store images and other binary data to the file system. No reason to burden database with big binary data. Only store file link (or URL) in the database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DareM