GCP IoT Error on sending new device configuration:

2019-08-24 12:35发布

问题:

I'm trying to send my GCP IoT device a new configuration. The Base64-encoded binary string is approximately 15k bytes in length. GCP IoT device config size limit is 64k according to the docs. But I'm still getting a 413 (request entity too large) error. What am I doing wrong? 15k seems incredibly tiny to be producing such an error. Thanks for any help.

Here's the JavaScript code that sends the config data:

  sendDeviceConfig(deviceId, configPayload) {
    const parentName = `projects/${this.projectId}/locations/${this.cloudRegion}`;
    const registryName = `${parentName}/registries/${this.registryId}`;

    const binaryData = Buffer.from(configPayload).toString('base64');
    const request = {
      name: `${registryName}/devices/${deviceId}`,
      versionToUpdate: 0,
      binaryData: binaryData,
    };

    return new Promise((resolve, reject)=>{
      this.client.projects.locations.registries.devices.modifyCloudToDeviceConfig(
        request,
        (err) => {
          if (err) {
            this.logger.error('Could not update config:', deviceId);
            reject(err);
          } else {
            resolve();
          }
        }
      );
    });
  }

... And part of the HTML formatted (wtf?) error response:

<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 413 (Request Entity Too Large)!!1</title>
  ...
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>413.</b> <ins>That’s an error.</ins>
  <p>Your client issued a request that was too large.

回答1:

I think the payload will be ~ 22.4kb with base64 encoding.

However, if the config is over 16kb and put in the request header, google will return a 413. It should be in the body of a Post.



回答2:

It looks like the legacy client library may be doing something weird, the following code, used as a drop-in for the sample code, works for me with larger configuration payloads:

  const iot = require('@google-cloud/iot');

  const newclient = new iot.v1.DeviceManagerClient({
    // optional auth parameters.
  });

  const parentName = `projects/${projectId}/locations/${cloudRegion}`;
  const registryName = `${parentName}/registries/${registryId}`;
  const binaryData = Buffer.from(data).toString('base64');
  const request = {
    name: `${registryName}/devices/${deviceId}`,
    binaryData: binaryData,
  };
  newclient.modifyCloudToDeviceConfig(request)
    .then(responses => {
      const response = responses[0];
      // doThingsWith(response)
    })
    .catch(err => {
      console.error(err);
    });