Dynamic policy and signature creation for browser uploads to Amazon S3 - php

I would like to select a file in an HTML form and upload it directly to Amazon S3. I understand the principles of browser-based uploads to S3 using POST, but how can I create a policy and signature for the form if I haven't yet chosen a file to upload? Should I use AJAX to send the filename to my server and return an appropriate policy and signature based on the file extension?

You can use Ajax, Flash or any other method to upload the file to your server and then transfer it to the Amazon S3.

Related

Google photo reference image download and upload to S3

I am working on download google photo reference image and directly upload to S3 after verifying that image exists on url in PHP. Now currently I am unable to download image from google photo reference image
https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=XXXXXXXXX
When hit this url in web browser that browser redirect me to the another URL. Now I simply verify this url that image exists on this url or not if image exists oh this url and url is valid then simply upload this image to S3 bucket I am using Lumen microservice for this task.
If there is any third party library that provide this functionality and provide help to comlete this task. So kindly suggest.

Azure Blob PHP SDK - Upload directly from a custom multipart API request to Azure Storage

I am using the Azure\azure-storage-php library for PHP to upload a blob file (image or video in my case) to the Azure Blob Storage. The file is uploaded from a mobile app using a multipart API call to the server which then uploads it to the blob storage.
The problems in this scenario are:
The file takes double the time to upload, as it is fully uploaded at first to the server then from the server to Azure.
Once uploaded to the server, a success response (HTTP 200) is returned to the mobile app (from our server API). But actually the file is not yet available in Azure and might take some time depending on its size to be ready.
What I'm looking for is a way to 'stream' the file immediately from the multipart to azure (as a pass-through) to prevent this 'double upload' scenario.
I don't want to give the mobile app the direct link to the blob storage to prevent abuse, in addition I need to perform extra checks (ex mimetype checking) which is why I need the server in between.
Is this achievable?
For reference, here's the sample I'm using (my code is basically the same): storage-blobs-php-quickstart/blob/master/phpQS.php.
It sounds like you constructed a multipart/form request to send form data with a blob file to your server from your mobile app, then to put the blob in its request to Azure Blob Storage from your server. So in the process while, actually the same blob content is transfered from mobile to your server to Azure Storage that takes double the time of uploading a blob.
However, if you want to directly upload file to Azure Storage to across your server, it's impossible for using a multipart/form request, you must have to separate a standalone request for the file in multipart/form to do the upload operation.
In this case, to putting a blob from mobile app, I think you can directly using Azure Storage SDK for Android or iOS, even for JS on browser (a sample for uploading blob from browser directly) for using webkit widget in mobile app to upload file.
As you think, it may be not secure to write the account name & key of Azure Storage to your mobile app. But you can make an API for generating a blob url with sas token and write permission in PHP on your server, and then call it from mobile to get the url to upload blob.
The sample code for generate a blob url with sas token and write permission is like the function generateBlobDownloadLinkWithSAS of the offical sample code, just change 'r' with 'cw'.
Then, a simple PUT request with the sas url is enough for uploading file, as the REST document section Example: Upload a Blob using a Container’s Shared Access Signature said as the sample below.
PUT https://myaccount.blob.core.windows.net/pictures/photo.jpg?sv=2015-02-21&st=2015-07-01T08%3a49Z&se=2015-07-02T08%3a49Z&
sr=c&sp=w&si=YWJjZGVmZw%3d%3d&sig=Rcp6gQRfV7WDlURdVTqCa%2bqEArnfJxDgE%2bKH3TCChIs%3d HTTP/1.1
Host: myaccount.blob.core.windows.net
Content-Length: 12
Hello World.

How uploading to S3 through PHP script on EC2 works?

Please help me understand the process of uploading files to Amazon S3 server via PHP. I have a website on EC2, which will have PHP script for uploading file to S3 server from client's machine. What I need to understand, is whether the file will go directly to S3 from client's machine, or if it will first be uploaded onto EC2, and then to S3. If it's the second option, then how can I optimize the upload so that file goes directly to S3 from client's machine?
It is possible to upload a file to S3 using any of the scenarios you specified.
In the first scenario, the file gets uploaded to your PHP backend on EC2 and then you upload it from PHP to S3 via a PUT request. Basically, in this scenario, all uploads pass through your EC2 server.
The second option is to upload the file directly to S3 from the client's browser. This is done by using a POST request directly to S3 and a policy that you can generate using your PHP logic, and attach it to the POST request. This policy is basically a set of rules that allow S3 to accept the upload (without it anyone would be able to upload anything in your bucket).
In this second scenario, your PHP scripts on EC2 will only need to generate a valid policy for the upload, but the actual file that's being uploaded will go directly to S3 without passing trough your EC2 server.
You can get more info on the second scenario here:
http://aws.amazon.com/articles/1434
even if it's not PHP specific, it explains how to generate the policy and how to form the POST request.
You can also get more information by reading through the API docs for POST requests:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
EDIT: The official AWS SDK for PHP contains a helper class for doing this: http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.S3.Model.PostObject.html

API - To upload image and to list them

I want to upload image to a image hosting service (free). I want users to upload images using the form in my website, but directly to the my image hosting's account(in a album). I don't want users to upload images to my server first and then later upload this file to imagehosting account by using my server script.
I have read about Imageshack's API and imgur API. But i donot think they will allow direct uploading.
Do you know any image hosting service or API, that allows:
uploading images directly to the image hosting account (by users of
my website, instead of using script to do the upload)
easy to use API with PHP
allows fetching of images from the albums later
Please help
Try to use uploadify library. This is very nice and usefull lib.
http://www.uploadify.com/

How to Delete a file from Amazon S3 by HTTP Request using PHP

I would like to find a simple PHP script that will send a HTTP Request to my bucket in Amazon S3 and delete a file. I have found code around the web using PHP that manages S3 files, but what I am looking for is just the delete request. I already know the name of the file and the bucket, I just want to send the request to delete it.

Categories