yokawasa / azure-functions-python-samples

Azure Functions Python Sample Codes. NOTE: The project, hosted in a repository, is no longer actively maintained by its creators or contributors. There won't be any further updates, bug fixes, or support from the original developers in the project.
MIT License
392 stars 181 forks source link

Could use example for Http Trigger based file upload #19

Closed SpicySyntax closed 5 years ago

SpicySyntax commented 5 years ago

I am trying to create a function app that has two parts: 1) Http Trigger File upload which uploads a file to blob-storage 2) Blob trigger that downloads the file and performs some operations.

From the examples that are provided it is clear how to imlement 2. But I cannot seem to find an example of an HTTP file upload, and I think it would make these examples very useful if one were to be included.

Cheers!

yokawasa commented 5 years ago

@SpicySyntax Thanks for the feedback. Let me consider and will get back.

yokawasa commented 5 years ago

@SpicySyntax After consideration of this request, I decided not to add a sample function that uploads a file to blob-storage as file uploading may be long-running job thus it may possibly cause unexpected timeout issues.

Please read this in StackOverflow

Now imagine, you might be having a good Internet connection, but the users may not be. There are several other problems that you must take a note of before anything. And this is an excerpt from official documentation, https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices.

Instead, I added a http-trigger-blob-sas-token function that can provide SAS token for accessing Azure Blob storage, and by using this you can achieve the solution introduced in this in StackOverflow

I have found another way of doing things. Here is the solution that works for me.

When a client needs to upload a file, it calls the Azure Function to be authenticated (using the Identity provided by the Framework) & authorized (it can be a simple targeted check in a Table Storage, meaning is (s)he allowed to do such operation).

The Azure Function will ask for a Shared Access Signature to access a specific Blob. The SAS will give the client access to the Blob storage with Write-only privileges for a limited time (watch out of the clock's skew on Azure).

The client will then use the returned SAS to upload the file directly to the Blob storage. That way, it avoids the long term communication with the client as mentioned by Afzaal Ahmad Zeeshan and reduces the overall cost even more as the Azure Function is no more dependent on the connection speed of the client.

I added a sample implementation of the solution above - upload-blob-sas-token.py

Hope this would help

yokawasa commented 5 years ago

I'm closing this for now but please feel free to reopen if you need further discussion

SpicySyntax commented 5 years ago

@yokawasa Yeah it can remain closed, your example is definitely best practice. Thanks for the quick response, that has been quite useful!

snobu commented 5 years ago

Just to emphasise the SAS solution is definitely the way to go here.

The same approach is taken by the Azure IoT Device SDK with its File Upload implementation (if you need a place to start for your own implementation).

jtlz2 commented 5 years ago

@SpicySyntax Great! Did you implement (2) and if so might you be willing and able to share? Thanks!