Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyudu/e1786b9dff1533feb8ebfaa9a90e9379 to your computer and use it in GitHub Desktop.
Save banyudu/e1786b9dff1533feb8ebfaa9a90e9379 to your computer and use it in GitHub Desktop.
Serverless跳过腾讯云的方法

Serverless跳过腾讯云的方法

自从腾讯云和Serverless Components达成战略合作之后,国内用Serverless命令行工具就到处都是腾讯云的身影了。

由于目前我的服务还是部署在AWS上的,所以这个事情对我来说多多少少地有些困扰,把系统语言换成英文也没好使,所以特意查了下Serverless中判断是腾讯云用户还是AWS用户的方法:

关键代码如下:

/**
 * Detect if the user is located in China by looking at their settings
 */
const isChinaUser = () => {
  let result;
  if (
    process.env.SERVERLESS_PLATFORM_VENDOR === 'tencent' ||
    process.env.SLS_GEO_LOCATION === 'cn'
  ) {
    result = true;
  } else if (process.env.SERVERLESS_PLATFORM_VENDOR === 'aws') {
    result = false;
  } else {
    result = new Intl.DateTimeFormat('en', { timeZoneName: 'long' })
      .format()
      .includes('China Standard Time');
  }

  return result;
};

从代码中可以看出,由于我当前用的是中国时区,所以被判断成了中国用户,就自动转到腾讯云相关服务中了。

解决办法也很简单,在环境变量中加一个 SERVERLESS_PLATFORM_VENDOR,值为'aws'就好了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment