I've developped an simple FileUploader Service based on the Symfony documentation found here: https://symfony.com/doc/current/controller/upload_file.html
My app is just a simple API called inside a Ionic Mobile App.
What I want to know is what is the best practices to give access to my uploaded images.
For exemple, I have an Sport entity App\Entity\Sport that stores an image:
When i request GET /api/sports/ I want to send as response the full url to the image so it can be displayed inside the mobile app (e.g. http://symfony/api/public/uploads/sports/XXXXX.jpg)
Maybe I'm overcomplicating the issue but do I need to make an GET /public/uploads/sports/{id} as an endpoint to get my pictures ? because if possible i would like to have the pictures instantly available after the GET /api/sports and not having to query another time for the picture aswell
Why not just simply add the URL into a field of the JSON response object, where you can access the the picture directly? IMHO there is nothing wrong with that.
I find https://github.com/WhiteHouse/api-standards a good reference to start with if it comes to these kind of decisions.
Related
I am developing a api using codeigniter for user signup and user update profile.
I have image field in form. To create user i have used post method which works fine.
But issue i am getting when i am updating user. I know that with put request we have to send urlencoded or json data. But what should i do to update image?
Should i make 2 request? One with normal url encoded data (PUT request) and one with post request (Update image).
In rest api development which is the significant way to deal with these type of request?
can anyone help me?
You can try Advance Rest API plug-ins or browser extension, or you can try postman software for testing api with multipart-formdata (images)
enter image description here
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:
Select an image using <input type="file"> on client www.domain.com
Upload it to the www.domain.com using POST with multipart/form-data
Send this image with PUT/POST API call to the api.domain.com
api.domain.com will save this image to another server like static.domain.com and will store image's id in the database
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?
P.S: 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!
I'm currently porting a webservice I've built to work with Google App Engine. One of the main functions in the webservice is to upload an image (a profile picture, for example). Currently what I do is:
Authenticate the user who wants to upload the image using his unique API KEY and other data.
Upload the file, give it a unique name and store the name of the file inside the user's row in the mysql database.
Now in order to port the file upload to App Engine I'm using Google Cloud Storage and following this tutorial:
https://cloud.google.com/appengine/docs/php/googlestorage/user_upload
I'm trying to get the file upload to work with my Android app the following way:
The user makes a request, the webservice authenticates him and sends in response the upload url created by CloudStorageTools::createUploadUrl.
The user uploads the image to this URL
Now heres the problem: After the upload is done, a POST is made to the given php file in createUploadUrl (I quote from google's docs) with the uploaded file. But how can this script know who uploaded the file it got? I can't pass any parameters indicating who uploaded the file to createUploadUrl so I can't insert the file name to a user in the Cloud SQL database, so now theres only a file not associated with anything in Cloud Storage.
Any hints? Am I missing something?
I'm posting this as a separate answer because a) it's different approach than the first answer, and b) I'd advocate for my other answer over this one because I think it's best to let GAE handle auth. However, I think you can do what you're trying to do this way:
Instead of routing a singular URL to your upload handler, use a regex match like this in your app.yaml to route any matching URLs to your handler:
handlers:
- url: upload_handler/(.*)
script: my-php-script-that-uploads-stuff.php
Then when invoking createUploadURL, simply pass in your API_KEY after the 'upload_handler/' as a query argument, e.g.
$upload_url = CloudStorageTools::createUploadUrl(sprintf('/upload_handler/?API_KEY=%s', $API_KEY), $options);
Then in my-php-script-that-uploads-stuff.php:
parse_str(parse_url($_SERVER['REQUEST_URI'])['query'])
This will parse the request URL to get the query string and then parse the query string, populating the value of API_KEY with the value passed in the URL in your local scope.
I just tested this pattern of extracting stuff from the request URL in a php script in with dev_appserver and it worked.
I think you can do this by using the google appengine users service in your php script.
From the link above, from the Overview page of the users service, here's how you get the current user:
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
I have some Users in my API Service Authenticating with OAuth, via a Web Client (website).
When the User requests their private profile images.. how should these be served up? I see a few options few drawbacks and unsure what's the best decision going forward.
1) host the images with a hashed name... eg (~/public/folder/309dsfas928f39rjkfe93.jpg)
Pro
- Simple, keeps all assets centralized to the Apigility based server.
Con
- nothing preventing something from finding an image .. no of assuring access control once a URL is found.
2) pass back the img data in a JSON request
Pro
- You're assured that the person receiving an image has permission.. every time the image is accessed
Con
- now images need to be stored (temporarily?) on the WebClient's server..
- images traveling in JSON seems wrong to me
Are there datastreaming options instead? Thank you
Nice question!
Right now I have a solution where I pass the image in a base64 encoded sting in the json response. This works nicely and on the client side you can quickly paste the string in the src tag of the html image. So when it comes to workflow it is very smooth.
But I guess others might have something to say about this solution :P
I am working on file sharing for objects stored on amazon S3.Now the path for the object stored on S3 is default like this https://s3.amazonaws.com/bucket_name/path_to_file/file_name.jpg/docx etc.Now I want to share these file URLs via email through my app.
Currently when I share I see the entire URL as is in the email.I want it to be sent in an encoded form so that its hard to guess the exact location of the file.
I am using PHP and I was planning to use base_64_encode/decode functions or md5 the URLs but not sure if thats the right way to go.
So,I am looking for some tool or API (by amazon ot 3rd party) that can do it for me.
I would also like to shorten the URLs while sharing.
Would like to seek advice and guidance from someone implemented something similar.
Not sure if it comes under URL-REWRITING but tagging it under it.
Thank you
You have two options to do this:
You can map your url and s3's url in your server, and give your url to user. When user make request to your url, redirect it to s3's. You can ref http://en.wikipedia.org/wiki/URL_redirection
You can call API provided by url redirect service provider, e.g. tiny.cc/api-docs