Google Cloud Storage - Knowing who uploaded - php

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();

Related

How to get file ID on upload of a file using Google Drive API with Laravel

I am using the "nao-pon/flysystem-google-drive" package for Laravel to use Google Drive as a filesystem and store files.
I also need to call these files and show them, but since Google drive doesn't allow searching by name and only a unique ID, is there a way to get the file ID of the uploaded file from Google Drive.
this is the code I use to upload a file on google drive -
\Storage::disk('google')->put($file_name, file_get_contents($file));
This returns a boolean true and false but I need the file ID so I can store it and call the specific file when required. Is there any workaround?
I understand that you already scripted a PHP Drive API code and you only need to get the new file ID. If my understanding of your question is correct, then you can manage it very easily. After uploading a file using Files.create(), you will get a files resource as a response. In the resource you can find the id property of the generated file. Please ask me any doubts about this approach.
You can use the following code after uploading file:
$details = \Storage::disk("google")->getMetadata($fileName);

How to upload objects to Firebase Storage using Postman for testing?

How to upload images to Firebase cloud storage using postman for testing purposes.
Here we will try to upload images to Firebase Storage using simple upload request in the Google Cloud Storage JSON API.
1) You need to create a project at FireBase console to begin. You can create a project at FireBase console using the link https://console.firebase.google.com/?authuser=0
2) Go to Console and navigate to option stating Storage. If project creation was correct you will get a storage created at default bucket such as imageupload.appspot.com in my case.
3) Now we need the url were we post our image using postman. You can get the url by uploading an image to your storage. Hence upload the image.
click on File location you will get the url of uploaded image. eg:
http://firebasestorage.googleapis.com/v0/b/imageupload.appspot.com/o/IMG-20170630-WA0003.jpg?alt=media&token=f59a9a31-65d7-4e5b-88cf-fc117deacc21 where our required url for posting image is: http://firebasestorage.googleapis.com/v0/b/imageupload.appspot.com/o
4) Now change the Rules at Firebase Storage:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
This will allow a public access to your url. You can read more about it at http://firebase.google.com/docs/storage/security/start
6) Sending a simple upload request:
To use simple upload, create a POST request to the method's /upload URI.
6-a) Add the query parameter uploadType=media.
For example, for a bucket named myBucket:
POST https://firebasestorage.googleapis.com/v0/b/imageupload.appspot.com/o?uploadType=media
6-b) Add a name query parameter to identify which resource the upload is associated with.
For example, to specify that an object's name is myObject:
POST https://firebasestorage.googleapis.com/v0/b/imageupload.appspot.com/o?uploadType=media&name=myObject
6-c) Add the file's data to the request body.
6-d) set contentType: image/png as I am uploading a png image from postman.
6-e) Send the request.

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:
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!

Generating encoded URL's for file sharing stored on amazon S3

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

Display images stored in AWS S3 on website with php

I'm building a MYSQL database driven website on a AWS EC2 instance. Users can submit their data and we will automatically create a web page for them. The webpage will display their submitted data including a photo. Upon original submission of the user's data, we store the photo in S3 using AWSSDKforPHP. When their information is approved by an administrator a webpage is created via a php script.
I've tried the method of generating a signed url. This however requires a expiration time. Is there a way around this? It also includes your access and secret key in the request. Not sure if that is the most secure way to do things. I'd like the method to be as secure as possible so I don't want to make the bucket public. Unless there is a way to make the stored images available only for viewing.
What's the best method to use in a situation like this? Thanks for your help!!
Basically URLs to amazon S3 buckets are s3.amazonaws/bucket_name/key_name all you need to do is make sure the content on those buckets is publicly available and that mime type for those keys is indeed image (image/jpeg or image/png)
I used this web tool to manage my site. I opend my bucket just to its ip and now i can control the restriction from the tool, for the basic viewer I gave read access only

Categories