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

时间:2014-05-28 17:39:04

标签: 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 "*.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将所有特定文件类型上传到存储桶?

编辑:

我故意将Set-DefaultAWSRegion设置为存储桶不在的区域,并获得

Write-S3Object : The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. 

作为错误消息,正如预期的那样,所以看起来它可以连接到存储桶,并且它知道它不在某个区域。

此外,如果我在存储桶名称之前输入s3://前缀,我会收到一条消息,指出无法找到该存储桶,因此看起来我现在输入的内容是正确的。

我可以执行Get-S3Bucket并查看我帐户中的所有存储桶,因此我知道它已正确配置。

EDIT2:

如果我这样做:

> $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,我会获得对象的完整路径。 但是,它有空格。这可能是个问题吗?

4 个答案:

答案 0 :(得分:3)

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

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,因为变量不在该上下文中进行评估。同样,您正在获取此" FilePath属性指示的文件不存在!" 错误,因为您的文件系统中没有名为$f[0].fullName的文件

将单个文件写入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文件类型的所有文件,并且对于每个对象(由百分号表示),我们将使用Key将文件(由$_表示)写入MyTestBucketName这是正在迭代的当前文件的name属性。

答案 1 :(得分:2)

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

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

这解决了我的问题,也应该为你解决。

答案 2 :(得分:1)

对我来说,这些解决方案都不起作用,所以我发现了这一点。

当你在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执行的,当它执行时,它会停止识别您正在发送的文件的路径。

所以我加了| %{$ _。FullName}到GetChildItem获取每个文件的完整路径,现在一切正常:

$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

}

希望这有助于那里的人!

答案 3 :(得分:0)

对于那些寻找执行此操作而不是公共访问的脚本的其他新手,完全删除-CannedACLName public-read。花了我两个令人沮丧的时间才弄明白。 :)