How to fix MalformedXML error when invoking PutBucketTaggingCommand?

0

When invoking 'PutBucketTaggingCommand', I get a 'MalformedXML' error. The code is as follows:

const s3Client = new S3Client({ credentials, region })
const putBucketTaggingCommand = new PutBucketTaggingCommand({
      Bucket  : lambdaFunctionsBucket,
      Tagging : {
        TagSet : [{ Key : siteTag, Value : '' }]
      }
    })
    await s3Client.send(putBucketTaggingCommand)
  • SDK: @aws-sdk/client-s3@3.536.0 (Javascript V3)
  • effective role has 's3:*' privileges
  • lambdaFunctionsBucket has a valid bucket name (all letters, numbers, and hyphens, starts with letter)
  • siteTag has a value of 'site:liquid-labs.com'. (EDIT: actually, I think the siteTag was undefined an that was the problem.)
  • region has a value of 'us-east-1'

I have tried removing the region, using a 'Value' of '123', using a 'Value' of undefined. The entire error message is:

MalformedXML: The XML you provided was not well-formed or did not validate against our published schema
    at throwDefaultError (/Users/zane/playground/cloudsite/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /Users/zane/playground/cloudsite/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_CommandError (/Users/zane/playground/cloudsite/node_modules/@aws-sdk/client-s3/dist-cjs/index.js:4756:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/zane/playground/cloudsite/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /Users/zane/playground/cloudsite/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:225:18
    at async /Users/zane/playground/cloudsite/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /Users/zane/playground/cloudsite/node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/index.js:173:18
    at async /Users/zane/playground/cloudsite/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:97:20
    at async /Users/zane/playground/cloudsite/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:120:14 {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: 'F0C8TQP0WVF0A5BH',
    extendedRequestId: 'v06rEedH70pW3W73JSLntGwGUOULFqHh08KW1XtgrT9dH/mC03enm38bDGfZaABBsPrIc9sbj2c=',
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Code: 'MalformedXML',
  RequestId: 'F0C8TQP0WVF0A5BH',
  HostId: 'v06rEedH70pW3W73JSLntGwGUOULFqHh08KW1XtgrT9dH/mC03enm38bDGfZaABBsPrIc9sbj2c='
}
asked a month ago147 views
3 Answers
0

You have ":". You want "=". Take a look at https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/put-bucket-tagging.html where you will see the example: TagSet=[{Key=string,Value=string},{Key=string,Value=string}]

So yours should be: const putBucketTaggingCommand = new PutBucketTaggingCommand({ Bucket : lambdaFunctionsBucket, Tagging : { TagSet=[{Key=siteTag,Value=''}] } })

AWS
evaleah
answered a month ago
  • Thanks for the response, but I don't think that's right. I'm using Javascript SDK v3, and what you suggest would be invalid Javascript syntax.

0

It looks like the issue was a the key variable (siteTag) was undefined. It would be nice to have a more helpful error message, but there it is. (I'd printed the value, but then changed the implementation and introduced the bug.)

answered a month ago
0

From the details you've provided, a couple of things stand out:

  • Undefined siteTag Value: You mentioned that siteTag was possibly undefined at the time of the operation. This could certainly lead to a malformed request, as S3 expects each tag in the TagSet to have both a Key and a Value. An undefined Key or Value would result in invalid XML being generated by the SDK.
  • XML Validation: The error message specifically mentions that the XML was not well-formed or did not validate against S3's published schema. This further supports the idea that the issue is related to the content of the TagSet. Given this, here are a few steps you can take to troubleshoot and potentially fix the issue:
  • Ensure Defined Tags: Make sure that both Key and Value for each tag in the TagSet are defined and not empty strings. If you intend to have an empty value for a tag, ensure that the Value is explicitly set to an empty string ('') and not undefined.
  • Check Tag Format: Verify that the format of your tags complies with S3's restrictions. For example, tag keys and values have certain allowed characters and size limitations.
  • Log the Request: Temporarily add logging to your code to print the final putBucketTaggingCommand object just before it's sent. This can help you visually inspect the request to ensure it's structured correctly.
  • Minimal Example: Try creating a minimal example with hardcoded values for Bucket, Key, and Value that you know are valid. If this works, then gradually reintroduce the dynamic aspects of your code to identify where the issue lies.
  • SDK Documentation: Double-check the SDK documentation for PutBucketTaggingCommand to ensure that all parameters are being used correctly and that there haven't been any recent changes to the API or the way it's called.
profile picture
EXPERT
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions