欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Azure]使用Azure Powershell上传本地目录中的文件文件(递归)

程序员文章站 2023-12-30 19:13:10
...

针对ASM环境的脚本:

param(
    # SrcDir
    [Parameter(Mandatory = $true)]
    [string]$SrcDir,

    # Subscription Name
    [Parameter(Mandatory = $true)] 
    [string]$SubscriptionName,

    # StorageAccountName
    [Parameter(Mandatory = $true)]
    [string]$StorageAccountName,
 
    # Container Name
    [Parameter(Mandatory = $true)]
    [string]$ContainerName
)

function Upload ($folder, $SrcDirLen)
{
    $subItems = Get-ChildItem $folder;
    foreach ($item in $subItems)
    {
        if ($item.Mode -eq "d-----")
        {
            Upload $item.FullName $SrcDirLen;
        } else {
	    $fileName = "$folder\$item";
	    $destpath = $folder.Substring($SrcDirLen);
	    $blobName = "$destpath/$item";
	    Write-Host "Copying $fileName to $blobName";
	    [void](Set-AzureStorageBlobContent -File $filename -Container $ContainerName -Blob $blobName -Context $storageAccount.Context -Force);
	}
    }
}

$cred = Get-Credential -Message "Use your Azure account to login";
[void](Add-AzureAccount -Environment AzureChinaCloud -Credential $cred);
[void](Select-AzureSubscription -SubscriptionName $SubscriptionName);

$storageAccount = Get-AzureStorageAccount -StorageAccountName $StorageAccountName;
upload $SrcDir $SrcDir.Length;
Write-Host "All files in $SrcDir uploaded to $containerName


针对ARM环境的脚本:

param(
    # SrcDir
    [Parameter(Mandatory = $true)]
    [string]$SrcDir,

    # Subscription Name
    [Parameter(Mandatory = $true)] 
    [string]$SubscriptionName, 
 
    # ResourceGroupName
    [Parameter(Mandatory = $true)]
    [string]$ResourceGroupName,

    # StorageAccountName
    [Parameter(Mandatory = $true)]
    [string]$StorageAccountName,
 
    # Container Name
    [Parameter(Mandatory = $true)]
    [string]$ContainerName
)

function Upload ($folder, $SrcDirLen)
{
    $subItems = Get-ChildItem $folder;
    foreach ($item in $subItems)
    {
        if ($item.Mode -eq "d-----")
        {
            Upload $item.FullName $SrcDirLen;
        } else {
	    $fileName = "$folder\$item";
	    $destpath = $folder.Substring($SrcDirLen);
	    $blobName = "$destpath/$item";
	    Write-Host "Copying $fileName to $blobName";
	    [void](Set-AzureStorageBlobContent -File $filename -Container $ContainerName -Blob $blobName -Context $storageAccount.Context -Force);
	}
    }
}

$cred = Get-Credential -Message "Use your Azure account to login";
[void](Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $cred);
[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);

$storageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $ResourceGroupName;
Upload $SrcDir $SrcDir.Length;

Write-Host "All files in $SrcDir uploaded to $ContainerName.";


测试效果:

PS C:\Users\DanielHX> &C:\Users\DanielHX\Desktop\[ARM]upload_local_dir_to_storage.ps1 -SubscriptionName Internal-005 -StorageAccountName daneaststorag
e -ContainerName demo -SrcDir D:\test -ResourceGroupName daneastresourcegroup1
Copying D:\test\test1\1.txt to \test1/1.txt
Copying D:\test\test1\2.txt to \test1/2.txt
Copying D:\test\test2\test4\1.txt to \test2\test4/1.txt
Copying D:\test\test2\1.txt to \test2/1.txt
Copying D:\test\test2\2.txt to \test2/2.txt
Copying D:\test\test2\3.txt to \test2/3.txt
Copying D:\test\test3\1.txt to \test3/1.txt
Copying D:\test\test3\2.txt to \test3/2.txt
Copying D:\test\test3\3.txt to \test3/3.txt
Copying D:\test\test3\4.txt to \test3/4.txt
Copying D:\test\1.txt to /1.txt
All files in D:\test uploaded to demo.
PS C:\Users\DanielHX> &C:\Users\DanielHX\Desktop\[ASM]upload_local_dir_to_storage.ps1 -SrcDir D:\test -SubscriptionName Internal-005 -StorageAccountNa
me danieleaststorage -ContainerName test
Copying D:\test\test1\1.txt to \test1/1.txt
Copying D:\test\test1\2.txt to \test1/2.txt
Copying D:\test\test2\test4\1.txt to \test2\test4/1.txt
Copying D:\test\test2\1.txt to \test2/1.txt
Copying D:\test\test2\2.txt to \test2/2.txt
Copying D:\test\test2\3.txt to \test2/3.txt
Copying D:\test\test3\1.txt to \test3/1.txt
Copying D:\test\test3\2.txt to \test3/2.txt
Copying D:\test\test3\3.txt to \test3/3.txt
Copying D:\test\test3\4.txt to \test3/4.txt
Copying D:\test\1.txt to /1.txt
All files in D:\test uploaded to test.



上一篇:

下一篇: