首页 > 解决方案 > 如何使用文件夹的最后修改日期将文件上传到 S3?

问题描述

我正在尝试组合一个 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 
   }
}

但是,上面给了我错误:

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

任何帮助,将不胜感激。

标签: amazon-web-servicespowershellamazon-s3amazon-ec2scripting

解决方案


一方面,$folderaws您可能打算使用$folderPath. 此外,扩展的Get-ChildItem不一致。根据上下文,对象有时会扩展为名称,有时会扩展为完整路径(有关更详细的说明,请参见此处)。因此,明确使用给定场景所需的属性(Name, FullName, ...)被认为是一种很好的做法。

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

推荐阅读