How Quickly Upload Files from URL to Azure Blob Storage Using Azure CLI in 2 steps

contenu

The Azure CLI's az storage blob upload command lets you upload local files to Azure Blob Storage. However, sometimes it’s more convenient to upload a file directly from an HTTP(S) URL to Azure Blob Storage. Unfortunately, the Azure CLI doesn’t support this directly—you need to download the file locally first. This article explores using Bash and PowerShell commands to download a file from a URL and then upload it to Azure Blob Storage. Plus, if you use these commands in the Azure Cloud Shell, you won’t have to worry about your local network’s bandwidth speeds or limits, which is great for uploading large files.

Traduction en cours...

How to Upload Files from a URL to Azure Blob Storage?

Traduction en cours...

Let's start by looking at how to upload a file to Azure Blob Storage using the Azure CLI. In this scenario, we have a file hosted online, accessible via an HTTP(S) URL. Our goal is to take this URL and upload the file to Azure Blob Storage.

Traduction en cours...

1. Download the File(s)

Traduction en cours...

Before uploading the file to Azure Blob Storage, we need to download it. You can do this using `wget` or `curl` in bash, or Invoke-WebRequest in PowerShell.

Traduction en cours...

Invoke-WebRequest -Uri "https://example.com/documents/files.zip" -OutFile "docfiles.zip"

Traduction en cours...

2. Uploading the File to Azure Blob Storage

Traduction en cours...

With the file downloaded, we can now upload it to Azure Blob Storage using the Azure CLI's az storage blob upload command.

Traduction en cours...

az storage blob upload --account-name \ --account-key \ --container-name \ --name \ --file "docfiles.zip"

Traduction en cours...

Make sure to set the command arguments correctly for your Azure Blob Storage account:

Traduction en cours...
  • --account-name – name of your Azure Storage account
  • --account-key – key for your Azure Storage account, used for authentication
  • --container-name – name of the container within your Azure Storage account where the blob will be uploaded
  • --name – name you want to give the blob, which can be different from the local file name
  • --file – path and name of the local file you want to upload to blob storage.
Traduction en cours...

2.1 Delete the File

Traduction en cours...

After uploading the downloaded file to Azure Blob Storage, you can delete the local copy from your machine (or from Azure Cloud Shell if you used it).

Traduction en cours...

Remove-Item -Path "docfiles.zip"

  • or - BASH: rm "docfiles.zip"
Traduction en cours...

3. Bash/Powershell scripts to automate file upload from HTTP(S) URL to Azure Blob Storage

Traduction en cours...
Traduction en cours...

Conclusion

Traduction en cours...

In this article, we explored how to upload a file with a known HTTP(S) URL to an Azure Blob Storage container. Although the Azure CLI can’t directly upload from a URL, you can first download the file locally and then use the CLI to upload it to Azure Blob Storage. Additionally, running these commands in Azure Cloud Shell saves your local network bandwidth.

Traduction en cours...