Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AZCopy: Set the file Content-Type

Tags:

azure

azcopy

We are trying to use AZCopy in our deployment script to upload some assets directly into our storage which is exposed via a CDN on Azure.

When we upload the files, the Content-Type is application/octet-stream but we would need to be able to specify by example text/javascript

Is there a way to achieve this? We don't bother having to call AZCopy several times for each file types.

like image 349
Charles Ouellet Avatar asked Nov 12 '13 13:11

Charles Ouellet


People also ask

What is AzCopy command?

AzCopy is a command-line utility that you can use to copy blobs or files to or from a storage account. This article helps you download AzCopy, connect to your storage account, and then transfer data. AzCopy V10 is the currently supported version of AzCopy.

Can AzCopy move files?

AzCopy is a versatile command line utility that allows you to move files from another PC or Server into Azure Storage, and then into your Azure virtual machine. To get started, you need to install AzCopy from here.


3 Answers

The latest version of AzCopy (v3.1.0 and v4.1.0-preview) has included the support for setting content type, please download and find more details at http://aka.ms/azcopy.

To be more specific, you can set the content type via the option /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

If "Content-Type" is not specified at the /SetContentType option, AzCopy will set each blob’s content type according to its file extension. To set the same content type for all the blobs, you must explicitly specify a value for “Content-Type", for example, /SetContentType:video/mp4. Note that this option is only applicable when uploading blobs to the storage end points.

like image 96
Zhiming Yuan - Microsoft Avatar answered Oct 13 '22 11:10

Zhiming Yuan - Microsoft


I found a way of doing this using 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"'

Please let me know if you find a way of doing this using the Azure CLI or in my way but recursively (for all .js files).

like image 43
johnykes Avatar answered Oct 13 '22 11:10

johnykes


To add to the other answers here for anyone still finding this question, there is (now) an AzCopyConfig.json file where you can specify the MIME types for each file extension.

AzCopy determines content type of a blob based on a JSON file that stores content type to file extension mapping. This JSON file is named AzCopyConfig.json, and is located in the AzCopy directory. If you have a file type that is not in the list you can append the mapping to the JSON file:

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

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

like image 2
elkaz Avatar answered Oct 13 '22 10:10

elkaz