4

我一直在使用 PowerShell v3(来自此处的 CTP2 )及其新的 Invoke-RestMethod 进行一些工作,如下所示:

Invoke-RestMethod -Uri $dest -method PUT -Credential $cred -InFile $file

但是,我想用它来推送非常大的二进制对象,因此希望能够从大型二进制文件中推送一系列字节。

例如,如果我有一个 20Gb VHD,我想将它分成多个块,例如每个 5Gb(不先拆分和保存单个块),然后将它们放入/发布到 BLOB 存储,如 S3、Rackspace、Azure 等.我还假设块大小大于可用内存。

我读过 Get-Content 在大型二进制文件上的工作效率不是很高,但这似乎并不是一个模糊的要求。有没有人有任何可用于此的方法,特别是与 PowerShell 的新 Invoke-RestMethod 结合使用?

4

2 回答 2

1

我相信您正在寻找的 Invoke-RestMethod 参数是

-TransferEncoding Chunked

但无法控制块或缓冲区大小。如果我错了,有人可以纠正我,但我认为块大小是 4KB。每个块都被加载到内存中然后发送,因此您的内存不会被您发送的文件填满。

于 2012-07-25T05:18:53.450 回答
0

要检索文件的部分(块),您可以创建System.IO.BinaryReader一个方便的花花公子Read( [Byte[]] buffer, [int] offset, [int] length)方法。这是一个使它变得容易的函数:

function Read-Bytes {
    [CmdletBinding()]
    param (
          [Parameter(Mandatory = $true, Position = 0)]
          [string] $Path
        , [Parameter(Mandatory = $true, Position = 1)]
          [int] $Offset
        , [Parameter(Mandatory = $true, Position = 2)]
          [int] $Size
    )

    if (!(Test-Path -Path $Path)) {
        throw ('Could not locate file: {0}' -f $Path);
    }

    # Initialize a byte array to hold the buffer
    $Buffer = [Byte[]]@(0)*$Size;

    # Get a reference to the file
    $FileStream = (Get-Item -Path $Path).OpenRead();

    if ($Offset -lt $FileStream.Length) {
        $FileStream.Position = $Offset;
        Write-Debug -Message ('Set FileStream position to {0}' -f $Offset);
    }
    else {
        throw ('Failed to set $FileStream offset to {0}' -f $Offset);
    }

    $ReadResult = $FileStream.Read($Buffer, 0, $Size);
    $FileStream.Close();

    # Write buffer to PowerShell pipeline
    Write-Output -InputObject $Buffer;

}

Read-Bytes -Path C:\Windows\System32\KBDIT142.DLL -Size 10 -Offset 90;
于 2012-07-27T21:46:35.210 回答