I'm trying replicate the answer in downloading-from-s3-with-aws-cli-using-filter-on-specific-prefix using PowerShell. I tried using the method in whats-the-equivalent-of-xargs-in-powershell, and have gotten close to getting the key name from S3 ls and passing it to a call command.
,@(Get-ChildItem -recurse | aws s3 ls s3://s3-bucket-name/year_2022/ABCDEFT-8D) | Select-Object -ExpandProperty syncroot | Select-Object @{Name='KeyName';Expression={$_.split(" ")[-1]}} | %{&"aws" s3 cp s3://s3-bucket-name/year_2022/$_ .}
Although it iterates and gets all the key names in Select-Object but in foreach it ends up passing
s3://s3-bucket-name/year_2022/@{KeyName=ABCDEFT-8D-FILE1.XML}
s3://s3-bucket-name/year_2022/@{KeyName=ABCDEFT-8D-FILE2.XML}
And I get the key not found error. After searching around I tried replacing $_ with @_ but doing that passes the argument as s3://s3-bucket-name/year_2022/@_
I have no experience in powershell and made it this far by just searching existing answers, but I'm not able to figure this one out.
For reference, if I run the powershell code without the call
,@(Get-ChildItem -recurse | aws s3 ls s3://s3-bucket-name/year_2022/ABCDEFT-8D) | Select-Object -ExpandProperty syncroot | Select-Object @{Name='KeyName';Expression={$_.split(" ")[-1]}}
I get this:
KeyName
ABCDEFT-8D-FILE1.XML
ABCDEFT-8D-FILE2.XML
...
From what I could figure out, the output above is a hashtable and so I thought that @_ should work, but doesn't.
The powershell version is 7.2.5
CodePudding user response:
I think instead of select-object creating an object, you want foreach-object outputting a string:
# return the last word
'one two three' | Foreach-Object { $_.split(" ")[-1] }
three