PowerShell 脚本无法将 zip 文件从 Windows 虚拟机上传到存储帐户

问题描述 投票:0回答:1

在 Windows Server 2019 Datacenter 虚拟机中,我想上传文件 (C:\Data\Archive�40312_Log.zip) 到存储帐户。但是,我收到以下错误:

Message: Response status code does not indicate success: 403 (This request is not authorized to perform this operation.).
CategoryInfo InvalidOperation: (Method: PUT, Reques…-Length: 6159687
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
FullyQualifiedErrorId: WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
StackTrace: at SendToStorageAccount, <No file>: line 18
at <ScriptBlock>, <No file>: line 1
The File 20240312_Log.zip FAILED to upload

在“日志”容器中,我创建了以下 SAS 令牌:

存储帐户的网络限制为:从所有网络启用

我创建了一个不同的 SAS 令牌,并将我的笔记本电脑 IP 列入白名单,并且我可以使用以下脚本上传 zip 文件。但从虚拟机上传失败:

function SendToStorageAccount {
    param (
        [string] $zipFileName
    )
    $myDevSas = [System.Environment]::GetEnvironmentVariable('myDevSas','Machine')
    #The target URL wit SAS Token
    $uri = "https://mystdev.blob.core.windows.net/logs/$($zipFileName)?$($myDevSas)"

    #Define required Headers
    $headers = @{
        'x-ms-blob-type' = 'BlockBlob'
    }
    #Upload File...
    $apiCall
    try {
        $apiCall = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $zipFileName    
        Write-Output $apiCall
        Write-Output "The File $($zipFileName) uploaded successfully"
        Remove-Item $zipFileName
    }
    catch {
        "Message: $($_.Exception.Message)"
        "CategoryInfo $($_.CategoryInfo)"
        "FullyQualifiedErrorId: $($_.FullyQualifiedErrorId)"
        "StackTrace: $(($_.ScriptStackTrace -replace '^'))"
        Write-Output $apiCall
        Write-Output "The File $($zipFileName) FAILED to upload"
    }
}


$fileName = "20240312_Log.zip"
SendToStorageAccount $fileName

蒂亚!

azure powershell azure-blob-storage
1个回答
0
投票

消息:响应状态码不表示成功:403(此请求无权执行此操作。)。

当您没有足够的权限或传递错误的 SAS 令牌来访问存储帐户时,就会出现上述错误。

存储帐户的网络限制为:从所有网络启用

如果您的存储帐户网络已从所有网络启用,则无需在 SAS 令牌中传递虚拟机 IP 地址。

在我的环境中,我创建了虚拟机并创建了 SAS 令牌,但没有传递 IP 地址。相同的脚本执行成功。

脚本:

[System.Environment]::SetEnvironmentVariable('myDevSas', 'sp=racwl&st=2024-03-13T05:53:48Z&se=2024-03-13T13:53:48Z&spr=https&sv=2022-11-02&sr=c&sig=xxxx', [System.EnvironmentVariableTarget]::Machine)

function SendToStorageAccount {
    param (
        [string] $zipFileName
    )
    $myDevSas = [System.Environment]::GetEnvironmentVariable('myDevSas','Machine')
    #The target URL with SAS Token
    $uri = "https://venkat789.blob.core.windows.net/logs/$($zipFileName)?$($myDevSas)"

    #Define required Headers
    $headers = @{
        'x-ms-blob-type' = 'BlockBlob'
    }
    #Upload File...
    $apiCall
    try {
        $apiCall = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $zipFileName    
        Write-Output $apiCall
        Write-Output "The file $($zipFileName) uploaded successfully"
        Remove-Item $zipFileName
    }
    catch {
        "Message: $($_.Exception.Message)"
        "CategoryInfo $($_.CategoryInfo)"
        "FullyQualifiedErrorId: $($_.FullyQualifiedErrorId)"
        "StackTrace: $(($_.ScriptStackTrace -replace '^'))"
        Write-Output $apiCall
        Write-Output "The file $($zipFileName) FAILED to upload"
    }
}


$fileName = "antarctica-latest-free.zip"
SendToStorageAccount $fileName

输出:

The file antarctica-latest-free.zip uploaded successfully

Enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.