0

I am trying to write Python code to connect to Azure File Share with URL with some credentials/keys:

I am running this code from an IDE (Visual Studio Code).

But, I am not sure how to put URL info.

from azure.storage.file import FileService

storageAccount='cocacola'
accountKey='xxxdrinksomethingxxxx'

file_service = FileService(account_name=storageAccount, account_key=accountKey)

share_name = 'dietcoke'
directory_name = 'test'
file_name = '20230728.csv'
file = file_service.get_file_to_text(share_name, directory_name, file_name)
print(file.content)

Currently, error message is "azure.common.AzureMissingResourceHttpError: The specified parent path does not exist. ErrorCode: ParentNotFound"

How do I add the URL info code here?

Java
  • 1,208
  • 3
  • 15
  • 29
  • 1
    Have you tried using the method [`get_file_to_path`](https://learn.microsoft.com/en-us/python/api/azure-storage-file/azure.storage.file.fileservice.fileservice?view=azure-python#azure-storage-file-fileservice-fileservice-get-file-to-path) Any way, here the message says the parent file is not here. Are you sure about the path you provide? – BeGreen Jul 28 '23 at 21:06
  • @BeGreen I have not tried the get_file_to_path, but from my code, where is path information? I basically tried some other example of code (https://stackoverflow.com/questions/58056141/load-files-to-azure-file-storage-using-python). – Java Jul 28 '23 at 21:13
  • 1
    Then the file you are trying to download does not exists. Be sure to have to correct storage account/key, share name/directory/file name. At this point I don't think I can help more since the code is technically correct. – BeGreen Jul 28 '23 at 21:19
  • @BeGreen I got the assumption that we need to have another code to put URL separately. I will check/debug again. Regards to "directory_name", is it like a folder hierarchy within Azure File Share? – Java Jul 28 '23 at 22:38

1 Answers1

1

Currently, error message is "azure.common.AzureMissingResourceHttpError: The specified parent path does not exist. ErrorCode: ParentNotFound"

The above error when you don't specified path or directory in your storage account.

Here is my structure in the storage account :

Storage account name = venkat123 
fileshare=fileshare1
directory=directory1
filename=day.csv 

Portal: enter image description here

You can use the below code to read the CSV file with the account URL and key.

For that, you need to install the package of azure-storage-file-share.

Code:

from azure.storage.fileshare import ShareServiceClient, ShareClient, ShareDirectoryClient, ShareFileClient
import pandas as pd
import io

account_url = "https://venkat123.file.core.windows.net"
account_key = "xxxxxx"
service_client = ShareServiceClient(account_url=account_url, credential=account_key)
share_name = "fileshare1"
share_client = service_client.get_share_client(share_name)

# Get a ShareDirectoryClient object to connect to the directory
directory_path = "directory1"
directory_client = share_client.get_directory_client(directory_path)
file_name = "day.csv"
file_client = directory_client.get_file_client(file_name)

file_contents = file_client.download_file().readall()
csv_data = file_contents.decode('utf-8')
df = pd.read_csv(io.StringIO(csv_data))
print(df)

Output:

  Numeric  Numeric-2 Numeric-Suffix
0         1          1            1st
1         2          2            2nd
2         3          3            3rd
3         4          4            4th
4         5          5            5th
5         6          6            6th
6         7          7            7th
7         8          8            8th
8         9          9            9th
9        10         10           10th
10       11         11           11th
11       12         12           12th
12       13         13           13th
13       14         14           14th
14       15         15           15th
15       16         16           16th
16       17         17           17th
17       18         18           18th
18       19         19           19th
19       20         20           20th
20       21         21           21st
21       22         22           22nd
22       23         23           23rd
23       24         24           24th
24       25         25           25th
25       26         26           26th
26       27         27           27th
27       28         28           28th
28       29         29           29th
29       30         30           30th
30       31         31           31st 

enter image description here

Reference:

Azure Storage File Share client library for Python | Microsoft Learn

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • Thank you so much for your code. I am getting this error: azure.storage.fileshare._shared.authentication.AzureSigningError: Invalid base64-encoded string: number of data characters (85) cannot be 1 more than a multiple of 4 – Java Jul 31 '23 at 18:07
  • 1
    Hi, @Java The issue may be that you are passing an incorrect account key. Check your storage account name and the corresponding account key. Ensure the key is copied correctly and there are no extra spaces or characters. – Venkatesan Aug 01 '23 at 06:31
  • 1
    You were right. There was one character missing in the account key. Thank you so much! – Java Aug 01 '23 at 16:58