-1

What specific syntax needs to be changed in the aws s3api put-object-tagging --bucket bucketName --key fileName.tar.gz --tagging TagSet={Key=public,Value=yes} command to prevent the error shown below when the command is run in PowerShell?

Note that the aws s3api put-object-tagging --bucket bucketName --key fileName.tar.gz --tagging TagSet={Key=public,Value=yes} command syntax works perfectly when run in windows cmd on the very same computer.

Here is the PowerShell log including the command and the error message on the same computer where this command works in windows cmd:

PS C:\Users\userName> aws s3api put-object-tagging --bucket bucketName --key fileName.tar.gz --tagging TagSet={Key=public,Value=yes}
At line:1 char:129
+ ... --key fileName.tar.gz --tagging TagSet={Key=public,Value=ye ...
+                                                                 ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

PS C:\Users\userName>

Also note that I am new to PowerShell. I just sometimes now need to translate short scripts from other languages into PowerShell.

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • For options try one dash instead of two dashes. – jdweng Oct 12 '22 at 23:20
  • @jdweng, the [`aws` CLI](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/index.html) uses `--`, not `-` before parameter names. – mklement0 Oct 14 '22 at 02:49
  • To summarize: The only _shell syntax_ problem with the command in your question is that you cannot pass `TagSet={Key=public,Value=yes}` _unquoted_ in PowerShell: -> `'TagSet={Key=public,Value=yes}'`. If that didn't help, the implication is that the `aws` command didn't work to begin with. Indeed, the shortcut syntax you're trying to use seems to require `[` and `]`: -> `'TagSet=[{Key=public,Value=yes}]'` The fact that you managed to get _different_ `aws` syntax to work (a JSON string, the verbose alternative to the shorthand syntax) is not the answer to the question you asked. – mklement0 Oct 15 '22 at 20:33

1 Answers1

1

Update:

  • The answer below addresses your question as asked, based on your (seemingly incorrect) claim that the first command mentioned in your question worked as-is from cmd.exe. The points made below generally apply to the syntax of arguments passed to commands in PowerShell.

  • Based on a later comment, you state that what solved your problem was to pass a JSON string to the aws CLI's --tagging parameter:

    • You state that '{\"TagSet\": [{\"Key\":\"public\",\"Value\":\"yes\"}]}' worked for you (which represents the following verbatim JSON string:
      {"TagSet": [{"Key":"public","Value":"yes"}]).

      • Here too it is the quoting ('...') that makes the argument work, as discussed in the bottom section.
      • As an aside: This strange need to manually \-escape " characters embedded in an argument passed to external programs is indeed an - unfortunate - requirement in PowerShell up to at least v7.2.x, due to a long-standing bug. It may get fixed in a future version, which may require opt-in: see this answer.
    • However, apart from the PowerShell syntax problem that is the subject of your question, it looks like your original command simply used incorrect aws shorthand syntax (a shorter alternative to passing JSON): according to the docs for the put-object-tagging subcommand, the following should work - in addition to the required quoting ('...') for PowerShell's sake, note the [...] around {...} which were missing from your own original command:
      'TagSet=[{Key=public,Value=yes}]'


Replace:

TagSet={Key=public,Value=yes}

with:

'TagSet={Key=public,Value=yes}'

That is, quote your argument, which prevents PowerShell from interpreting {...} as a script block, which is what caused your problem.

Note the use of a '...', i.e a single-quoted verbatim string, which is the best choice if an argument contains no embedded variable references or subexpression (for the latter, you need string interpolation in the form of "...", i.e., a double-quoted expandable string).

PowerShell differs from other shells in two notable respects:

  • its escape character is ` (the so-called backtick), not \ (as in POSIX-compatible shells such as Bash) or ^ (as in cmd.exe)

  • it has more metacharacters than other shells, such as {, }, and @


As for the specific error message you saw:

The content of a script block ({ ... }) is parsed as PowerShell code, and the content in your case, Key=public,Value=yes, is parsed as a command, whose syntax happens to be invalid:

  • Key=public is interpreted as a command name

  • ,Value=yes is parsed as its argument, which - due to the , - is parsed as an array, but a literal array passed as an argument must not start with ,, which is what the error message is trying to say: it's missing an array element before ,

  • You can more easily provoke this error by submitting the following on the command line:

    foo , bar # -> Error "Missing argument in parameter list."
    
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • The syntax that worked to solve the problem in the OP was actually the following instead: `aws s3api put-object-tagging --bucket "myBucketName" --key "nameOfObject" --tagging '{\"TagSet\": [{\"Key\":\"public\",\"Value\":\"yes\"}]}'` – CodeMed Oct 13 '22 at 23:33