PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] how to Upload a files from Desktop to Azure blob storage using azure Function app

 July 30, 2022     azure, azure-blob-storage, azure-functions, file-upload, python     No comments   

Issue

I'm trying to Upload some files form my PC through a azure function app. At first i read some excel files then i do some Cleaning processes using Pandas and i want to upload it to Azure function app. offline every works fine, but as i published to Azure it gives Error 500. I'm guessing it can't get files from my PC.

my code looks like this:

#I get my files offline
AISauftragID = req.params.get('ID','756382')
Position = req.params.get('Position','7')
location = fr'C:\Users\bb\ff\Doc\VS Code\{ID}-{Position}'
#Some Cleaning using pandas
#connecting to Azure
blob_service_client = BlobServiceClient.from_connection_string("blablabla")
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('foldername')
try:
    # Create new Container in the service
    container_client.create_container()
    properties = container_client.get_container_properties()
except ResourceExistsError:
    print("Container already exists.")
blob = BlobClient.from_connection_string(conn_str="blablabla",
                                                container_name="foldername",
                                                blob_name="filename") 
if blob.exists():
        logging.info('filename.csv exists')  
        return func.HttpResponse(
            "filename.csv exists",
            status_code=200)
        
else:
    blob_client = container_client.get_blob_client("filename.csv")
    # upload data
    blob_client.upload_blob(dataframe.to_csv(index=False, encoding = "utf-8"), blob_type="BlockBlob")
    return func.HttpResponse(
            "Konto.csv exists",
            status_code=200)

my function.Json file looks like this:

    {
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
   
  ]
}

Solution

I'm guessing it can't get files from my PC.

That is correct. The azure function runs somewhere in an Azure Cloud data center. There is no way this function can access the local harddrive of an on-premises workstation. Also, you really don't want to because it would be a major security risk.

You seem to have the idea to use an azure function for uploading the local file, as stated, that is a no go. You need some client process like a browser or application to send the file to azure and then you can write an azure function or whatever to process that file.

To process a file you either create an http triggered azure function that accepts a POST action with the file content or you upload the file to, for example, azure blob storage and create a blob triggered / event grid triggered azure function to process the file.



Answered By - Peter Bons
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing