Skip to content

Commit 426b804

Browse files
authored
[Storage] Fix #32852: az storage copy: Fix Azcopy download link to be using github release links (#32868)
1 parent 5ae35f4 commit 426b804

File tree

1 file changed

+41
-30
lines changed
  • src/azure-cli/azure/cli/command_modules/storage/azcopy

1 file changed

+41
-30
lines changed

src/azure-cli/azure/cli/command_modules/storage/azcopy/util.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import platform
88
import subprocess
99
import datetime
10-
import zipfile
1110
import stat
1211
from urllib.parse import urlparse
1312
from urllib.request import urlopen
@@ -56,36 +55,41 @@ def install_azcopy(self, install_location):
5655
install_dir = os.path.dirname(install_location)
5756
if not os.path.exists(install_dir):
5857
os.makedirs(install_dir)
59-
base_url = 'https://azcopyvnext-awgzd8g7aagqhzhe.b02.azurefd.net/release20211027/azcopy_{}_{}_{}.{}'
58+
file_extension = 'zip'
6059
if self.system == 'Windows':
6160
if platform.machine().endswith('64'):
62-
file_url = base_url.format('windows', 'amd64', AZCOPY_VERSION, 'zip')
61+
file_url = 'https://aka.ms/downloadazcopy-v10-windows'
6362
if _verify_url(file_url) is None:
6463
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindowsX64')
6564
else:
66-
file_url = base_url.format('windows', '386', AZCOPY_VERSION, 'zip')
65+
file_url = 'https://aka.ms/downloadazcopy-v10-windows-32bit'
6766
if _verify_url(file_url) is None:
6867
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindows')
6968
elif self.system == 'Linux':
70-
file_url = base_url.format('linux', 'amd64', AZCOPY_VERSION, 'tar.gz')
69+
file_extension = 'tar.gz'
70+
file_url = 'https://aka.ms/downloadazcopy-v10-linux'
7171
if _verify_url(file_url) is None:
7272
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLILinux')
7373
elif self.system == 'Darwin':
74-
file_url = base_url.format('darwin', 'amd64', AZCOPY_VERSION, 'zip')
74+
file_url = 'https://aka.ms/downloadazcopy-v10-mac'
7575
if _verify_url(file_url) is None:
7676
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIDarwin')
7777
else:
78-
raise CLIError('Azcopy ({}) does not exist.'.format(self.system))
78+
raise CLIError('Azcopy executable does not exist for {}.'.format(self.system))
79+
azcopy_install_guide = 'https://learn.microsoft.com/azure/storage/common/storage-use-azcopy-v10'
80+
if not file_url:
81+
raise CLIError('Error while attempting to download azcopy. You could manually install the azcopy '
82+
'executable to {} by following the guide here: {}'.format(install_dir,
83+
azcopy_install_guide))
7984
try:
8085
os.chmod(install_dir, os.stat(install_dir).st_mode | stat.S_IWUSR)
81-
_urlretrieve(file_url, install_location)
86+
_urlretrieve(file_url, install_location, file_extension)
8287
os.chmod(install_location,
8388
os.stat(install_location).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
84-
except OSError as err:
85-
azcopy_install_guide = 'https://learn.microsoft.com/azure/storage/common/storage-use-azcopy-v10'
86-
raise CLIError('Connection error while attempting to download azcopy {}. You could also install the '
87-
'specified azcopy version to {} manually following the guide here: {} '
88-
'({})'.format(AZCOPY_VERSION, install_dir, azcopy_install_guide, err))
89+
except (OSError, CLIError) as err:
90+
raise CLIError('Error while attempting to download azcopy from {}. You could manually install the azcopy '
91+
'executable to {} by following the guide here: {} '
92+
'({})'.format(file_url, install_dir, azcopy_install_guide, err))
8993

9094
def check_version(self):
9195
try:
@@ -226,34 +230,41 @@ def _get_default_install_location():
226230
return install_location
227231

228232

229-
def _urlretrieve(url, install_location):
233+
def _urlretrieve(url, install_location, file_extension):
230234
import io
231235
logger.warning('Downloading AzCopy from %s', url)
232-
req = urlopen(url)
233-
compressedFile = io.BytesIO(req.read())
234-
if url.endswith('zip'):
235-
zip_file = zipfile.ZipFile(compressedFile)
236-
for fileName in zip_file.namelist():
237-
if fileName.endswith('azcopy') or fileName.endswith('azcopy.exe'):
238-
with open(install_location, 'wb') as f:
239-
f.write(zip_file.read(fileName))
240-
elif url.endswith('gz'):
241-
import tarfile
242-
with tarfile.open(fileobj=compressedFile, mode="r:gz") as tar:
243-
for tarinfo in tar:
244-
if tarinfo.isfile() and tarinfo.name.endswith('azcopy'):
236+
res = urlopen(url)
237+
if res.status != 200:
238+
raise CLIError('Invalid downloading url {}'.format(url))
239+
compressedFile = io.BytesIO(res.read())
240+
if file_extension == 'zip':
241+
try:
242+
import zipfile
243+
zip_file = zipfile.ZipFile(compressedFile)
244+
for fileName in zip_file.namelist():
245+
if fileName.endswith('azcopy') or fileName.endswith('azcopy.exe'):
245246
with open(install_location, 'wb') as f:
246-
f.write(tar.extractfile(tarinfo).read())
247+
f.write(zip_file.read(fileName))
248+
except Exception as ex: # pylint: disable=broad-except
249+
raise CLIError(ex)
247250
else:
248-
raise CLIError('Invalid downloading url {}'.format(url))
251+
try:
252+
import tarfile
253+
with tarfile.open(fileobj=compressedFile, mode="r:gz") as tar:
254+
for tarinfo in tar:
255+
if tarinfo.isfile() and tarinfo.name.endswith('azcopy'):
256+
with open(install_location, 'wb') as f:
257+
f.write(tar.extractfile(tarinfo).read())
258+
except Exception as ex: # pylint: disable=broad-except
259+
raise CLIError(ex)
249260

250261

251262
def _verify_url(url):
252263
from urllib.error import HTTPError, URLError
253264
try:
254265
response = urlopen(url)
255266
if response.code == 200:
256-
return response.url
267+
return url
257268
return None
258269
except (HTTPError, URLError):
259270
logger.warning('There is an error downloading from the url: %s', url)

0 commit comments

Comments
 (0)