Unable to represent a file data type

Hello everyone, I’m trying to create a music store API, where you can basically listen and buy songs. The problems I’m having are how i would represent the artwork (picture file) and song (music file) in gorm (Golang orm library) and In what format do I return it to the client after being stored and are there external packages I can use to simplify things? Thank you.

Both are binary data for which you basically have two options:

  1. Store them as binary objects in the database.
  2. Store references to the objects in the database and store the objects on disk or in some external storage like S3.

I an not sure if Gorm supports the first option.

Assuming by API you mean HTTP, you should return such objects using the propery mimetype like image/jpeg and audio/mpeg.

Thank you very much for your reply,
Please can you explain further the second option?

If you store a reference to an object in the database, an URL would be the most portable way. An URL could be something like

  • file:///usr/share/objects/123.jpg to reference a file in the local file system
  • https://www.example.com/objects/123.jpg to reference a file on a HTTP server

The database would then contain just this URL.

To respond to a client request (say a HTTP request) for an object, your application would use Gorm or any other SQL library to get the URL from the database. It would then fetch the content of the object from the URL which would result in a []byte. And then it would return this byte array to the client as the HTTP response, setting the propert HTTP headers like Content-Type: image/jepg.

1 Like

I would try it out. Thanks

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