8

当我这样做时:

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 并查看我帐户中的所有存储桶,因此我知道它已正确配置。

编辑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单独做,我会得到对象的完整路径。但是,这里面有空格。这会是个问题吗?

4

4 回答 4

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 文件类型的所有文件,并且对于每个对象(由百分号表示)我们将文件(由正在迭代的文件。

于 2014-05-28T20:35:24.077 回答
2

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

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

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

于 2015-08-21T08:11:37.097 回答
2

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

当您在 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

}

希望这可以帮助那里的人!

于 2017-12-13T15:30:47.357 回答
0

对于其他正在寻找执行此操作但不用于公共访问的脚本的新手,请完全删除 -CannedACLName public-read。我花了 2 个令人沮丧的小时才弄清楚这一点。:)

于 2017-03-16T15:42:50.147 回答