我如何只通过Get-AzureStorageBlobContent下载最新文件

时间:2017-03-31 13:28:18

标签: powershell azure

我写过这段代码。我怎样才能下载最新文件?

$container_name = 'packageitems'
$destination_path = 'C:\pstest'
$connection_string = 'DefaultEndpointsProtocol=https;AccountName=[REPLACEWITHACCOUNTNAME];AccountKey=[REPLACEWITHACCOUNTKEY]'

$storage_account = New-AzureStorageContext -ConnectionString $connection_string

$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account

foreach ($blob in $blobs)
{
    New-Item -ItemType Directory -Force -Path $destination_path

    Get-AzureStorageBlobContent `
    -Container $container_name -Blob $blob.Name -Destination $destination_path `
    -Context $storage_account

}

1 个答案:

答案 0 :(得分:3)

试试这段代码:

$container_name = 'packageitems'
$destination_path = 'C:\pstest'
$connection_string = 'DefaultEndpointsProtocol=https;AccountName=[REPLACEWITHACCOUNTNAME];AccountKey=[REPLACEWITHACCOUNTKEY]'
$storage_account = New-AzureStorageContext -ConnectionString $connection_string
# Get the blobs list and then sort them by last modified date descending
$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account  | sort @{expression="LastModified";Descending=$true}
# First blob in that list would be the last modified.
$latestBlob = $blobs[0]
# Just download that blob
Get-AzureStorageBlobContent `
    -Container $container_name -Blob $latestBlob.Name -Destination $destination_path `
    -Context $storage_account

上面的代码是列出blob,然后根据上次修改日期按降序对它们进行排序。数组中的第一个元素将是最新的blob。然后它下载这个blob。