简体   繁体   中英

az storage blob download not downloading file when run in Azure Pipelines

I have a powershell script that is meant to query an Azure Storage Container for the newest blob (by creation date) and then subsequently download it to a local folder. I can run this script locally and it performs as expected downloading the file with a new file name "test.yml".

function Get-BlobList
{
    return az storage blob list `
        --account-name $accountName `
        --account-key $key `
        --container-name $containerName | `
        ConvertFrom-Json
}

$files = `
    $(Get-BlobList) | `
    Sort-Object -Descending { $_.properties.CreationTime } | `
    Select-Object name

az storage blob download `
    --account-name $accountName `
    --account-key $key `
    --container-name $containerName `
    --name $($files[0].name) `
    --file "$downloadFolder\test.yml"

However when I run this script in an Azure DevOps pipeline against the same Azure Storage Container, for whatever reason the file is not downloading. No errors are thrown but the output from the local run and the pipelines run are different.

Local run (working and downloads file as expected):

{
  "content": null,
  "deleted": false,
  "metadata": {},
  "name": "20230127.yml",
  "properties": {
    "appendBlobCommittedBlockCount": null,
    "blobTier": null,
    "blobTierChangeTime": null,
    "blobTierInferred": false,
    "blobType": "BlockBlob",
    "contentLength": 247,
    "contentRange": "bytes 0-246/247",
    "contentSettings": {
      "cacheControl": null,
      "contentDisposition": null,
      "contentEncoding": null,
      "contentLanguage": null,
      "contentMd5": "uGLe+ORvBsW6k6il8FekhQ==",
      "contentType": "application/octet-stream"
    },
    "copy": {
      "completionTime": null,
      "id": null,
      "progress": null,
      "source": null,
      "status": null,
      "statusDescription": null
    },
    "creationTime": "2023-01-27T23:48:51+00:00",
    "deletedTime": null,
    "etag": "\"0x8DB00C10ABD11C7\"",
    "lastModified": "2023-01-27T23:48:51+00:00",
    "lease": {
      "duration": null,
      "state": "available",
      "status": "unlocked"
    },
    "pageBlobSequenceNumber": null,
    "remainingRetentionDays": null,
    "serverEncrypted": true
  },
  "snapshot": null
}

Pipeline run (not downloading but also no errors):

{
  "container": "manifests",
  "content": "",
  "contentMd5": null,
  "deleted": false,
  "encryptedMetadata": null,
  "encryptionKeySha256": null,
  "encryptionScope": null,
  "hasLegalHold": null,
  "hasVersionsOnly": null,
  "immutabilityPolicy": {
    "expiryTime": null,
    "policyMode": null
  },
  "isAppendBlobSealed": null,
  "isCurrentVersion": null,
  "lastAccessedOn": null,
  "metadata": {},
  "name": "20230127.yml",
  "objectReplicationDestinationPolicy": null,
  "objectReplicationSourceProperties": [],
  "properties": {
    "appendBlobCommittedBlockCount": null,
    "blobTier": null,
    "blobTierChangeTime": null,
    "blobTierInferred": null,
    "blobType": "BlockBlob",
    "contentLength": 247,
    "contentRange": "bytes None-None/247",
    "contentSettings": {
      "cacheControl": null,
      "contentDisposition": null,
      "contentEncoding": null,
      "contentLanguage": null,
      "contentMd5": "uGLe+ORvBsW6k6il8FekhQ==",
      "contentType": "application/octet-stream"
    },
    "copy": {
      "completionTime": null,
      "destinationSnapshot": null,
      "id": null,
      "incrementalCopy": null,
      "progress": null,
      "source": null,
      "status": null,
      "statusDescription": null
    },
    "creationTime": null,
    "deletedTime": null,
    "etag": "\"0x8DB00C10ABD11C7\"",
    "lastModified": "2023-01-27T23:48:51+00:00",
    "lease": {
      "duration": null,
      "state": "available",
      "status": "unlocked"
    },
    "pageBlobSequenceNumber": null,
    "pageRanges": null,
    "rehydrationStatus": null,
    "remainingRetentionDays": null,
    "serverEncrypted": true
  },
  "rehydratePriority": null,
  "requestServerEncrypted": true,
  "snapshot": null,
  "tagCount": null,
  "tags": null,
  "versionId": null
}

Noticeably the "creationTime" is null on the pipeline run and the "contentRange" says "bytes None-None/247"

Is there something I am missing when running this command in a pipeline?

You can try to execute az storage blob download command in Azure CLI task to download files from Azure Blob Storage like below:

- task: AzureCLI@2
  displayName: 'Azure CLI '
  inputs:
    azureSubscription: {your service connection name}
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
     mkdir $(Build.SourcesDirectory)/BlobFile
     az storage blob download --container-name $(containername) --file $(Build.SourcesDirectory)/BlobFile --name "{file name}" --account-key $(accountkey) --account-name $(accountname)

Use mkdir to create a folder in current directory, then download file from blob and save it into this folder. And you can create a service connection to connect to your Azure blob first and then select it in this Azure CLI task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM