【问题标题】:How to upload all files of a specific type to S3 Bucket?如何将特定类型的所有文件上传到 S3 Bucket?
【发布时间】:2018-07-01 02:11:08
【问题描述】:

当我这样做时:

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

【问题讨论】:

  • 一切看起来都很好,除了 -BucketName 参数。这是你必须在 S3 中设置的东西吗?
  • @TimFerrill bucket.name 就是一个例子。它实际上是实际的存储桶名称,减去 s3:// 前缀。
  • 此语句的另一个问题(发生在我身上)是 CannedACLName 选项。在文档中,它以“-CannedACLName PublicRead”的形式提供,但这会导致与上述相同的错误。它需要读取“-CannedACLName public-read”。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    【解决方案3】:

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

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

    希望这对那里的人有所帮助!

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 2015-04-12
        • 2014-07-16
        • 2014-05-31
        • 2023-01-09
        • 2014-03-15
        • 2021-10-04
        • 2012-12-28
        相关资源
        最近更新 更多