what's the simplest way to copy files from Azure CLI to Azure Windows VMs?

moon yi 60 Reputation points
2024-08-07T14:32:44.32+00:00

Hi

I was wondering if we can use scp to copy files from Azure CLI terminal to Azure Windows VMs. Note that I don't have public IP allocated to VMs. I need something easy for multiple copy of files from the Azure CLI to multiple VMs.

Cheers,

Moon

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,780 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Marcin Policht 21,595 Reputation points MVP
    2024-08-07T14:44:05.82+00:00

    You can directly copy files from the Azure VMs by accessing the storage account that provides persistent storage for Azure Cloud Shell.

    Step 1: Identify the Azure Cloud Shell Storage Account

    Open Azure Cloud Shell:

    • Go to the Azure portal and open the Cloud Shell.
      • If you have not set up persistent storage for Cloud Shell, it will prompt you to create one. Note the storage account and file share name used.

    Locate Storage Account and File Share:

    • In the Azure portal, navigate to the "Storage accounts" section.
    • Find the storage account associated with your Cloud Shell.
      • Within the storage account, navigate to "File shares" and locate the file share being used for Cloud Shell.

    Step 2: Access the Storage Account from Your VM

    1. Generate a Shared Access Signature (SAS) Token:
      • In the Azure portal, navigate to the storage account and select "Shared access signature" under the "Security + networking" section.
      • Generate a SAS token with appropriate permissions (read, write, list) and note the URL.
    2. Install AzCopy on Your Azure Windows VM:
      • If not already installed, download and install AzCopy on your VM.
    3. Download Files Using AzCopy:
      • Open a command prompt or PowerShell window on your Azure VM.
      • Use AzCopy to copy files from the Cloud Shell storage file share to the VM.
    # Example of copying a file from the Cloud Shell storage to the VM
    # AzCopy should be installed on your VM. You can download it from https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10
    # Variables
    $storageAccountName = "<your-storage-account-name>"
    $fileShareName = "<your-file-share-name>"
    $sasToken = "<your-sas-token>"
    $destinationPath = "C:\path\to\destination"
    # AzCopy command to copy files
    azcopy copy "https://$storageAccountName.file.core.windows.net/$fileShareName?<sas-token>" $destinationPath --recursive
    
    

    Step 3: Mount the Azure File Share Directly (Alternative Method)

    1. Mount the Azure File Share on the VM:
      • Instead of using AzCopy, you can directly mount the Azure File Share to the VM as a network drive.
    # Variables
    $storageAccountName = "<your-storage-account-name>"
    $fileShareName = "<your-file-share-name>"
    $sasToken = "<your-sas-token>"
    $driveLetter = "Z"
    # Mount the file share
    New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root "\\$storageAccountName.file.core.windows.net\$fileShareName" -Persist -Credential (New-Object PSCredential "Azure\$storageAccountName", (ConvertTo-SecureString $sasToken -AsPlainText -Force))
    
    
    1. Copy Files Using File Explorer or Command Line:
      • Once the file share is mounted, you can use File Explorer or command line tools (like copy or xcopy) to transfer files from the mounted drive to the local filesystem on the VM.

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


  2. Marcin Policht 21,595 Reputation points MVP
    2024-08-11T14:56:54.5433333+00:00

    Clarification:

    1. Step 2 and Step 3 Explanation:
      • Step 2: This step involves generating a Shared Access Signature (SAS) token from your Azure Cloud Shell storage account (which is your source) and then using AzCopy on your Azure Windows VM to copy files from this storage account to a local directory on the VM (which is your destination). The SAS token gives temporary access to the file share in Cloud Shell storage.
      • Step 3: This is an alternative method where instead of using AzCopy, you directly mount the Azure File Share (from your Azure Cloud Shell storage) as a network drive on your Azure Windows VM. Once mounted, you can copy files using File Explorer or command line tools like copy or xcopy.
    2. Understanding Storage Account and Destination Variables:
      • $storageAccountName: This is the name of your Azure Cloud Shell storage account (the source).
      • $fileShareName: This is the file share name within the Azure Cloud Shell storage account.
      • $sasToken: This is the SAS token generated for the Azure Cloud Shell storage account, allowing access.
      • $destinationPath: This is the local path on your Azure Windows VM (the destination) where you want to copy the files.
      • $driveLetter: This is the letter you choose to assign to the mounted Azure File Share on your VM.
    3. AzCopy Command and SAS Token:
      • In the AzCopy command, you should replace <sas-token> in the URL with the actual SAS token you generated. The token is usually long and should be added directly in the URL without altering it.
      • The $sasToken variable in the PowerShell script is just a placeholder for the token. When writing the actual command, ensure you insert the token correctly in the URL string.

    Revised Example for Step 2:

    Here’s the AzCopy command with example placeholders:

    # Example of copying a file from the Cloud Shell storage to the VM
    # Replace the placeholders with your actual values
    $storageAccountName = "<your-storage-account-name>"  # Cloud Shell storage account name
    $fileShareName = "<your-file-share-name>"            # Cloud Shell file share name
    $sasToken = "<your-sas-token>"                       # SAS token generated for Cloud Shell storage
    $destinationPath = "C:\path\to\destination"          # Local path on VM where files will be copied
    # AzCopy command to copy files
    azcopy copy "https://$storageAccountName.file.core.windows.net/$fileShareName?$sasToken" $destinationPath --recursive
    
    

    Note: Ensure that the sasToken value is URL-encoded and placed in the query string of the AzCopy command.

    Addressing the Error:

    The error message you received, OutOfRangeInput, indicates that the name of the resource (likely a file or directory in the file share) exceeds the allowable length in Azure Storage. Ensure that file paths and names are within Azure's character limits, and avoid using overly long directory structures or file names.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


  3. Nikhil Duserla 1,940 Reputation points Microsoft Vendor
    2024-08-20T09:49:00.2366667+00:00

    Hi moon yi,

    Thank you for sharing information.

    Based on the information provided, here are some additional suggestions to help resolve the issue:

    The error message indicates that the copy operation failed due to an authorization issue. Specifically, the request is not authorized to perform the operation, resulting in a 403-error code.

    Log in to AzCopy: Use the azcopy login command or attach a valid SAS token to your Azure URL. Authorize a user identity

    Verify Permissions: Ensure that the account or identity has the necessary permissions, such as the Storage Blob Data Contributor role, and that these permissions are correctly configured.

    Check Storage Account and Container: Confirm that the storage account and container specified in the AzCopy command are valid and exist in your Azure subscription. Double-check their names for accuracy.

    Inspect Network Connectivity: Network issues might be preventing AzCopy from connecting to the Azure storage account. Verify your network connection.

    Use Verbose Mode: Run AzCopy with the --verbose flag to obtain more detailed error messages for troubleshooting.

    If you have any further queries, do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.