Skip to content

Commit 818a0aa

Browse files
jonie001evelyn-ys
andauthored
[Storage] az storage blob download: Allow downloading to stdout for pipe support (Azure#22317)
* fix blob download stdout * fix check * Update src/azure-cli/azure/cli/command_modules/storage/operations/blob.py Co-authored-by: Yishi Wang <[email protected]> Co-authored-by: Yishi Wang <[email protected]>
1 parent 11527e5 commit 818a0aa

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,8 @@
11931193
examples:
11941194
- name: Download a blob.
11951195
text: az storage blob download -f /path/to/file -c mycontainer -n MyBlob
1196+
- name: Download a blob content to stdout(pipe support).
1197+
text: az storage blob download -c mycontainer -n myblob --account-name mystorageaccount --account-key myaccountkey
11961198
"""
11971199

11981200
helps['storage blob url'] = """

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,8 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
10081008
c.register_blob_arguments_track2()
10091009
c.register_precondition_options()
10101010
c.argument('file_path', options_list=('--file', '-f'), type=file_type, completer=FilesCompleter(),
1011-
help='Path of file to write out to.', validator=blob_download_file_path_validator)
1011+
help='Path of file to write out to. If not specified, stdout will be used '
1012+
'and max_connections will be set to 1.', validator=blob_download_file_path_validator)
10121013
c.argument('start_range', type=int,
10131014
help='Start of byte range to use for downloading a section of the blob. If no end_range is given, '
10141015
'all bytes after the start_range will be downloaded. The start_range and end_range params are '

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

+2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ def transform_blob_list_output(result):
218218

219219

220220
def transform_blob_json_output(result):
221+
if result is None:
222+
return
221223
result = todict(result)
222224
new_result = {
223225
"content": "",

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

+2
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,8 @@ def blob_tier_validator_track2(cmd, namespace):
14451445

14461446

14471447
def blob_download_file_path_validator(namespace):
1448+
if namespace.file_path is None:
1449+
return
14481450
if os.path.isdir(namespace.file_path):
14491451
from azure.cli.core.azclierror import FileOperationError
14501452
raise FileOperationError('File is expected, not a directory: {}'.format(namespace.file_path))

src/azure-cli/azure/cli/command_modules/storage/operations/blob.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# --------------------------------------------------------------------------------------------
55

66
import os
7+
import sys
78
from datetime import datetime
89

910
from azure.cli.core.profiles import ResourceType, get_sdk
@@ -529,7 +530,6 @@ def transform_blob_type(cmd, blob_type):
529530
def _adjust_block_blob_size(client, blob_type, length):
530531
if not blob_type or blob_type != 'block' or length is None:
531532
return
532-
533533
# increase the block size to 100MB when the block list will contain more than 50,000 blocks(each block 4MB)
534534
if length > 50000 * 4 * 1024 * 1024:
535535
client._config.max_block_size = 100 * 1024 * 1024
@@ -623,7 +623,7 @@ def upload_blob(cmd, client, file_path=None, container_name=None, blob_name=None
623623
return response
624624

625625

626-
def download_blob(client, file_path, open_mode='wb', start_range=None, end_range=None,
626+
def download_blob(client, file_path=None, open_mode='wb', start_range=None, end_range=None,
627627
progress_callback=None, **kwargs):
628628
offset = None
629629
length = None
@@ -632,11 +632,16 @@ def download_blob(client, file_path, open_mode='wb', start_range=None, end_range
632632
length = end_range - start_range + 1
633633
if progress_callback:
634634
kwargs['raw_response_hook'] = progress_callback
635+
if not file_path:
636+
kwargs['max_concurrency'] = 1
635637
download_stream = client.download_blob(offset=offset, length=length, **kwargs)
636-
with open(file_path, open_mode) as stream:
638+
if file_path:
639+
with open(file_path, open_mode) as stream:
640+
download_stream.readinto(stream)
641+
return download_stream.properties
642+
with os.fdopen(sys.stdout.fileno(), open_mode) as stream:
637643
download_stream.readinto(stream)
638-
639-
return download_stream.properties
644+
return
640645

641646

642647
def get_block_ids(content_length, block_length):

0 commit comments

Comments
 (0)