【问题标题】:AZCopy: Set the file Content-TypeAZCopy:设置文件 Content-Type
【发布时间】:2013-11-12 13:57:22
【问题描述】:

我们正在尝试在我们的部署脚本中使用AZCopy 将一些资产直接上传到我们的存储中,这些资产通过 Azure 上的 CDN 公开。

当我们上传文件时,Content-Type 是application/octet-stream,但我们需要能够通过示例指定text/javascript

有没有办法做到这一点?我们不必为每种文件类型多次调用 AZCopy。

【问题讨论】:

    标签: azure azcopy


    【解决方案1】:

    AzCopy 最新版本(v3.1.0 和 v4.1.0-preview)已支持设置内容类型,请在http://aka.ms/azcopy 下载并了解更多详细信息。

    更具体地说,您可以通过选项 /SetContentType:[content-type] 设置内容类型。

    AzCopy /Source:D:\test\ /Dest:https://myaccount.blob.core.windows.net/myContainer/ /DestKey:key /Pattern:ab /SetContentType

    AzCopy /Source:D:\test\ /Dest:https://myaccount.blob.core.windows.net/myContainer/ /DestKey:key /Pattern:ab /SetContentType:video/mp4

    如果在 /SetContentType 选项中未指定“Content-Type”,AzCopy 将根据其文件扩展名设置每个 blob 的内容类型。要为所有 blob 设置相同的内容类型,您必须明确指定“Content-Type”的值,例如 /SetContentType:video/mp4。 请注意,此选项仅在将 Blob 上传到存储端点时适用。

    【讨论】:

      【解决方案2】:

      我找到了一种使用 Azure Pipelines 的方法:

      
      - task: AzureFileCopy@4
        displayName: "Azure Storage - copy new files"
        inputs:
          SourcePath: '$(System.DefaultWorkingDirectory)/dist/*'
          azureSubscription: 'johnykes-PAYG(xxxxx-b455-4c4d-a8f8-c2a5fd479f10)'
          Destination: 'AzureBlob'
          storage: 'johnykeschatfrontend'
          ContainerName: '$web'
      
      - task: AzureFileCopy@4
        displayName: "Azure Storage - overwrite .js files with correct Content Type"
        inputs:
          SourcePath: '$(System.DefaultWorkingDirectory)/dist/*.js'
          azureSubscription: 'johnykes-PAYG(xxxxx-b455-4c4d-a8f8-c2a5fd479f10)'
          Destination: 'AzureBlob'
          storage: 'johnykeschatfrontend'
          ContainerName: '$web'
          AdditionalArgumentsForBlobCopy: '--content-type "application/javascript"'
      

      如果您找到了使用 Azure CLI 或以我的方式但递归地执行此操作的方法(适用于所有 .js 文件),请告诉我。

      【讨论】:

      【解决方案3】:

      要在此处为​​仍然发现此问题的任何人添加其他答案,(现在)有一个 AzCopyConfig.json 文件,您可以在其中为每个文件扩展名指定 MIME 类型。

      AzCopy 根据存储内容类型到文件扩展名映射的 JSON 文件确定 Blob 的内容类型。此 JSON 文件名为 AzCopyConfig.json,位于 AzCopy 目录中。如果您的文件类型不在列表中,您可以将映射附加到 JSON 文件:

      { "MIMETypeMapping": { ".myext": "text/mycustomtype", . . } }

      来源:https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy#automatically-determine-content-type-of-a-blob

      【讨论】:

      • 链接不再指向正确的部分,我再也找不到了。这还有可能吗?
      【解决方案4】:

      查看 AZCopy 中可用的选项,我认为这不是直接可行的。上传 Blob 后,您需要编写一些代码来设置 Blob 的内容类型。

      您可以做的是获取所需容器中的 blob 列表。 Blob 列表将为您提供每个 Blob 的当前内容类型。然后,您需要遍历此列表,从文件扩展名 (Get MIME type from filename extension) 中找出内容类型,然后使用更新的内容类型设置 blob 的属性。

      【讨论】:

      • 感谢 Gaurav,我们考虑过类似的解决方案,但希望使用 AZCopy 有更简单的方法,但我想我们会按照您的建议做!
      【解决方案5】:

      更新: 请注意,我的问题有点不同:使用 AzCopy 下载文件时,我需要重命名 blob 中的文件,因为它们没有扩展名。也许这个脚本可以颠倒来实现你的目标!

      我找不到使用 来完成这项工作的方法,因此我实现了一个连接到我的存储的 脚本,将项目下载到“下载”文件夹(位于同一脚本位置)中,然后通过添加扩展名来重命名它们。

      请注意,这种情况很容易处理,因为我知道每个文件只需要添加 .pdf 扩展名。 如果我能弄清楚,我会尝试改进这个脚本并包含一个更好的解决方案来处理不同的数据类型!

      1-必须下载Powershell 7.*

      2-通过更改ExecutionPolicyCurrentUserPowershell 安装Azure Modules 然后安装模块(可能需要一段时间,您可以添加-Verbose 参数以获取更多信息:

      • Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      • Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

      3-自定义此脚本并使用.ps1扩展名保存:

       
      ## Input Parameters  
      param  ($subscriptionId="", ## Your subscriptionId here, if not specified "Connect-AzAccount" will use the first available subscription.
              $resourceGroupName="your-resource-name",  
              $storageAccName="your-storage-account-name",
              $storageContainer="your-storage-container",
              $itemsCount=-1) ## Amount of items to download. If -1 (default value) or 0 it will download everything inside the container.
      
      $downloadPath=".\Download"
      $downloadLocation="Download"
       
      ## Connect to Azure Account and set the Context
      if ($subscriptionId -ne "")
      {
          Connect-AzAccount -Subscription $subscriptionId
      }
      else
      {
          Connect-AzAccount
      }
       
       
      Function BlobCycleAndRename
      {
          param([array]$Contents) 
          
          foreach($content in $Contents)
          {  
              $itemName=$content.Name
              ## Download the blob content  
              Get-AzStorageBlobContent -Container $container.Name  -Context $ctx -Blob $itemName -Destination $destination -Force  
              
              ## Rename the items, if an item with the same name already exists, it is removed first.
              if (Test-Path ($destination+"\"+($itemName+".your-extension-here")))
              {
                  Remove-Item -Path ($destination+"\"+($itemName+".your-extension-here"))
              }
              
              Rename-Item -Path ($destination+"\"+$itemName) -NewName ($itemName+".your-extension-here") -Force
          }  
          
      }
       
      ## Function to download all blob contents  
      Function DownloadBlobContents  
      {  
          Write-Host -ForegroundColor Green "Download blob contents from storage container.."    
              
          ## Get the storage account  
          $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName     
          ## Get the storage account context  
          $ctx=$storageAcc.Context  
          ## Get the containers 
          $container=Get-AzStorageContainer -Context $ctx -Name $storageContainer
      
          ## check if folder exists  
          $folderPath=$downloadPath+"\"+$container.Name  
          $destination=$downloadLocation+"\"+$container.Name  
          $folderExists=Test-Path -Path $folderPath  
          if($folderExists)  
          {  
              Write-Host -ForegroundColor Magenta $container.Name "- folder exists"  
              ## Get the blob contents from the container  
              $blobContent
              
              if ($itemsCount -le 0) 
              {
                  $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx
                  
              }
              else
              {
                  $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx -MaxCount $itemsCount
              }
      
              ## Download and rename blobs
              BlobCycleAndRename -Contents $blobContents 
               
          }  
          else  
          {        
              Write-Host -ForegroundColor Magenta $container.Name "- folder does not exist"  
              ## Create the new folder  
              New-Item -ItemType Directory -Path $folderPath  
              
              ## Get the blob contents from the container  
              $blobContents
              if ($itemsCount -le 0) 
              {
                  $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx
                  
              }
              else
              {
                  $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx -MaxCount $itemsCount
              }
                  
              ## Download and rename blobs
              BlobCycleAndRename -Contents $blobContents
       
          }     
      }   
        
      DownloadBlobContents  
       
      ## Disconnect from Azure Account  
      Disconnect-AzAccount  
      

      一些学分转到:https://www.c-sharpcorner.com/blogs/how-to-download-blob-contents-from-azure-storage-account-using-powershell


      以前的帖子 请注意,我的问题有点不同(它以某种方式“反转”了):从 AzCopy 下载文件时,我需要设置文件扩展名,因为它们上传时没有扩展名。

      我在这里回复@Kolky,但我没有足够的代表来直接添加 cmets!

      正如所指出的,MIMETypeMapping 页面似乎不再存在,但是我仍然能够找到一些有用的东西。

      您必须设置 AZCOPY_CONTENT_TYPE_MAP 环境变量并为其提供一个 json 配置文件,您可以在其中提供所需的映射类型。

            "MIMETypeMapping": {
              ".323": "text/h323",
              ".aaf": "application/octet-stream",
              ".aca": "application/octet-stream",
              ".accdb": "application/msaccess",
            }
      

      您可以使用命令 AzCopy env(或 /path/AzCopy.exe)访问已配置的 AzCopy 环境变量。 我仍在试图弄清楚我是否做错了什么,因为这似乎不起作用,但这仍然对某人有帮助! :)

      几个链接供参考:

      https://github.com/Azure/azure-storage-azcopy/wiki/Custom-mime-mapping

      https://docs.microsoft.com/it-it/azure/storage/common/storage-ref-azcopy-configuration-settings#AZCOPY_CONTENT_TYPE_MAP

      https://github.com/Azure/azure-storage-azcopy/issues/136

      【讨论】:

      • 嗨,有效果吗?
      • 您好!不幸的是,我找不到使用 AzCopy 进行这项工作的方法。我的解决方案是制作一个 PowerShell 脚本,并使用 Azure Modules for Powershell 我下载了这些项目并随后重命名。我正在编辑我的帖子以包含我的解决方案。请注意,这对我来说非常简单,因为我只需要添加“.pdf”扩展名。我会尝试弄清楚如何处理多种类型并尽快发布更完整的解决方案!当然,请随时改进并在这里分享! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2020-09-30
      • 2011-05-24
      • 2013-11-07
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多