Skip to content

Commit b30a2d5

Browse files
authored
[DataLake][Rename]Rename with Sas (#12057)
* [DataLake][Rename]Rename with Sas * small fix * recordings * fix pylint * fix pylint * fix pylint * Update sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/aio/_data_lake_directory_client_async.py
1 parent 94dc3bd commit b30a2d5

20 files changed

+770
-1572
lines changed

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/_data_lake_directory_client.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
7+
try:
8+
from urllib.parse import quote, unquote
9+
except ImportError:
10+
from urllib2 import quote, unquote # type: ignore
11+
612
from ._shared.base_client import parse_connection_str
713
from ._data_lake_file_client import DataLakeFileClient
814
from ._models import DirectoryProperties
@@ -300,16 +306,28 @@ def rename_directory(self, new_name, # type: str
300306
"""
301307
new_name = new_name.strip('/')
302308
new_file_system = new_name.split('/')[0]
303-
path = new_name[len(new_file_system):]
309+
new_path_and_token = new_name[len(new_file_system):].split('?')
310+
new_path = new_path_and_token[0]
311+
try:
312+
new_dir_sas = new_path_and_token[1] or self._query_str.strip('?')
313+
except IndexError:
314+
if not self._raw_credential and new_file_system != self.file_system_name:
315+
raise ValueError("please provide the sas token for the new file")
316+
if not self._raw_credential and new_file_system == self.file_system_name:
317+
new_dir_sas = self._query_str.strip('?')
304318

305319
new_directory_client = DataLakeDirectoryClient(
306-
self.url, new_file_system, directory_name=path, credential=self._raw_credential,
320+
"{}://{}".format(self.scheme, self.primary_hostname), new_file_system, directory_name=new_path,
321+
credential=self._raw_credential or new_dir_sas,
307322
_hosts=self._hosts, _configuration=self._config, _pipeline=self._pipeline,
308323
require_encryption=self.require_encryption,
309324
key_encryption_key=self.key_encryption_key,
310325
key_resolver_function=self.key_resolver_function)
311-
new_directory_client._rename_path('/'+self.file_system_name+'/'+self.path_name, # pylint: disable=protected-access
312-
**kwargs)
326+
new_directory_client._rename_path( # pylint: disable=protected-access
327+
'/{}/{}{}'.format(quote(unquote(self.file_system_name)),
328+
quote(unquote(self.path_name)),
329+
self._query_str),
330+
**kwargs)
313331
return new_directory_client
314332

315333
def create_sub_directory(self, sub_directory, # type: Union[DirectoryProperties, str]

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/_data_lake_file_client.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
from io import BytesIO
7+
8+
try:
9+
from urllib.parse import quote, unquote
10+
except ImportError:
11+
from urllib2 import quote, unquote # type: ignore
12+
713
import six
814

915
from ._quick_query_helper import DataLakeFileQueryReader
@@ -631,21 +637,35 @@ def rename_file(self, new_name, # type: str
631637
"""
632638
new_name = new_name.strip('/')
633639
new_file_system = new_name.split('/')[0]
634-
path = new_name[len(new_file_system):]
635-
636-
new_directory_client = DataLakeFileClient(
637-
self.url, new_file_system, file_path=path, credential=self._raw_credential,
640+
new_path_and_token = new_name[len(new_file_system):].split('?')
641+
new_path = new_path_and_token[0]
642+
try:
643+
new_file_sas = new_path_and_token[1] or self._query_str.strip('?')
644+
except IndexError:
645+
if not self._raw_credential and new_file_system != self.file_system_name:
646+
raise ValueError("please provide the sas token for the new file")
647+
if not self._raw_credential and new_file_system == self.file_system_name:
648+
new_file_sas = self._query_str.strip('?')
649+
650+
new_file_client = DataLakeFileClient(
651+
"{}://{}".format(self.scheme, self.primary_hostname), new_file_system, file_path=new_path,
652+
credential=self._raw_credential or new_file_sas,
638653
_hosts=self._hosts, _configuration=self._config, _pipeline=self._pipeline,
639654
_location_mode=self._location_mode, require_encryption=self.require_encryption,
640655
key_encryption_key=self.key_encryption_key,
641-
key_resolver_function=self.key_resolver_function)
642-
new_directory_client._rename_path('/'+self.file_system_name+'/'+self.path_name, # pylint: disable=protected-access
643-
**kwargs)
644-
return new_directory_client
656+
key_resolver_function=self.key_resolver_function
657+
)
658+
new_file_client._rename_path( # pylint: disable=protected-access
659+
'/{}/{}{}'.format(quote(unquote(self.file_system_name)),
660+
quote(unquote(self.path_name)),
661+
self._query_str),
662+
**kwargs)
663+
return new_file_client
645664

646665
def query_file(self, query_expression, **kwargs):
647666
# type: (str, **Any) -> DataLakeFileQueryReader
648-
"""Enables users to select/project on datalake file data by providing simple query expressions.
667+
"""
668+
Enables users to select/project on datalake file data by providing simple query expressions.
649669
This operations returns a DataLakeFileQueryReader, users need to use readall() or readinto() to get query data.
650670
651671
:param str query_expression:

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/_path_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# --------------------------------------------------------------------------
66

77
try:
8-
from urllib.parse import urlparse, quote, unquote
8+
from urllib.parse import urlparse, quote
99
except ImportError:
1010
from urlparse import urlparse # type: ignore
11-
from urllib2 import quote, unquote # type: ignore
11+
from urllib2 import quote # type: ignore
1212

1313
import six
1414

@@ -409,7 +409,7 @@ def _rename_path_options(self, rename_source, content_settings=None, metadata=No
409409
path_http_headers = get_path_http_headers(content_settings)
410410

411411
options = {
412-
'rename_source': quote(unquote(rename_source)),
412+
'rename_source': rename_source,
413413
'path_http_headers': path_http_headers,
414414
'lease_access_conditions': access_conditions,
415415
'source_lease_id': source_lease_id,

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/aio/_data_lake_directory_client_async.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
# pylint: disable=invalid-overridden-method
7-
7+
try:
8+
from urllib.parse import quote, unquote
9+
except ImportError:
10+
from urllib2 import quote, unquote # type: ignore
811
from ._data_lake_file_client_async import DataLakeFileClient
912
from .._data_lake_directory_client import DataLakeDirectoryClient as DataLakeDirectoryClientBase
1013
from .._models import DirectoryProperties
@@ -270,16 +273,28 @@ async def rename_directory(self, new_name, # type: str
270273
"""
271274
new_name = new_name.strip('/')
272275
new_file_system = new_name.split('/')[0]
273-
path = new_name[len(new_file_system):]
276+
new_path_and_token = new_name[len(new_file_system):].split('?')
277+
new_path = new_path_and_token[0]
278+
try:
279+
new_dir_sas = new_path_and_token[1] or self._query_str.strip('?')
280+
except IndexError:
281+
if not self._raw_credential and new_file_system != self.file_system_name:
282+
raise ValueError("please provide the sas token for the new directory")
283+
if not self._raw_credential and new_file_system == self.file_system_name:
284+
new_dir_sas = self._query_str.strip('?')
274285

275286
new_directory_client = DataLakeDirectoryClient(
276-
self.url, new_file_system, directory_name=path, credential=self._raw_credential,
287+
"{}://{}".format(self.scheme, self.primary_hostname), new_file_system, directory_name=new_path,
288+
credential=self._raw_credential or new_dir_sas,
277289
_hosts=self._hosts, _configuration=self._config, _pipeline=self._pipeline,
278290
_location_mode=self._location_mode, require_encryption=self.require_encryption,
279291
key_encryption_key=self.key_encryption_key,
280292
key_resolver_function=self.key_resolver_function)
281-
await new_directory_client._rename_path('/' + self.file_system_name + '/' + self.path_name, # pylint: disable=protected-access
282-
**kwargs)
293+
await new_directory_client._rename_path( # pylint: disable=protected-access
294+
'/{}/{}{}'.format(quote(unquote(self.file_system_name)),
295+
quote(unquote(self.path_name)),
296+
self._query_str),
297+
**kwargs)
283298
return new_directory_client
284299

285300
async def create_sub_directory(self, sub_directory, # type: Union[DirectoryProperties, str]

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/aio/_data_lake_file_client_async.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
# --------------------------------------------------------------------------
66
# pylint: disable=invalid-overridden-method
77

8+
try:
9+
from urllib.parse import quote, unquote
10+
except ImportError:
11+
from urllib2 import quote, unquote # type: ignore
12+
813
from ._download_async import StorageStreamDownloader
914
from ._path_client_async import PathClient
1015
from .._data_lake_file_client import DataLakeFileClient as DataLakeFileClientBase
@@ -500,14 +505,26 @@ async def rename_file(self, new_name, # type: str
500505
"""
501506
new_name = new_name.strip('/')
502507
new_file_system = new_name.split('/')[0]
503-
path = new_name[len(new_file_system):]
504-
505-
new_directory_client = DataLakeFileClient(
506-
self.url, new_file_system, file_path=path, credential=self._raw_credential,
508+
new_path_and_token = new_name[len(new_file_system):].split('?')
509+
new_path = new_path_and_token[0]
510+
try:
511+
new_file_sas = new_path_and_token[1] or self._query_str.strip('?')
512+
except IndexError:
513+
if not self._raw_credential and new_file_system != self.file_system_name:
514+
raise ValueError("please provide the sas token for the new file")
515+
if not self._raw_credential and new_file_system == self.file_system_name:
516+
new_file_sas = self._query_str.strip('?')
517+
518+
new_file_client = DataLakeFileClient(
519+
"{}://{}".format(self.scheme, self.primary_hostname), new_file_system, file_path=new_path,
520+
credential=self._raw_credential or new_file_sas,
507521
_hosts=self._hosts, _configuration=self._config, _pipeline=self._pipeline,
508522
_location_mode=self._location_mode, require_encryption=self.require_encryption,
509523
key_encryption_key=self.key_encryption_key,
510524
key_resolver_function=self.key_resolver_function)
511-
await new_directory_client._rename_path('/' + self.file_system_name + '/' + self.path_name, # pylint: disable=protected-access
512-
**kwargs)
513-
return new_directory_client
525+
await new_file_client._rename_path( # pylint: disable=protected-access
526+
'/{}/{}{}'.format(quote(unquote(self.file_system_name)),
527+
quote(unquote(self.path_name)),
528+
self._query_str),
529+
**kwargs)
530+
return new_file_client

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/aio/_path_client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(
3535
**kwargs)
3636

3737
kwargs.pop('_hosts', None)
38-
self._blob_client = BlobClient(self._blob_account_url, file_system_name, blob_name=path_name,
38+
self._blob_client = BlobClient(self._blob_account_url, file_system_name, blob_name=self.path_name,
3939
credential=credential, _hosts=self._blob_client._hosts, **kwargs) # type: ignore # pylint: disable=protected-access
4040
self._client = DataLakeStorageClient(self.url, file_system_name, path_name, pipeline=self._pipeline)
4141
self._loop = kwargs.get('loop', None)

0 commit comments

Comments
 (0)