Powershell 如何将特定类型的所有文件上载到S3 Bucket?

Powershell 如何将特定类型的所有文件上载到S3 Bucket?,powershell,amazon-web-services,amazon-s3,aws-powershell,Powershell,Amazon Web Services,Amazon S3,Aws Powershell,当我这样做时: foreach ($f in (Get-ChildItem -filter "*.flv")){ Write-S3Object -BucketName bucket.example -File $f.fullName -Key $f.name -CannedACLName PublicRead } 我得到这个错误: Write-S3Object : At line:1 char:51 + foreach ($f in (Get-ChildItem -filter "*

当我这样做时:

foreach ($f in (Get-ChildItem -filter "*.flv")){
    Write-S3Object -BucketName bucket.example -File $f.fullName -Key $f.name -CannedACLName PublicRead
}
我得到这个错误:

Write-S3Object :
At line:1 char:51
+  foreach ($f in (Get-ChildItem -filter "*.flv")){ Write-S3Object -BucketName xx. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...eS3ObjectCmdlet:WriteS3ObjectCmdlet) [Write-S3Objec
   t], InvalidOperationException
    + FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.WriteS3ObjectCmdlet
我做错了什么?我可以做些什么来查看更多的错误,或者这只是一个语法问题

如何使用powershell将所有特定文件类型上传到bucket

编辑: 我故意将
set DefaultAWSRegion
设置为bucket不在的区域,并获得

Write-S3Object : The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. 
作为错误消息,正如预期的那样,它看起来可以连接到bucket,并且知道它不在某个区域

另外,如果我在bucket名称之前输入
s3://
前缀,我会收到一条消息,提示找不到bucket,因此看起来我现在输入的内容是正确的

我可以做Get-S3Bucket并查看我帐户上的所有bucket,因此我知道它配置正确

编辑2: 如果我这样做:

> $f = Get-ChildItem -filter "*.flv"
> Write-S3Object

cmdlet Write-S3Object at command pipeline position 1
Supply values for the following parameters:
BucketName: bucket.name
Key: $f[0].name
File: $f[0].fullName
Write-S3Object : The file indicated by the FilePath property does not exist!
At line:1 char:1
+ Write-S3Object
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...eS3ObjectCmdlet:WriteS3ObjectCmdlet) [Write-S3Objec
   t], InvalidOperationException
    + FullyQualifiedErrorId : System.ArgumentException,Amazon.PowerShell.Cmdlets.S3.WriteS3ObjectCmdlet
如果我分别执行
$f[0].fullName
,我将获得对象的完整路径。
然而,这里面有空间。这可能是个问题吗?

当您在命令行中填写缺少的参数时,需要指定它们的文字字符串值。当我在本地模拟您的问题时:

PS C:\> Write-S3Object

cmdlet Write-S3Object at command pipeline position 1
Supply values for the following parameters:
BucketName: MyTestBucketNameHere
Key: $testName
File: C:/test.txt
我在S3上得到了一个文件,其密钥名为
$testName
,因为变量不会在该上下文中求值。同样,由于文件系统中没有名为
$f[0].fullName
的文件,因此您会收到“FilePath属性指示的文件不存在!”错误

将单个文件写入S3的示例:

PS C:> Write-S3Object -BucketName "MyTestBucketName" -Key "file.txt" -File "C:/test.txt"
要将所有文件写入S3,请执行以下操作:

PS C:\> (Get-ChildItem -filter "*.flv") | % { Write-S3Object -BucketName "MyTestBucketName" -File $_ -Key $_.name}

这将首先获取当前目录中具有flv文件类型的所有文件,对于每个对象(由百分号表示),我们将使用当前迭代文件的name属性键将文件(由
$\uuuu
表示)写入MyTestBucketName。

参数-CannedACLName PublicRead不正确,正确的参数是

foreach($f in(Get ChildItem-filter“*.flv”)){
Write-S3Object-BucketName bucket.example-File$f.fullName-Key$f.name-CannedACLName public read
}


这为我解决了这个问题,也应该为您解决。

对于其他正在寻找实现此功能但不用于公共访问的脚本的新手,请删除-CannedACLName public read。我花了两个小时才弄明白这一点

对我来说,这些解决方案都不起作用,所以我发现是这样的

当您使用Powershell ISE(4.0)时,无论您如何发送本地文件名,它都会知道其位置,这意味着您可以简单地执行此操作,在我的示例中,我尝试将名为e:\backups的文件夹的所有备份上载到S3中的一个存储桶中:

$results = Get-ChildItem -Path E:\Backups

foreach ($f in $results) { 

  $filename = [System.IO.Path]::GetFileName($f)

  Write-S3Object -BucketName bi-dbs-daily-backup -File $f -Key $filename

}
如果我在Powershell ISE中运行此程序,一切正常,但是如果我创建一个.ps1并尝试使用任务计划程序运行它,我会得到:FilePath属性指示的文件不存在

事实证明,当Windows尝试运行.ps1时,它基本上是从CLI运行的,当它运行时,它会停止识别您发送的文件的路径

因此,我在GetChildItem中添加了|%{$.FullName},以获取每个文件的完整路径,现在一切正常:

$results = Get-ChildItem -Path E:\Backups **| % { $_.FullName }** 

foreach ($f in $results) { 

  $filename = [System.IO.Path]::GetFileName($f)

  Write-S3Object -BucketName bi-dbs-daily-backup -File $f -Key $filename

}

希望这有助于其他人

除了-BucketName参数外,其他一切看起来都很好。这是你必须在S3中设置的吗?@TimFerrill
bucket.name
就是一个例子。它实际上是实际的bucket名称,减去
s3://
前缀。在文档中,它作为“-CannedACLName PublicRead”提供,但这会导致与上述相同的错误。它需要读“-CannedACLName public read”。谢谢。亚马逊文档中出现了错误的名称:此页面上列出了正确的名称: