【问题标题】:Azure Block Blob PowerShell Script Creating 0 B filesAzure Block Blob PowerShell 脚本创建 0 B 个文件
【发布时间】:2020-11-05 18:31:49
【问题描述】:

我是 Azure Blob 存储的新手,我编写了一个 PowerShell 脚本,用于将 SQL Server 备份文件从给定文件夹上传到我的 Azure 容器。当我执行它时,Azure 中出现了一个文件,但它的大小为 0 字节。这是我的代码:

#Finds newest .bak file in folder
$dir = "I:\Backup\*.bak"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1

#Get the File-Name without path
$name = (Get-Item $latest).Name

#The target URL wit SAS Token
$uri = "https://*****.blob.core.windows.net/sapprodback/$($name)?***My SAS Token is here***"

#Define required Headers
$headers = @{
'x-ms-blob-type' = 'BlockBlob'
}

#Upload File...
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $latest

我错过了什么?

【问题讨论】:

  • $file 在哪里定义?应该是 $latest 吗?
  • 好点。让我更新我的代码。 $file 应该是 $latest

标签: azure powershell azure-blob-storage


【解决方案1】:

感谢 ewong 的评论,我明白了。我的 -InFile 没有完整的文件路径。我创建了另一个变量并将其连接到我的 $latest.name 并且它起作用了。

#Finds newest .bak file in folder
$dir = "I:\Backup\*.bak"
$dir2 = "I:\Backup\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1

#Get the File-Name without path
$name = (Get-Item $latest).Name
$file = $dir2 + $name

#The target URL wit SAS Token
$uri = "https://*****.blob.core.windows.net/sapprodback/$($name)?***My SAS Token is here***"

#Define required Headers
$headers = @{
'x-ms-blob-type' = 'BlockBlob'
}

#Upload File...
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $file

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2021-06-11
    • 2019-10-07
    • 2013-09-16
    相关资源
    最近更新 更多