简体   繁体   中英

API-centric web application file upload

I am creating an API-centric web application using PHP. I have read a lot of articles on API-centric arhitecture and design, but I have a problem related to file uploads.

Suppose I have an image and I want to upload it to the API server. How should I upload that image and how then to receive the link to that image?

Here is how I want to create it now:

  1. Select an image using <input type="file"> on client www.domain.com
  2. Upload it to the www.domain.com using POST with multipart/form-data
  3. Send this image with PUT/POST API call to the api.domain.com
  4. api.domain.com will save this image to another server like static.domain.com and will store image's id in the database
  5. Then, when I will need this image, I can use GET API call to the api.domain.com and I will receive image's url (something like static.domain.com/image.jpg )

Aditional questions:

  • Is this approach the right one or I am doing completely wrong?
  • Will I need an aditional server to store uploaded files if my application will be small, or I can store files right on the API server?
  • If I will store images on same server as API server, won't it be strange if image urls will look like api.domain.com/image.jpg ?

PS: We can skip a lot of API-related things as I need only an idea on how to deal with file uploads.

You haven't really said what kind of API that you are going to be implementing here, so I assume that it is just a restful API.

  • Is this approach the right one or I am doing completely wrong?

No, I wouldn't say you're doing it wrong. You would essentially send the file using POST.

  • Will I need an aditional server to store uploaded files if my application will be small, or I can store files right on the API server?

Yes, it will allow you to store this on the same server, I don't see why not. I doubt that you will use a lot of storage, if the application is small.

  • If I will store images on same server as API server, won't it be strange if image urls will look like api.domain.com/image.jpg?

The api.domain.com/image.jpg technically is just the URL that you connect to the API with and GET/POST data. It does not mean the file is going to be that URL. The API could return like:

{
   type: "IMG", 
   id: "1", 
   url: "example.com/uploads/image.jpg"
}

I hope this this helps, even a little!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM