Vituvo
Vituvo

Reputation: 1048

Save an image file into a database with node/request/sequelize/mysql

I'm trying to save a remote image file into a database, but I'm having some issues with it since I've never done it before.

I need to download the image and pass it along (with node-request) with a few other properties to another node api that saves it into a mysql database (using sequelize). I've managed to get some data to save, but when I download it manually and try to open it, it's not really usable and no image shows up.

I've tried a few things: getting the image with node-request, converting it to a base64 string (read about that somewhere) and passing it along in a json payload, but that didn't work. Tried sending it as a multipart, but that didn't work either. Haven't worked with streams/buffers/multipart all that much before and never in node. I've tried looking into node-request pipes, but I couldn't really figure out how possibly apply them to this context.

Here's what I currently have (it's a part es6 class so there's no 'function' keywords; also, request is promisified):

function getImageData(imageUrl) {
    return request({
        url: imageUrl,
        encoding: null,
        json: false
    });
}

function createEntry(entry) {
    return getImageData(entry.image)
        .then((imageData) => {
            entry.image_src = imageData.toString('base64');
            var requestObject = {
                url: 'http://localhost:3000/api/entry',
                method: 'post',
                json: false,
                formData: entry
            };
            return request(requestObject);
        });
}

I'm almost 100% certain the problem is in this part because the api just takes what it gets and gives it to sequelize to put into the table, but I could be wrong. Image field is set as longblob.

I'm sure it's something simple once I figure it out, but so far I'm stumped.

Upvotes: 1

Views: 4074

Answers (1)

rsp
rsp

Reputation: 111506

This is not a direct answer to your question but it is rarely needed to actually store an image in the database. What is usually done is storing an image on storage like S3, a CDN like CloudFront or even just in a file system of a static file server, and then storing only the file name or some ID of the image in the actual database.

If there is any chance that you are going to serve those images to some clients then serving them from the database instead of a CDN or file system will be very inefficient. If you're not going to serve those images then there is still very little reason to actually put them in the database. It's not like you're going to query the database for specific contents of the image or sort the results on the particular serialization of an image format that you use.

The simplest thing you can do is save the images with a unique filename (either a random string, UUID or a key from your database) and keep the ID or filename in the database with other data that you need. If you need to serve it efficiently then consider using S3 or some CDN for that.

Upvotes: 3

Related Questions