【问题标题】:How to upload files to S3 using the last modified date of a folder?如何使用文件夹的最后修改日期将文件上传到 S3?
【发布时间】:2019-10-03 23:06:16
【问题描述】:

我正在尝试编写一个 PowerShell 脚本,该脚本允许我根据文件夹的最后修改日期将文件上传到我的 AWS S3 存储桶。

这是我迄今为止所拥有的:

using namespace System.IO;
Set-AWSCredentials -StoredCredentials MyCredentialsAws
Set-DefaultAWSRegion us-east-1

[String] $root = "C:\Users\Administrator\Documents\TestFolder";

[DateTime]$today = [DateTime]::Now.Date;

[FileSystemInfo[]]$folderList = Get-ChildItem -Path $root -Directory;
foreach( $folder in $folderList ) {

    if( $folder.LastWriteTime -lt $today ) {
        [String] $folderPath = $folder.FullName;
        aws s3 cp $folder s3://bucketname/$folder --recursive 
   }
}

但是,上面给了我错误:

“用户提供的路径不存在”

任何帮助将不胜感激。

【问题讨论】:

  • 该消息看起来像是来自aws 命令。试试$name = $folder.Name; aws s3 cp $folderPath s3://bucketname/$name --recursive
  • 解决了!!!你是神队友@AnsgarWiechers

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


【解决方案1】:

一方面,当您可能打算使用$folderPath 时,您使用$folder 作为aws 命令行中的源。此外,Get-ChildItem 的扩展并不一致。根据上下文,对象有时会扩展为名称,有时会扩展为完整路径(see here 以获得更详细的解释)。因此,明确使用给定场景所需的属性被认为是一种很好的做法(NameFullName、...)。

if ($folder.LastWriteTime -lt $today) {
    $folderPath = $folder.FullName
    $folderName = $folder.Name
    aws s3 cp $folderPath s3://bucketname/$folderName --recursive 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2021-04-12
    • 2011-11-02
    • 2023-04-09
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多