0

I'm using an Azure Function to retrieve a .xlsx file using an HTTP GET request and upload it to blob storage. In the code, I'm using file.open to write the content to a temporary file and then uploading the file to blob storage.

This is the code I'm using.

fileData = open(pathlib.Path(__file__).parent / fileString, 'w+').write(r.content)

but it fails, and the Application Insights response says:

Exception: FileNotFoundError: [Errno 2] No such file or directory:
5
  • 1
    parent path of a function ? that can't be the path to your blob Commented Apr 13, 2021 at 9:16
  • I thought it's the parent path for the Function Commented Apr 13, 2021 at 9:31
  • storage blob don't have the directory structure. Commented Apr 13, 2021 at 9:39
  • I'm not writing to the storage blob. I'm writing to a temporary file and then uploading that to the storage blob. Commented Apr 13, 2021 at 9:39
  • 1
    My understanding is that using 'w+' in open() creates the file. Commented Apr 13, 2021 at 9:58

2 Answers 2

1

The following two paths work well in singleton mode on my side:

1,

import logging
from pathlib import Path

import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
    path = str(Path.home()) + "/test.txt"
    fileData = open(path, 'w+').write("testsomething")
    return func.HttpResponse(
            "create file with no problem."+path,
            status_code=200
        )

2,

import logging
import tempfile
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    path = tempfile.gettempdir() + "/test.txt"
    fileData = open(path, 'w+').write("testsomething")
    return func.HttpResponse(
            "create file with no problem."+path,
            status_code=200
    )

This article says, Files written to the temporary directory aren't guaranteed to persist across invocations(Maybe your problem is similar, but not sure).

Your code seems to have nothing to do with multi-instance, but any way, this is not the recommended method. As user3732793 says in his answer, you can directly send data to storage blob without temporary file. Just retrieve a .xlsx file using an HTTP GET request and send data to blob storage by blob output binding or blob storage sdk.

Sign up to request clarification or add additional context in comments.

Comments

0

I used tempfile to solve the issue.

    fp = tempfile.NamedTemporaryFile()
    fp.write(r.content)

And then, when uploading to blob storage, I had to open the file and read the bytes.

    with open(fp.name, 'rb') as data:
      logging.info(data)
      if azureConnector.exists():
        logging.info('connector exists')
        return
      azureConnector.upload_blob(data, overwrite=True)

This is uploading the file to Azure blob storage now.

1 Comment

Can you tell me where is the wrong of my answer? In addition, if the function is extended to multiple instances, it will not be able to find files generated by one instance from another instance. This method is not recommended. . .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.