简体   繁体   English

az storage blob download 在 Azure 管道中运行时不下载文件

[英]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.我有一个 powershell 脚本,用于查询 Azure 存储容器以获取最新的 blob(按创建日期),然后将其下载到本地文件夹。 I can run this script locally and it performs as expected downloading the file with a new file name "test.yml".我可以在本地运行这个脚本,它会按预期执行,下载文件名为“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.但是,当我在 Azure DevOps 管道中针对同一个 Azure 存储容器运行此脚本时,无论出于何种原因,文件都没有下载。 No errors are thrown but the output from the local run and the pipelines run are different.不会抛出任何错误,但本地运行的 output 与管道运行不同。

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"值得注意的是,管道运行中的“creationTime”为 null,“contentRange”表示“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:您可以尝试在Azure CLI 任务中执行az storage blob download 命令,以从 Azure Blob 存储下载文件,如下所示:

- 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.使用mkdir在当前目录中创建一个文件夹,然后从 blob 下载文件并将其保存到该文件夹中。 And you can create a service connection to connect to your Azure blob first and then select it in this Azure CLI task.您可以创建一个服务连接,首先连接到您的 Azure blob,然后在此 Azure CLI 任务中连接到 select。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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