@@ -1964,7 +1964,7 @@ class ABSStore(MutableMapping):
1964
1964
In order to use this store, you must install the Microsoft Azure Storage SDK for Python.
1965
1965
"""
1966
1966
1967
- def __init__ (self , container , prefix , account_name = None , account_key = None ,
1967
+ def __init__ (self , container , prefix = '' , account_name = None , account_key = None ,
1968
1968
blob_service_kwargs = None ):
1969
1969
from azure .storage .blob import BlockBlobService
1970
1970
self .container = container
@@ -1990,21 +1990,25 @@ def __setstate__(self, state):
1990
1990
self .client = BlockBlobService (self .account_name , self .account_key ,
1991
1991
** self .blob_service_kwargs )
1992
1992
1993
- @staticmethod
1994
- def _append_path_to_prefix (path , prefix ):
1995
- return '/' .join ([normalize_storage_path (prefix ),
1996
- normalize_storage_path (path )])
1993
+ def _append_path_to_prefix (self , path ):
1994
+ if self .prefix == '' :
1995
+ return normalize_storage_path (path )
1996
+ else :
1997
+ return '/' .join ([self .prefix , normalize_storage_path (path )])
1997
1998
1998
1999
@staticmethod
1999
2000
def _strip_prefix_from_path (path , prefix ):
2000
2001
# normalized things will not have any leading or trailing slashes
2001
2002
path_norm = normalize_storage_path (path )
2002
2003
prefix_norm = normalize_storage_path (prefix )
2003
- return path_norm [(len (prefix_norm )+ 1 ):]
2004
+ if prefix :
2005
+ return path_norm [(len (prefix_norm )+ 1 ):]
2006
+ else :
2007
+ return path_norm
2004
2008
2005
2009
def __getitem__ (self , key ):
2006
2010
from azure .common import AzureMissingResourceHttpError
2007
- blob_name = '/' . join ([ self .prefix , key ] )
2011
+ blob_name = self ._append_path_to_prefix ( key )
2008
2012
try :
2009
2013
blob = self .client .get_blob_to_bytes (self .container , blob_name )
2010
2014
return blob .content
@@ -2013,13 +2017,13 @@ def __getitem__(self, key):
2013
2017
2014
2018
def __setitem__ (self , key , value ):
2015
2019
value = ensure_bytes (value )
2016
- blob_name = '/' . join ([ self .prefix , key ] )
2020
+ blob_name = self ._append_path_to_prefix ( key )
2017
2021
self .client .create_blob_from_bytes (self .container , blob_name , value )
2018
2022
2019
2023
def __delitem__ (self , key ):
2020
2024
from azure .common import AzureMissingResourceHttpError
2021
2025
try :
2022
- self .client .delete_blob (self .container , '/' . join ([ self .prefix , key ] ))
2026
+ self .client .delete_blob (self .container , self ._append_path_to_prefix ( key ))
2023
2027
except AzureMissingResourceHttpError :
2024
2028
raise KeyError ('Blob %s not found' % key )
2025
2029
@@ -2034,53 +2038,62 @@ def keys(self):
2034
2038
return list (self .__iter__ ())
2035
2039
2036
2040
def __iter__ (self ):
2037
- for blob in self .client .list_blobs (self .container , self .prefix + '/' ):
2041
+ if self .prefix :
2042
+ list_blobs_prefix = self .prefix + '/'
2043
+ else :
2044
+ list_blobs_prefix = None
2045
+ for blob in self .client .list_blobs (self .container , list_blobs_prefix ):
2038
2046
yield self ._strip_prefix_from_path (blob .name , self .prefix )
2039
2047
2040
2048
def __len__ (self ):
2041
2049
return len (self .keys ())
2042
2050
2043
2051
def __contains__ (self , key ):
2044
- blob_name = '/' . join ([ self .prefix , key ] )
2052
+ blob_name = self ._append_path_to_prefix ( key )
2045
2053
if self .client .exists (self .container , blob_name ):
2046
2054
return True
2047
2055
else :
2048
2056
return False
2049
2057
2050
2058
def listdir (self , path = None ):
2051
- store_path = normalize_storage_path (path )
2052
- # prefix is normalized to not have a trailing slash
2053
- dir_path = self .prefix
2054
- if store_path :
2055
- dir_path = dir_path + '/' + store_path
2056
- dir_path += '/'
2059
+ from azure .storage .blob import Blob
2060
+ dir_path = normalize_storage_path (self ._append_path_to_prefix (path ))
2061
+ if dir_path :
2062
+ dir_path += '/'
2057
2063
items = list ()
2058
2064
for blob in self .client .list_blobs (self .container , prefix = dir_path , delimiter = '/' ):
2059
- if '/' in blob .name [len (dir_path ):]:
2065
+ if type (blob ) == Blob :
2066
+ items .append (self ._strip_prefix_from_path (blob .name , dir_path ))
2067
+ else :
2060
2068
items .append (self ._strip_prefix_from_path (
2061
2069
blob .name [:blob .name .find ('/' , len (dir_path ))], dir_path ))
2062
- else :
2063
- items .append (self ._strip_prefix_from_path (blob .name , dir_path ))
2064
2070
return items
2065
2071
2066
2072
def rmdir (self , path = None ):
2067
- dir_path = normalize_storage_path (self ._append_path_to_prefix (path , self .prefix )) + '/'
2073
+ dir_path = normalize_storage_path (self ._append_path_to_prefix (path ))
2074
+ if dir_path :
2075
+ dir_path += '/'
2068
2076
for blob in self .client .list_blobs (self .container , prefix = dir_path ):
2069
2077
self .client .delete_blob (self .container , blob .name )
2070
2078
2071
2079
def getsize (self , path = None ):
2080
+ from azure .storage .blob import Blob
2072
2081
store_path = normalize_storage_path (path )
2073
2082
fs_path = self .prefix
2074
2083
if store_path :
2075
- fs_path = self ._append_path_to_prefix (store_path , self . prefix )
2084
+ fs_path = self ._append_path_to_prefix (store_path )
2076
2085
if self .client .exists (self .container , fs_path ):
2077
2086
return self .client .get_blob_properties (self .container ,
2078
2087
fs_path ).properties .content_length
2079
2088
else :
2080
2089
size = 0
2081
- for blob in self .client .list_blobs (self .container , prefix = fs_path + '/' ,
2090
+ if fs_path == '' :
2091
+ fs_path = None
2092
+ else :
2093
+ fs_path += '/'
2094
+ for blob in self .client .list_blobs (self .container , prefix = fs_path ,
2082
2095
delimiter = '/' ):
2083
- if '/' not in blob . name [ len ( fs_path + '/' ):] :
2096
+ if type ( blob ) == Blob :
2084
2097
size += blob .properties .content_length
2085
2098
return size
2086
2099
0 commit comments