0

I am trying to make the call

from azure.storage.fileshare import  ShareDirectoryClient

shrdDirClient = ShareDirectoryClient.from_directory_url
(detailedFileURI,snapshot=None, credential=None)

but resulted in the error above. I tried

if hasattr(ShareDirectoryClient, 'from_directory_url'): 
   print("Present")

But it did not go into the loop.

My full code is too long. This is another approach I tried resulting in 'str' object is not callable error

from azure.storage.fileshare import  ShareDirectoryClient
from datetime import timedelta,datetime
now = datetime.now(timezone('UTC'))
sasToken = generate_share_sas(accountName, shareName, accountKey,\
             permission=AccountSasPermissions(read=True, \
                                    write=False, \
                                    delete=False, \
                     list=True, create=True), expiry=now + timedelta(days=3650)\
                                    )
accountURL = "https://nsclusterhdistorage.file.core.windows.net"
shareName = "dev-archived-data"
detailedFileURI = accountURL+'/'+shareName
sh = ShareDirectoryClient()
sh.from_directory_url(detailedFileURI,snapshot=None, credential=sasToken)

I am relatively new to python azure storage file share. can someone help

  • Try creating object of the class ShareDirectoryClient and use that to call the function – Nukala Raghava Aditya Oct 02 '21 at 12:12
  • Sorry, I can't reproduce this with the code you've given. What version of `azure-storage-file-share` do you have installed? – Luke Woodward Oct 02 '21 at 13:01
  • @NukalaRaghavaAditya: that won't help, [`from_directory_url` is a class method](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-file-share/azure/storage/fileshare/_directory_client.py#L124) – Luke Woodward Oct 02 '21 at 13:02
  • Class methods must be invoked using object right ? – Nukala Raghava Aditya Oct 03 '21 at 14:21
  • @Luke Woodward I have installed Version 12.6.0. Which version have you installed? – user17058107 Oct 04 '21 at 05:14
  • @user17058107: I have also installed version 12.6.0. I can only assume that (a) your code is incomplete (you don't specify what `detailedFileURI` is, so I think that assumption is safe), and (b) in the code you have chosen not to share with us you have unintentionally assigned a string value to `ShareDirectoryClient`. – Luke Woodward Oct 04 '21 at 21:10
  • @NukalaRaghavaAditya: nope, you can invoke them from an object instance but you can also invoke them from the class. See https://stackoverflow.com/q/136097 for example. – Luke Woodward Oct 04 '21 at 21:11
  • @Luke can you share the code you have used where you are not getting the error – user17058107 Oct 06 '21 at 00:06

2 Answers2

0

Sorry, I can't reproduce this.

I ran the following code:

from azure.storage.fileshare import ShareDirectoryClient

detailedFileURI = "Something"
shrdDirClient = ShareDirectoryClient.from_directory_url(detailedFileURI,snapshot=None, credential=None)

and it generated the following error:

Traceback (most recent call last):
  File "D:\StackOverflow\azure_storage_test.py", line 4, in <module>
    shrdDirClient = ShareDirectoryClient.from_directory_url(detailedFileURI,snapshot=None, credential=None)
  File "C:\Python37\lib\site-packages\azure\storage\fileshare\_directory_client.py", line 164, in from_directory_url
    credential=credential, **kwargs)
  File "C:\Python37\lib\site-packages\azure\storage\fileshare\_directory_client.py", line 96, in __init__
    raise ValueError("Please specify a share name.")
ValueError: Please specify a share name.

Now clearly this isn't successful execution. If I'm honest, I don't know what to set detailedFileURI to. But that's not the point. The point is that the code sample above is enough to prove that I can get in to the from_directory_url method, and this is clear from the traceback.

However, if I run the following code:

from azure.storage.fileshare import ShareDirectoryClient

ShareDirectoryClient = "some string"
detailedFileURI = "Something"
shrdDirClient = ShareDirectoryClient.from_directory_url(detailedFileURI,snapshot=None, credential=None)

then I do encounter the same error:

Traceback (most recent call last):
  File "D:\StackOverflow\azure_storage_test.py", line 5, in <module>
    shrdDirClient = ShareDirectoryClient.from_directory_url(detailedFileURI,snapshot=None, credential=None)
AttributeError: 'str' object has no attribute 'from_directory_url'

Of course, there's not a lot of point in importing ShareDirectoryClient if you're then going to assign a string to it. You may as well remove the import statement. However, doing this reproduces your error message. In the absence of any further code in your question I can only conclude that your code does the same as this, although perhaps more subtly.

The only other suggestion I have is that your installation of the azure-storage-file-share package has somehow got broken. If you run a Python script containing the following two lines only, you should get either <class 'type'> or <class 'str'> as output. I get <class 'type'>, and I would expect that anyone else using this package would get the same. However, if you get <class 'str'>, then it is likely that the package has got corrupted and you may want to try reinstalling it.

from azure.storage.fileshare import ShareDirectoryClient
print(type(ShareDirectoryClient))
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • I have added another approach of my code. Can u check that and tell. Nowhere I am assigning string values to ShareDirectoryClient. – user17058107 Oct 07 '21 at 09:14
  • @user17058107 as with your other code sample this new sample is incomplete. You haven't shown where you are importing (or defining) `generate_share_sas`, nor have you imported `timezone`. `accountName`, `accountKey` and `AccountSasPermissions` are similarly undefined, and `shareName` is only defined after it is used. It is clear that you simply cannot have run this code. If you want to be helped, you will need to include in your question code that someone can just copy straight from your question and run. The only changes you can make are to obscure secret values. – Luke Woodward Oct 07 '21 at 21:32
  • Thank you I have mentioned ShareDirectoryClient = "" in aother file which I imported – user17058107 Oct 12 '21 at 05:32
0

For some reason ‘ ShareDirectoryClient’ is of type {string}. You either import it as a string, i.e. ‘ azure.storage.fileshare’ simply has this defined as a string, or, you assign it to a string later in your code (not visible in the part that has been shared). Please try this: X = ‘some_string’ X() … and you will get the string-is-not-callable error which means a string cannot be invoked (i.e. it is not a function one can call)

Then another experiment: Y = ‘another_string’ Y.bla() … and you get the other error that a string object has no attribute named ‘bla’.

In Python everything is an object. If you define class MyClass() pass

And then try ‘MyClass.bla()’ you will get MyClass does does not have attribute ‘bla’.

Can you try to import ShareDirectoryClient and then type(ShareDirectoryClient) and we’ll see what type of object gets imported.

Kiril
  • 199
  • 2
  • 10