AWS-SDK (lightsail API) vue.js CORS 问题

AWS-SDK (lightsail API) vue.js CORS issues

我正在尝试构建一个使用 aws-sdk 获取所有 Lightsial 实例的小型 vue.js 应用程序。但是,我一直收到此错误。

:8081/#/:1 Access to XMLHttpRequest at 'https://lightsail.us-west-2.amazonaws.com/' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我的 vue 组件脚本

import { AmplifyEventBus } from 'aws-amplify-vue'
import { components } from 'aws-amplify-vue'
import AWS from 'aws-sdk'
import Lightsail from 'aws-sdk/clients/lightsail'
import Auth from '@aws-amplify/auth';

import awsconfig from '../aws-exports';


export default {
components: {
    AWS, Lightsail
  },
  data() {
    return {
     
    }
  },
  mounted() {

  var myCredentials = 
    {
      accessKeyId : '***************',
      secretAccessKey : '****************'
    }

  AWS.config.update({
    credentials: myCredentials, region: 'us-west-2'
  });

    var lightsail = new AWS.Lightsail();
    lightsail.getInstances(function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
    });




  }

}

我也在这个应用程序中使用 aws-amplify-vue 进行用户身份验证

根据我的研究,应该是CORS的问题。这是一个类似的线程,其中提到了一些解决方法。你能去看看它是否适用于你的场景吗?

Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?

这仅仅是因为当前版本的 <aws-sdk-js> 不允许 Lightsail API.

的 CORS

official document说的很清楚。

编辑: 另一个用户写的完全错误的答案在我添加评论后已被删除。所以我从我的回答中删除了"Right answer"这个词。感谢 your 快速行动。