How to save and process a file in Azure Function

Mansi Yadav 40 Reputation points
2024-12-16T09:30:02.3766667+00:00

I have written a script where I'm downloading some files one by one in a directory and then I'm passing the path of file to document intelligence for extracting the content. The script was running as expected in local but after deploying it to function app I got error that the directory where I'm storing the file is in read only mode. So I followed azure function documentation to create a temporary folder using the tempfile module. The issue was resolved but just after downloading the file Im getting error which is not at all clear to me. Please make me understand the error and let me know the solution too. Below is the error message:- Error Occured: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))Traceback (most recent call last):File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 789, in urlopenresponse = self._make_request(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 490, in _make_requestraise new_eFile "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 466, in _make_requestself._validate_conn(conn)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 1095, in _validate_connconn.connect()File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connection.py", line 730, in connectsock_and_verified = _ssl_wrap_socket_and_match_hostname(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connection.py", line 909, in ssl_wrap_socket_and_match_hostnamessl_sock = ssl_wrap_socket(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/ssl.py", line 469, in ssl_wrap_socketssl_sock = ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/ssl.py", line 513, in _ssl_wrap_socket_implreturn ssl_context.wrap_socket(sock, server_hostname=server_hostname)File "/usr/local/lib/python3.10/ssl.py", line 513, in wrap_socketreturn self.sslsocket_class._create(File "/usr/local/lib/python3.10/ssl.py", line 1104, in _createself.do_handshake()File "/usr/local/lib/python3.10/ssl.py", line 1375, in do_handshakeself._sslobj.do_handshake()ConnectionResetError: [Errno 104] Connection reset by peerDuring handling of the above exception, another exception occurred:Traceback (most recent call last):File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/adapters.py", line 667, in sendresp = conn.urlopen(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 843, in urlopenretries = retries.increment(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/retry.py", line 474, in incrementraise reraise(type(error), error, _stacktrace)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/util.py", line 38, in reraiseraise value.with_traceback(tb)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 789, in urlopenresponse = self._make_request(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 490, in _make_requestraise new_eFile "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 466, in _make_requestself._validate_conn(conn)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connectionpool.py", line 1095, in _validate_connconn.connect()File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connection.py", line 730, in connectsock_and_verified = _ssl_wrap_socket_and_match_hostname(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/connection.py", line 909, in ssl_wrap_socket_and_match_hostnamessl_sock = ssl_wrap_socket(File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/ssl.py", line 469, in ssl_wrap_socketssl_sock = ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)File "/home/site/wwwroot/.python_packages/lib/site-packages/urllib3/util/ssl.py", line 513, in _ssl_wrap_socket_implreturn ssl_context.wrap_socket(sock, server_hostname=server_hostname)File "/usr/local/lib/python3.10/ssl.py", line 513, in wrap_socketreturn self.sslsocket_class._create(File "/usr/local/lib/python3.10/ssl.py", line 1104, in _createself.do_handshake()File "/usr/local/lib/python3.10/ssl.py", line 1375, in do_handshakeself._sslobj.do_handshake()urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/home/site/wwwroot/function_app.py", line 228, in process_files_contentfile_content = download_file(token, site_id, drive_id, relative_path)File "/home/site/wwwroot/function_app.py", line 189, in download_fileresponse = requests.get(download_url, headers=headers)File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/api.py", line 73, in getreturn request("get", url, params=params, **kwargs)File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/api.py", line 59, in requestreturn session.request(method=method, url=url, **kwargs)File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 589, in requestresp = self.send(prep, **send_kwargs)File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 724, in sendhistory = [resp for resp in gen]File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 724, in <listcomp>history = [resp for resp in gen]File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 265, in resolve_redirectsresp = self.send(File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 703, in sendr = adapter.send(request, **kwargs)File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/adapters.py", line 682, in sendraise ConnectionError(err, request=request)requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,311 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 5,560 Reputation points Microsoft Employee
    2024-12-25T09:56:34.2366667+00:00

    Hello @Mansi Yadav

    The error message you received indicates that the connection was reset by the peer. This can happen due to various reasons such as network issues, server issues, or firewall restrictions.

    To resolve this issue, you can try the following steps:

    1. Check your network connection and ensure that it is stable.
    2. Check if there are any server issues or maintenance activities going on that might be causing the connection reset.
    3. Check if there are any firewall restrictions that might be blocking the connection. If so, you can try adding an exception for your application in the firewall settings.
    4. Try increasing the timeout value for the requests you are making to the server. This can be done by passing the timeout parameter to the requests.get() method.

    That should get you started.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.