【问题标题】:Issue with comparing two collections of blobs in Powershell在 Powershell 中比较两个 blob 集合的问题
【发布时间】:2018-07-29 23:36:56
【问题描述】:

我有一个 powershell 脚本,可以将 Azure blob 存储中的文件下拉到本地文件夹中。一旦它拉下一个文件,它就会创建一个同名的 0 字节文件,但扩展名为 .succeeded(例如 foo.csv 变为 foo.csv.succeeded)。原始文件和 .succeeded 文件都保留在 blob 存储中。

我想修改我的脚本,以便在再次运行脚本时不会重新下载具有 .succeeded 副本的文件。我试图为此比较两组 blob,但我无法让它工作。这是我拥有的代码的 sn-p:

$ext = '.succeeded'

$ctx = New-AzureStorageContext -sasToken $sasToken -StorageAccountName $accountname        
$blobs = Get-AzureStorageBlob -Container $blob_source_container -Context $ctx  

# Get list of all blobs from specified folder with the '.succeeded' extension
$succeeded = Get-AzureStorageBlob -Container $blob_source_container -Context $ctx | 
    Where-Object { (($_.Name -like "$blob_source_folder*") -or ($blob_source_folder -eq '*')) -and ($_.Name -like "*$sidecar_extension") }

# Strip the extension from the blob names ending in '.succeeded'
foreach($blob in $succeeded) {
    $blob.Name = $blob.Name.Substring(0, $blob.Name.LastIndexOf('.'))
}

foreach($blob in $blobs) {
    if($blob.Name -notlike "*$ext"){
        if($blob.Name -notin $succeeded | Select Name) {
            # do stuff
        }
    }
}

如果我在 do stuff 语句中放置一个 Write-Output $blob.Name,它会打印所有可以下载的 blob,无论它们是否在 $succeeded 中。

我这一天大部分时间都在这样做,我即将放弃。我错过了什么基本的东西吗?

【问题讨论】:

  • $sidecar_extension ($_.Name -like "*$sidecar_extension") 中有什么?这应该是 $ext 吗?
  • 是的。我的错。我的问题中的代码 sn-p 是“简化的”。在实际脚本中,它是 $sidecar_extension。在示例中是 $ext

标签: powershell azure azure-storage


【解决方案1】:

您只获得成功的查询似乎不正确。下面是一个快速的尝试,但写入输出 $blob 和 $succeeded 的内容以查看您哪里出错了。如果您的容器很大,请创建一个测试容器,其中只有几个文件进行调试。

尝试替换

# Get list of all blobs from specified folder with the '.succeeded' extension
$succeeded = Get-AzureStorageBlob -Container $blob_source_container -Context $ctx | 
    Where-Object { (($_.Name -like "$blob_source_folder*") -or ($blob_source_folder -eq '*')) -and ($_.Name -like "*$sidecar_extension") }

# Get list of all blobs from specified folder with the '.succeeded' extension
$succeeded = Get-AzureStorageBlob -Container $blob_source_container -blob *.succeeded -Context $ctx | 
    Where-Object { (($_.Name -like "$blob_source_folder*") -or ($blob_source_folder -eq '*')) -and ($_.Name -like "*$sidecar_extension") }

即将-blob *.succeeded 添加到您的 $succeeded 查询中。

【讨论】:

  • 感谢您的回复。我可以确认 $succeeded 变量包含它应该包含的所有值 - 具有 .succeeded 扩展名但已删除扩展名的所有文件名的列表。
  • 你的比较肯定有问题。看这里:stackoverflow.com/questions/9815192/…
【解决方案2】:

今天早上我自己解决了这个问题(在几个小时没看代码之后!)

我只是换了:

if($blob.Name -notin $succeeded | Select Name) {

与:

if($blob.Name -notin $succeeded.Name) {

它奏效了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多