0
votes

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.

1

1 Answers

0
votes

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'.

enter image description here

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.