在不使用适用于 .NET 的 AWS SDK 的情况下从 Powershell 将文件上传到 S3 的替代方案?

Alternative to upload files to S3 from Powershell without using AWS SDK for .NET?

我需要从 PowerShell 脚本将文件(总共 100GB,其中有几个 10GB 的文件)上传到 S3: http://docs.aws.amazon.com/powershell/latest/reference/Index.html(亚马逊简单存储服务)

我使用了适用于 .NET 的 AWS SDK,并且非常适合少量文件。我只是在之前的代码中创建了存储桶并尝试使用 Write-S3Object

上传所有文件
Write-S3Object -Region $S3_Region -AccessKey $S3_AccessKey -SecretKey $S3_SecretKey -ServerSideEncryption "AES256" -Folder $sourceFolder -BucketName $S3_Bucket -Recurse -KeyPrefix "/" 

但是我有几个稳定性方面的问题。它经常因两种类型的错误而失败。 (有4683个文件要上传)

Uploaded 2664 object(s) to bucket 'bucket.test' from 'R:\temp\files' with keyprefix '/'

Uploaded 35 object(s) to bucket 'bucket.test' from 'R:\temp\files' with keyprefix '/' System.InvalidOperationException: The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed. ---> Amazon.S3.AmazonS3Exception: The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed. ---> Amazon.Runtime.Internal.HttpErrorResponseException: The remote server returned an error: (404) Not Found. ---> System.Net.WebException: The remote server returned an error: (404) Not Found.

所以我的问题是:

我没有使用 Write-S3Object,而是使用 S3 TransferUtility 能够使用分段上传来上传大文件的对象。

我一直在使用下面的 powershell 脚本将文件上传到 S3。

function UploadToAmazonUsingSDK()
{
    param([string] $sourceLocation, [string] $bucketName, [string] $versionNumber)    

    Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net45\AWSSDK.dll"

    $AccessKey= "your aws accesskey"
    $SecretKey ="your secret key"

    $s3Config=New-Object Amazon.S3.AmazonS3Config
    $s3Config.UseHttp = $false
    $s3Config.ServiceURL = "https://s3-eu-west-1.amazonaws.com"
    $s3Config.BufferSize = 1024 * 32

    $client=[Amazon.AWSClientFactory]::CreateAmazonS3Client($AccessKey,$SecretKey,$s3Config)

    $transferUtility = New-Object -TypeName Amazon.S3.Transfer.TransferUtility($client)   

    $files = Get-ChildItem $sourceLocation

    foreach ($fileName in $files) {        
        $amazonKey = $versionNumber + '/' + $fileName        
        Write-Host $amazonKey
        Write-Host $fileName 
        Write-Host $fileName.FullName

        $transferUtilRequest = New-Object -TypeName Amazon.S3.Transfer.TransferUtilityUploadRequest
        $transferUtilRequest.BucketName = $bucketName
        $transferUtilRequest.FilePath = $fileName.FullName
        $transferUtilRequest.Key = $amazonKey
        $transferUtilRequest.AutoCloseStream = $true
        $transferUtility.Upload($transferUtilRequest)
    }    
}

如果你是 Golang 爱好者可以尝试一下 https://github.com/minio/mc。我们还有 64 位 Windows 的预编译二进制文件,可用于 powershell、命令提示符等

https://dl.minio.io:9000/updates/2015/Sept/windows-amd64/mc.exe

要添加您的 S3 凭据,只需执行

C:\> mc.exe config host add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12

mc.exe 实现了漂亮的进度条、彩色控制台输出、分段上传、会话管理等

我们将在下周发布新版本,同时支持 32 位和 64 位Windows。