Skip to content

Commit

Permalink
{Storage} az storage copy: Add aka.ms backup links when downloading…
Browse files Browse the repository at this point in the history
… AzCopy (#30712)

* add a backup using aka.ms links when the static link in CLI fails, keeping the static links in case aka.ms is not working

* lint
  • Loading branch information
calvinhzy authored Jan 24, 2025
1 parent 7b4d01f commit 2da376c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/azure-cli/azure/cli/command_modules/storage/azcopy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,20 @@ def install_azcopy(self, install_location):
if self.system == 'Windows':
if platform.machine().endswith('64'):
file_url = base_url.format('windows', 'amd64', AZCOPY_VERSION, 'zip')
if _verify_url(file_url) is None:
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindowsX64')
else:
file_url = base_url.format('windows', '386', AZCOPY_VERSION, 'zip')
if _verify_url(file_url) is None:
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindows')
elif self.system == 'Linux':
file_url = base_url.format('linux', 'amd64', AZCOPY_VERSION, 'tar.gz')
if _verify_url(file_url) is None:
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLILinux')
elif self.system == 'Darwin':
file_url = base_url.format('darwin', 'amd64', AZCOPY_VERSION, 'zip')
if _verify_url(file_url) is None:
file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIDarwin')
else:
raise CLIError('Azcopy ({}) does not exist.'.format(self.system))
try:
Expand All @@ -74,8 +82,10 @@ def install_azcopy(self, install_location):
os.chmod(install_location,
os.stat(install_location).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
except OSError as err:
azcopy_install_guide = 'https://learn.microsoft.com/azure/storage/common/storage-use-azcopy-v10'
raise CLIError('Connection error while attempting to download azcopy {}. You could also install the '
'specified azcopy version to {} manually. ({})'.format(AZCOPY_VERSION, install_dir, err))
'specified azcopy version to {} manually following the guide here: {} '
'({})'.format(AZCOPY_VERSION, install_dir, azcopy_install_guide, err))

def check_version(self):
try:
Expand Down Expand Up @@ -218,6 +228,7 @@ def _get_default_install_location():

def _urlretrieve(url, install_location):
import io
logger.warning('Downloading AzCopy from %s', url)
req = urlopen(url)
compressedFile = io.BytesIO(req.read())
if url.endswith('zip'):
Expand All @@ -235,3 +246,15 @@ def _urlretrieve(url, install_location):
f.write(tar.extractfile(tarinfo).read())
else:
raise CLIError('Invalid downloading url {}'.format(url))


def _verify_url(url):
from urllib.error import HTTPError, URLError
try:
response = urlopen(url)
if response.code == 200:
return response.url
return None
except (HTTPError, URLError):
logger.warning('There is an error downloading from the url: %s', url)
return None

0 comments on commit 2da376c

Please sign in to comment.