Skip to content

Commit 953bbfa

Browse files
authored
[Storage] az storage file upload-batch/download-batch: Add OAuth login support (#31567)
1 parent dea5e58 commit 953bbfa

File tree

2 files changed

+162
-3
lines changed

2 files changed

+162
-3
lines changed

src/azure-cli/azure/cli/command_modules/storage/commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,12 @@ def get_custom_sdk(custom_module, client_factory, resource_type=ResourceType.DAT
714714
g.storage_custom_command('copy start-batch', 'storage_file_copy_batch', client_factory=cf_share_client)
715715
g.storage_custom_command_oauth('upload', 'storage_file_upload',
716716
exception_handler=file_related_exception_handler)
717-
g.storage_custom_command('upload-batch', 'storage_file_upload_batch',
718-
custom_command_type=get_custom_sdk('file', client_factory=cf_share_client))
717+
g.storage_custom_command_oauth('upload-batch', 'storage_file_upload_batch',
718+
custom_command_type=get_custom_sdk('file', client_factory=cf_share_client))
719719
g.storage_custom_command_oauth('download', 'download_file',
720720
exception_handler=file_related_exception_handler,
721721
transform=transform_file_show_result)
722-
g.storage_custom_command('download-batch', 'storage_file_download_batch', client_factory=cf_share_client)
722+
g.storage_custom_command_oauth('download-batch', 'storage_file_download_batch', client_factory=cf_share_client)
723723
g.storage_command_oauth('hard-link create', 'create_hardlink')
724724

725725
with self.command_group('storage cors', get_custom_sdk('cors', multi_service_properties_factory)) as g:

src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_batch_operations.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,165 @@ def test_storage_blob_batch_sas_scenarios(self, test_dir, storage_account_info):
626626
self.storage_cmd('storage blob list -c {}', storage_account_info, container_name).assert_with_checks(
627627
JMESPathCheck('length(@)', 0))
628628

629+
@ResourceGroupPreparer()
630+
@StorageAccountPreparer()
631+
@StorageTestFilesPreparer()
632+
def test_storage_file_batch_download_scenarios_oauth(self, resource_group, test_dir, storage_account):
633+
account_info = self.get_account_info(resource_group, storage_account)
634+
src_share = self.create_share(account_info)
635+
# Prepare files
636+
snapshot = self.storage_cmd('storage share snapshot -n {} ',
637+
account_info, src_share).get_output_in_json()["snapshot"]
638+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --max-connections 3 --account-name {}',
639+
test_dir, src_share, storage_account)
640+
641+
# download without pattern
642+
local_folder = self.create_temp_dir()
643+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}', src_share, local_folder, storage_account)
644+
self.assertEqual(41, sum(len(f) for r, d, f in os.walk(local_folder)))
645+
646+
# download with pattern apple/*
647+
local_folder = self.create_temp_dir()
648+
share_url = self.file_oauth_cmd('storage file url -s {} -p \'\' -otsv --account-name {}',
649+
src_share, storage_account).output.strip()[:-1]
650+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --pattern apple/* --account-name {}',
651+
share_url, local_folder, storage_account)
652+
self.assertEqual(10, sum(len(f) for r, d, f in os.walk(local_folder)))
653+
654+
# download with pattern */file0
655+
local_folder = self.create_temp_dir()
656+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --pattern */file_0 --account-name {}',
657+
src_share, local_folder, storage_account)
658+
self.assertEqual(4, sum(len(f) for r, d, f in os.walk(local_folder)))
659+
660+
# download with pattern nonexsits/*
661+
local_folder = self.create_temp_dir()
662+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --pattern nonexists/* --account-name {}',
663+
src_share, local_folder, storage_account)
664+
self.assertEqual(0, sum(len(f) for r, d, f in os.walk(local_folder)))
665+
666+
# download with snapshot
667+
local_folder = self.create_temp_dir()
668+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --snapshot {} --account-name {}',
669+
src_share, local_folder, snapshot, storage_account)
670+
self.assertEqual(0, sum(len(f) for r, d, f in os.walk(local_folder)))
671+
672+
snapshot = self.storage_cmd('storage share snapshot -n {} ',
673+
account_info, src_share).get_output_in_json()["snapshot"]
674+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --snapshot {} --account-name {}',
675+
src_share, local_folder, snapshot, storage_account)
676+
self.assertEqual(41, sum(len(f) for r, d, f in os.walk(local_folder)))
677+
678+
local_folder = self.create_temp_dir()
679+
share_url = self.file_oauth_cmd('storage file url -s {} -p \'\' -otsv --account-name {}',
680+
src_share, storage_account).output.strip()[:-1]
681+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --pattern apple/* --snapshot {} --account-name {} ',
682+
share_url, local_folder, snapshot, storage_account)
683+
self.assertEqual(10, sum(len(f) for r, d, f in os.walk(local_folder)))
684+
685+
686+
@ResourceGroupPreparer()
687+
@StorageAccountPreparer()
688+
@StorageTestFilesPreparer()
689+
def test_storage_file_batch_upload_scenarios_oauth(self, resource_group, test_dir, storage_account):
690+
account_info = self.get_account_info(resource_group, storage_account)
691+
# upload without pattern
692+
src_share = self.create_share(account_info)
693+
local_folder = self.create_temp_dir()
694+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --max-connections 3 --account-name {}',
695+
test_dir, src_share, storage_account)
696+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}',
697+
src_share, local_folder, storage_account)
698+
self.assertEqual(41, sum(len(f) for r, d, f in os.walk(local_folder)))
699+
700+
# upload with pattern apple/*
701+
src_share = self.create_share(account_info)
702+
local_folder = self.create_temp_dir()
703+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern apple/* --account-name {}',
704+
test_dir, src_share, storage_account)
705+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}',
706+
src_share, local_folder, storage_account)
707+
self.assertEqual(10, sum(len(f) for r, d, f in os.walk(local_folder)))
708+
709+
# upload with pattern */file_0
710+
src_share = self.create_share(account_info)
711+
local_folder = self.create_temp_dir()
712+
share_url = self.file_oauth_cmd('storage file url -s {} -p \'\' -otsv --account-name {}'
713+
, src_share, storage_account).output.strip()[:-1]
714+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern */file_0 --account-name {}',
715+
test_dir, share_url, storage_account)
716+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}',
717+
src_share, local_folder, storage_account)
718+
self.assertEqual(4, sum(len(f) for r, d, f in os.walk(local_folder)))
719+
720+
# upload with pattern nonexists/*
721+
src_share = self.create_share(account_info)
722+
local_folder = self.create_temp_dir()
723+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern nonexists/* --account-name {}',
724+
test_dir, src_share, storage_account)
725+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}',
726+
src_share, local_folder, storage_account)
727+
self.assertEqual(0, sum(len(f) for r, d, f in os.walk(local_folder)))
728+
729+
# upload while specifying share path
730+
src_share = self.create_share(account_info)
731+
local_folder = self.create_temp_dir()
732+
share_url = self.file_oauth_cmd('storage file url -s {} -p \'\' -otsv --account-name {}',
733+
src_share, storage_account).output.strip()[:-1]
734+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern */file_0 --destination-path some_dir --account-name {}',
735+
test_dir, share_url, storage_account)
736+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --pattern some_dir* --account-name {}',
737+
src_share, local_folder, storage_account)
738+
self.assertEqual(4, sum(len(f) for r, d, f in os.walk(local_folder)))
739+
740+
# upload to specifying share path
741+
src_share = self.create_share(account_info)
742+
local_folder = self.create_temp_dir()
743+
sub_dir = 'test_dir/sub_dir'
744+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern */file_0 --destination-path {} --account-name {} ',
745+
test_dir, src_share, sub_dir, storage_account)
746+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}',
747+
src_share + "/" + sub_dir, local_folder, storage_account)
748+
self.assertEqual(4, sum(len(f) for r, d, f in os.walk(local_folder)))
749+
750+
# upload with content settings
751+
src_share = self.create_share(account_info)
752+
local_folder = self.create_temp_dir()
753+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --pattern apple/file_0 '
754+
'--content-cache-control no-cache '
755+
'--content-disposition attachment '
756+
'--content-encoding compress '
757+
'--content-language en-US '
758+
'--content-type "multipart/form-data;" '
759+
'--metadata key=val '
760+
'--account-name {}', test_dir, src_share, storage_account)
761+
self.file_oauth_cmd('storage file show -s {} -p "{}" --account-name {}', src_share, 'apple/file_0', storage_account). \
762+
assert_with_checks(JMESPathCheck('name', 'file_0'),
763+
JMESPathCheck('properties.contentSettings.cacheControl', 'no-cache'),
764+
JMESPathCheck('properties.contentSettings.contentDisposition', 'attachment'),
765+
JMESPathCheck('properties.contentSettings.contentEncoding', 'compress'),
766+
JMESPathCheck('properties.contentSettings.contentLanguage', 'en-US'),
767+
JMESPathCheck('properties.contentSettings.contentType', 'multipart/form-data;'),
768+
JMESPathCheck('metadata', {'key': 'val'}))
769+
770+
# parallel upload with max-connections
771+
import time
772+
start_time = time.time()
773+
src_share = self.create_share(account_info)
774+
local_folder = self.create_temp_dir()
775+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --max-connections 3 --account-name {}',
776+
test_dir, src_share, storage_account)
777+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}', src_share, local_folder, storage_account)
778+
self.assertEqual(41, sum(len(f) for _, _, f in os.walk(local_folder)))
779+
multi_thread_time = time.time() - start_time
780+
start_time = time.time()
781+
src_share = self.create_share(account_info)
782+
self.file_oauth_cmd('storage file upload-batch -s "{}" -d {} --max-connections 1 --account-name {}',
783+
test_dir, src_share, storage_account)
784+
self.file_oauth_cmd('storage file download-batch -s {} -d "{}" --account-name {}', src_share, local_folder, storage_account)
785+
self.assertEqual(41, sum(len(f) for _, _, f in os.walk(local_folder)))
786+
single_thread_time = time.time() - start_time
787+
self.assertGreater(single_thread_time, multi_thread_time)
629788

630789
if __name__ == '__main__':
631790
import unittest

0 commit comments

Comments
 (0)