Search code examples
azurestorageaccount

How to copy local file to Azure storage (V2)


I would like to automate the process of copying a number of files from an on-premise server to an Azure Resource Manager Storage Account. I have done this with classic storage accounts, and there are many articles available for doing this task with the classic storage accounts, but all I can find for the V2 storage accounts is cmdlets and examples that deal with copying from one Azure storage account to another. Is it possible to do this, and if so, what cmdlet should I be using?


Solution

  • As far as managing data in a storage account is concerned (like uploading file), the process is the same for both V1 & V2 Storage accounts. Difference is how these storage accounts are managed (like fetching keys, regenerating keys etc.).

    The Cmdlet that you would want to use for uploading a file is Set-AzureStorageBlobContent. You would first create a AzureStorageContext using your storage account's name and key and then upload a file using this Cmdlet.

    From the Cmdlet help:

    Get-Help Set-AzureStorageBlobContent -Full
    
    NAME
        Set-AzureStorageBlobContent
    
    SYNOPSIS
        Upload local file to Azure Storage blob.
    
    SYNTAX
        Set-AzureStorageBlobContent [-File] <String> [-Container] <String> [-Blob <String>] [-BlobType <String>]
        [-Properties <Hashtable>] [-Metadata <Hashtable>] [-Force [<SwitchParameter>]] [-Context <AzureStorageContext>]
        [-ServerTimeoutPerRequest <Nullable`1[Int32]>] [-ClientTimeoutPerRequest <Nullable`1[Int32]>]
        [-ConcurrentTaskCount <Nullable`1[Int32]>] [-InformationAction <ActionPreference>] [-InformationVariable <String>]
        [<CommonParameters>]
    

    and here's a sample code snippet:

    $ctx = New-AzureStorageContext -StorageAccountName "account-name" -StorageAccountKey "account-key"
    Set-AzureStorageBlobContent -File "file-path" -Container "container-name" -Blob "blob-name" -Context $ctx