【问题标题】:Issue with a Powershell script transferring files to a S3 Bucket将文件传输到 S3 存储桶的 Powershell 脚本问题
【发布时间】:2018-05-15 20:13:58
【问题描述】:

我正在尝试设置一个 powershell 脚本以尝试设置将目录自动传输到 S3 存储桶,我一直按照http://todhilton.com/technicalwriting/upload-backup-your-files-to-amazon-s3-with-powershell/ 中列出的说明进行操作,但是当我运行它时出现以下错误。

Unable to find type [Amazon.AWSClientFactory].
At line:18 char:9
+ $client=[Amazon.AWSClientFactory]::CreateAmazonS3Client($accessKeyID, ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.AWSClientFactory:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

我的代码粘贴在下面...如果有人有一些见解,那就太棒了:)

# Constants
$sourceDrive = "C:\"
$sourceFolder = "Users\Administrator\AppData\Roaming\folder"
$sourcePath = $sourceDrive + $sourceFolder
$s3Bucket = "bucket"
$s3Folder = "Archive"

# Constants – Amazon S3 Credentials
$accessKeyID="KEY"
$secretAccessKey="Secret"

# Constants – Amazon S3 Configuration
$config=New-Object Amazon.S3.AmazonS3Config
$config.RegionEndpoint=[Amazon.RegionEndpoint]::"ap-southeast-2"
$config.ServiceURL = "https://s3-ap-southeast-2.amazonaws.com/"

# Instantiate the AmazonS3Client object
$client=[Amazon.AWSClientFactory]::CreateAmazonS3Client($accessKeyID,$secretAccessKey,$config)

# FUNCTION – Iterate through subfolders and upload files to S3
function RecurseFolders([string]$path) {
  $fc = New-Object -com Scripting.FileSystemObject
  $folder = $fc.GetFolder($path)
  foreach ($i in $folder.SubFolders) {
    $thisFolder = $i.Path

    # Transform the local directory path to notation compatible with S3 Buckets and Folders
    # 1. Trim off the drive letter and colon from the start of the Path
    $s3Path = $thisFolder.ToString()
    $s3Path = $s3Path.SubString(2)
    # 2. Replace back-slashes with forward-slashes
    # Escape the back-slash special character with a back-slash so that it reads it literally, like so: "\\"
    $s3Path = $s3Path -replace "\\", "/"
    $s3Path = "/" + $s3Folder + $s3Path

    # Upload directory to S3
    Write-S3Object -BucketName $s3Bucket -Folder $thisFolder -KeyPrefix $s3Path
  }

  # If subfolders exist in the current folder, then iterate through them too
  foreach ($i in $folder.subfolders) {
    RecurseFolders($i.path)
  }
}

# Upload root directory files to S3
$s3Path = "/" + $s3Folder + "/" + $sourceFolder
Write-S3Object -BucketName $s3Bucket -Folder $sourcePath -KeyPrefix $s3Path

# Upload subdirectories to S3
RecurseFolders($sourcePath)

【问题讨论】:

  • 您是否加载了所需的 AWS Dll?错误消息告诉您找不到“Amazon.AWSClientFactory”,将其导入您的会话

标签: amazon-web-services powershell amazon-s3


【解决方案1】:

请检查以下内容:

Amazon AWSClientFactory does not exists

Change: AWSClientFactory is removed

我使用了以下脚本:

# Bucket region details
$RegionEndpoint = 'us-east-1'
#$ServiceURL = 'https://s3-us-east-1.amazonaws.com/'

#Credentials initialized
$credsCSV = Get-ChildItem "E:\myAPIUser_credentials.csv" 
$credsContent = Import-Csv $credsCSV.FullName

$accessKeyID = $credsContent.'Access key ID'
$secretAccessKey = $credsContent.'Secret access key'

Initialize-AWSDefaults -Region $RegionEndpoint -AccessKey $accessKeyID -SecretKey $secretAccessKey

$sourceFolder = "E:\Code\powershell\PSmy\git\AWSPowerShell"
$targetFolder = Get-Date -Format "dd-MMM-yyyy"

Write-S3Object -BucketName $s3Bucket.BucketName -Folder $sourceFolder -Recurse -KeyPrefix $targetFolder\

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2018-06-02
    • 2019-10-14
    • 2022-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多