aws - CloudFront createInvalidation() 方法参数在使用 promises 时出错

aws - CloudFront createInvalidation() method arguments error when using promises

适用于 JavaScript 的 AWS SDK 允许在调用 AWS 服务 classes 的方法时使用承诺而不是回调。以下是 S3 的示例。 (我正在使用 TypeScript 和无服务器框架进行开发)

const s3 = new S3({ apiVersion: '2006-03-01' });

async function putFiles () {
      await s3.putObject({
        Bucket: 'my-bucket',
        Key: `test.js`,
        Body: Buffer.from(file, 'binary') // assume that the file variable was defined above.
      }).promise();
}

上面的函数工作得很好,我们将桶参数作为方法的唯一参数传递。

但是,当我尝试通过调用 AWS CloudFront class 上的 createInvalidation() 方法来执行类似操作时,它给我一个错误,指出参数不匹配。

以下是我的代码和我得到的错误。

const cloudfront = new aws.CloudFront();

async function invalidateFiles() {
      await this.cloudfront.createInvalidation({
        DistributionId: 'xxxxxxxxxxx',
        InvalidationBatch: {
          Paths: {
            Quantity: 1,
            Items: [`test.js`],
          },
        },
      }).promise();
}

有人可以帮忙解决这个问题吗?

您没有将 CallerReference 作为参数传递。

const cloudfront = new aws.CloudFront();
async function invalidateFiles() {
    await cloudfront.createInvalidation({
        DistributionId: 'xxxxxxxxxxx',
        InvalidationBatch: {
            CallerReference: `SOME-UNIQUE-STRING-${new Date().getTime()}`,
            Paths: {
                Quantity: 1,
                Items: ['test.js'],
            },
        },
    }).promise();
}