如何将Azure Web App文件目录挂载至虚拟机?
Hey there! Let me break down how to get your Azure Web App's files accessible on a virtual machine. Since Web Apps run in a sandboxed environment, you can't directly mount their local storage to a VM—but don't worry, we can use Azure's managed storage services to bridge the gap. Here are two reliable methods to do this:
Method 1: Use Azure Files (Recommended for SMB-Compatible Access)
This is the most straightforward approach because Azure Files supports the SMB protocol, which both Web Apps and VMs can natively access and mount.
Step 1: Create an Azure File Share
- Head to the Azure Portal, navigate to Storage Accounts, and create a new storage account (or use an existing one that fits your needs).
- Inside the storage account, go to File shares > + File share. Give it a descriptive name, set a storage quota, and click Create.
Step 2: Link the File Share to Your Azure Web App
- Open your Web App in the Azure Portal, go to Configuration > Path mappings > + New Azure Storage Mount.
- Fill in the required details:
- Name: A friendly label for the mount (e.g.,
webapp-content-mount) - Storage account: Select the storage account you just created
- File share: Pick the file share you set up
- Mount path: The path in your Web App where you want to store/access files (e.g.,
/site/wwwroot/uploadsfor Linux apps,D:\home\site\wwwroot\uploadsfor Windows apps) - Access key: Grab your storage account's access key from the Access keys section of the storage account
- Name: A friendly label for the mount (e.g.,
- Save the configuration. From now on, any files your Web App writes to that mount path will go straight to the Azure File Share instead of its local sandbox storage.
Step 3: Mount the File Share to Your VM
For Linux VMs:
- SSH into your Linux VM, then run these commands (replace placeholders with your actual storage details):
# Create a directory to serve as the mount point sudo mkdir /mnt/webapp-files # Mount the Azure File Share via CIFS sudo mount -t cifs //<your-storage-account>.file.core.windows.net/<your-file-share> /mnt/webapp-files -o username=<your-storage-account>,password=<your-storage-access-key>,serverino # To make the mount persistent across VM reboots, add this line to /etc/fstab echo "//<your-storage-account>.file.core.windows.net/<your-file-share> /mnt/webapp-files cifs username=<your-storage-account>,password=<your-storage-access-key>,serverino 0 0" | sudo tee -a /etc/fstab
For Windows VMs:
- RDP into your Windows VM, open PowerShell or Command Prompt, and run this (swap placeholders with your storage info):
# Mount the share as a network drive (Z: is an example—use any available drive letter) net use Z: \\<your-storage-account>.file.core.windows.net/<your-file-share> /user:<your-storage-account> <your-storage-access-key> # To keep the drive mounted after VM reboots, add the /persistent:yes flag net use Z: \\<your-storage-account>.file.core.windows.net/<your-file-share> /user:<your-storage-account> <your-storage-access-key> /persistent:yes
Method 2: Use Azure Blob Storage (Ideal for Unstructured Data)
If your Web App deals with unstructured data like images, videos, or large documents, Blob Storage is a great alternative. It's optimized for this type of content and integrates well with both Web Apps and VMs.
Step 1: Create a Blob Container
- In your storage account, go to Containers > + Container. Give it a name, set the access level (usually Private for security), and click Create.
Step 2: Update Your Web App to Use Blob Storage
You'll need to modify your Web App's code to read/write files directly to the Blob Container using Azure's Storage SDK. Here's a quick example in Python:
from azure.storage.blob import BlobServiceClient # Initialize the Blob Service Client with your storage connection string connection_string = "<your-storage-connection-string>" blob_service_client = BlobServiceClient.from_connection_string(connection_string) # Get the container client container_client = blob_service_client.get_container_client("<your-container-name>") # Upload a file to Blob Storage with open("local-file-path.txt", "rb") as data: container_client.upload_blob(name="uploaded-file.txt", data=data)
Step 3: Access Blob Storage from Your VM
For Linux VMs:
Use blobfuse to mount the Blob Container as a filesystem (so you can interact with blobs like regular files):
# Install blobfuse sudo apt-get update && sudo apt-get install blobfuse -y # Create a mount directory sudo mkdir /mnt/webapp-blobs # Mount the container (replace placeholders) sudo blobfuse /mnt/webapp-blobs --container-name=<your-container-name> --storage-account=<your-storage-account> --storage-account-key=<your-storage-access-key>
For Windows VMs:
You can use Azure Storage Explorer to browse, download, or upload blobs visually. Alternatively, use PowerShell to automate transfers:
# Install the Az.Storage module if you haven't already Install-Module -Name Az.Storage -Force # Connect to your storage account $storageContext = New-AzStorageContext -StorageAccountName "<your-storage-account>" -StorageAccountKey "<your-storage-access-key>" # Download all blobs from the container to a local directory Get-AzStorageBlobContent -Container "<your-container-name>" -Destination "C:\webapp-blobs" -Context $storageContext -Force
Key Things to Remember
- No Direct Sandbox Mounting: Azure Web App's local sandbox storage (e.g.,
/homefor Linux,D:\homefor Windows) is isolated and can't be mounted externally—using managed storage is the only supported way to share files with a VM. - Network Access: Ensure your VM has network access to the storage account. Check the storage account's Networking settings to allow traffic from the VM's subnet or public IP.
- Cost Considerations: Both Azure Files and Blob Storage have usage-based costs, so review Azure's pricing docs to understand the expenses for your use case.
内容的提问来源于stack exchange,提问作者Ayed Mohamed Amine

