DEV Community

Hamed0406
Hamed0406

Posted on

Effortlessly Retrieve Data from Azure Blob Storage with PowerShell

Why is a PowerShell script beneficial for interacting with Azure Blobs?

  • Streamlines processes by leveraging the Azure REST API for automation.
  • Seamlessly integrates with Azure Functions for enhanced cloud functionality.
  • Utilizes standard HTTP/HTTPS protocols for secure and reliable downloads from Azure Blob Storage.

here is script

Link to the repo

This PowerShell script is designed to interact with Azure Blob Storage by retrieving a list of blob items from a given storage container and then, optionally, downloading those items to a local directory. The script is divided into two main functions: Get-BlobItems and Invoke-BlobItems. Both functions require a URL to the Blob Storage container as a parameter, which must include a Shared Access Signature (SAS) token for authentication.

Function 1: Get-BlobItems

The Get-BlobItems function's primary purpose is to list all the blob items within a specified Azure Blob Storage container.

1.Splitting the URL: It begins by splitting the input URL into two parts: the base URI and the SAS token. This is necessary because the SAS token is used for authenticated access, and the base URI identifies the storage container.

2.Creating a New URL for the API Request: It then constructs a new URL that includes the base URI, a query to list the blobs (restype=container&comp=list), and the SAS token. This URL is used to make a REST API call to Azure Blob Storage.

**3.Invoking the REST API: **The script uses Invoke-RestMethod to call the Azure REST API with the newly constructed URL. The response is expected to be in XML format, listing all blobs within the container.

4.Processing the XML Response: The XML response is parsed to extract the names of the blob items. These names are relative paths to the blobs within the storage container.

5.Download URLs: For each blob item, a download URL is generated by combining the base URI, the blob item's name, and the SAS token. These URLs can be used to download the blob items directly.

Function 2: Invoke-BlobItems

The Invoke-BlobItems function extends the capabilities of Get-BlobItems by not only listing the blob items but also downloading them to a specified local path.

1.URL Splitting and API Request URL Construction: This part is identical to the Get-BlobItems function, where the function splits the input URL and constructs a new URL to invoke the Azure REST API for listing blobs.

2.XML Response Handling and Blob Listing: The response from the Azure REST API is processed in the same way, extracting the names of the blob items from the XML.

3.Downloading Blob Items: For each blob item listed in the XML response, the function checks if the local directory structure exists (based on the blob's relative path) and creates it if necessary. It then uses System.Net.WebClient to download each blob item to the specified local directory, constructing the full download URL for each item using the base URI, blob name, and SAS token.

Illustrative Diagram

To better understand the flow and components of the script, let's visualize the process with a diagram:

Image description

1.Input: The user provides a URL containing the base URI to the Blob Storage container and a SAS token for authentication.
2.URL Processing: The script splits the URL to extract the base URI and SAS token, then constructs a new URL for the REST API call.
3.API Call: A REST API request is made to Azure Blob Storage to list the blobs in the container.
4.Response Handling: The XML response is parsed to extract the names of the blobs.
5.Download URL Generation: For each blob, a download URL is generated.
6.File Download: In the Invoke-BlobItems function, the blobs are downloaded to a specified local directory.
The user inputs a URL with a SAS token.
The script processes this URL, separating the base URI and the SAS token.
A REST API call is made to list the blobs in the container.
The blobs are listed and optionally downloaded to a local directory.

Happy coding , until next time !

Image description

Top comments (0)