Search code examples
azureazure-storageazure-blob-storage

Find if there is a zero bytes file in a blob storage


I have an Azure blob container with a big amount of files (millions). I need to find out if there is any file with a file size of 0 bytes. Is there an easy way of doing it?

In case it makes any difference, the container doesn't have any tags applied.


Solution

  • Just in case this may help somebody else, here's the script I wrote in powershell:

    $ctx = New-AzStorageContext -StorageAccountName <AccountName> -StorageAccountKey <PrimaryKey>
    $ContainerName = "containerName"
    $MaxReturn = 10000
    $loopCount = 0
    $Total = 0
    $Token = $Null
    
    do {
      $Blobs = Get-AzStorageBlob -Context $ctx -Container $ContainerName -MaxCount $MaxReturn -ContinuationToken $Token
    
      $Total += $Blobs.Count
    
      foreach ($blobitem in $Blobs) {
        $loopCount += 1
        Write-Host -NoNewline "`rValidating batch: " $loopCount "/" $blobsList.Length " (Current total:" $Total ")"
    
        if ($blobitem.Length -eq 0) {
          Write-Host ""
          Write-Host "Zero bytes file found: " $blobitem.Name
        }
      }
    
      $loopCount = 0
      if ($Blobs.Length -le 0) { Break; }
      $Token = $Blobs[$blobs.Count - 1].ContinuationToken;
    }
    While ($Token -ne $Null)
    
    Write-Host ""
    Write-Host "Finished!"