我需要将IIS日志提早7天上传到AWS S3 Bukcet。通过使用以下代码,我可以访问存储桶下的AWS文件夹

Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
$AKey = ""
$SKey = ""
$source = "C:\inetpub\logs\LogFiles\*"
$outputpath = "C:\scripts\Logs\logs3.txt"
Set-AWSCredentials -AccessKey $AKey -SecretKey $SKey

function Get-Subdirectories {
    param  (
        [string] $BucketName,
        [string] $KeyPrefix,
        [bool] $Recurse
    )

    @(Get-S3Object -BucketName $BucketName -KeyPrefix $KeyPrefix -Delimiter '/') | Out-Null

    if ($AWSHistory.LastCommand.Responses.Last.CommonPrefixes.Count -eq 0) {
        return
    }

    $AWSHistory.LastCommand.Responses.Last.CommonPrefixes

    if ($Recurse) {
        $AWSHistory.LastCommand.Responses.Last.CommonPrefixes | ForEach-Object { Get-Subdirectories -BucketName $BucketName -KeyPrefix $_ -Recurse $Recurse }
    }
}

function Get-S3Directories {
    param  (
        [string] $BucketName,
        [bool] $Recurse = $false
    )
    Get-Subdirectories -BucketName $BucketName -KeyPrefix '/' -Recurse $Recurse
}

现在,如果我输入Get-S3Directories -BucketName backups,我将得到以下输出:
SERVER-xxx-OLogs/
SERVER-xxx-untime-logs /
SERVER-xxx-FRLogs/
SERVER-oooooRLogs/
SERVER-IISLogFiles/

现在的挑战是我必须在SERVER-IISLogFiles/目录下将IIS迁移7天以上

所以我创建了这个
$sfolder = Get-S3Directories -BucketName Backups
Foreach ($folder in $sfolder) {
    $wc = New-Object System.Net.WebClient

    Set-AWSCredentials -AccessKey $AKey -SecretKey $SKey -StoreAs For_Move
    Initialize-AWSDefaults -ProfileName For_Move -Region US-east-1

    Start-Transcript -path $outputpath -Force
    foreach ($i in Get-ChildItem $source -include *.log -recurse) {
        if ($i.CreationTime -lt ($(Get-Date).AddDays(-7))) {
            $fileName = (Get-ChildItem $i).Name
            $parentFolderName = Split-Path (Split-Path $i -Parent) -Leaf
            Write-S3Object -BucketName $folder -File $i
        }
    }
}
Stop-Transcript

我不完全确定它是否将移动到SERVER-IISLogFiles/目录,不确定除了此以外是否缺少任何内容,我怀疑它是否将IIS上的本地IIS文件夹的文件夹结构保留下来

最佳答案

更新:

您可以尝试使用以下方法在S3存储桶中创建文件夹结构。使用Key Prefix参数并拆分文件夹路径应该可以。

$Params = @{
    BucketName = 'backup'
    Folder = '$Source or whatever path'
    KeyPrefix = (Split-Path -Path 'C:\PATHTO\SOURCE\DIRECTORY' -Leaf).TrimEnd('\')
    Recurse = $true
    Region = 'REGION'
}
Write-S3Object @Params

09-04 04:27