0

I am trying to execute aws s3 cp command in Powershell. I have a list of files which I defined in an array-like below.

$filearray = ("file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt")

Now when I am trying to copy these files from s3 using aws s3 cp command nothing works.

$filearray | foreach { "aws s3 cp s3://<bucket_name/path_to_file/$_ . --sse aws:kms --sse-kms-key-id 12345-abcxzd" }

I tried below as well. But it didn't work. Can someone point out the mistake I am doing?

foreach ($element in $filearray) { "aws s3 cp s3://<bucket_name/path_to_file/$element . --sse aws:kms --sse-kms-key-id 12345-abcxzd" }

Now I have added variables to my aws command also which will be like below when the loop executes.

$kms_key_option="--sse aws:kms --sse-kms-key-id 12345-abcxzd" $s3_location="s3://<bucket_name/path_to_file/"

foreach ($element in $filearray) { & aws s3 cp $s3_location/$element $current_location $kms_key_option }

But there is a catch in this. Only the first 2 variables in command block work as variables rest 2 do not work as variables. i.e. $current_location $kms_key_option So the problem statement is changing here a little bit.

Note: I have aws cli installed on my windows machine it works with normal command but it doesn't work in foreach loop.

2 Answers 2

2

You are looking for the & operator.

foreach ($element in $filearray) { & "aws s3 cp s3://<bucket_name/path_to_file/$($element) . --sse aws:kms --sse-kms-key-id 12345-abcxzd" }
Sign up to request clarification or add additional context in comments.

2 Comments

This works somewhat but throws CommandNotFoundException in my case for aws s3 cp command.
I was able to get it working after resolving the CommandNotFoundException
0

I'd say try it without the Quotes in the foreach loops?

foreach ($element in $filearray) {
    aws s3 cp s3://<bucket_name/path_to_file/$element . --sse aws:kms --sse-kms-key-id 12345-abcxzd
}

1 Comment

I tried this but it didn't work. Though the command execution is successful it does not achieve copy operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.