缠丝猫 发表于 2023-1-13 09:41:47

初始化一个GCP项目并用gcloud访问操作

1 简介

谷歌云GCP(Google Cloud Platform)是由Google提供的云平台,还是为用户提供了许多免费的产品,还是可以尝试一下的。对于学习或者小项目,都可以使用。
2 创建一个新项目

要使用GCP,我们需要创建一个项目,它所有的资源都是在项目之下管理的:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092343590-1069840746.png
3 创建Service Account

在实际开发中,我们不能使用自己的账号在做操作,最好的方式是创建一个服务账号(Service Account),这应该也是所有云平台都推荐的方式。创建位置如下:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092343909-869949378.png
输入账号名字:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092344183-482089943.png
选择角色,为了方便,我直接选择Owner,会拥有所有权限,但实际应用肯定不能这样,要做好隔离:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092344552-630787413.png
4 创建密钥文件

对于Service Account,不是通过用户名密码来授权的,而是通过密钥文件,创建如下:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092344807-1370695444.png
选择新建一个密钥,并格式为json。创建后,会自动下载key文件。
5 设置gcloud SDK

Key文件拿到后,我们可以设置环境变量:GOOGLE_APPLICATION_CREDENTIALS:
$ export GOOGLE_APPLICATION_CREDENTIALS=/Users/larry/Software/google-cloud-sdk/pkslow-admin-for-all.json激活Service Account:
$ gcloud auth activate-service-account admin-for-all@pkslow.iam.gserviceaccount.com --key-file=${GOOGLE_APPLICATION_CREDENTIALS}设置SDK的项目ID:
$ gcloud config set project pkslow检查一下设置是否正确:
$ gcloud auth list
               Credentialed Accounts
ACTIVEACCOUNT
*       admin-for-all@pkslow.iam.gserviceaccount.com

To set the active account, run:
    $ gcloud config set account `ACCOUNT`


$ gcloud config list

account = admin-for-all@pkslow.iam.gserviceaccount.com
disable_usage_reporting = True
project = pkslow

Your active configuration is: 6 使用gcloud创建Pub/Sub

SDK设置好后,就可以使用了,我们使用它来创建Pub/Sub试试。创建主题和订阅:
$ gcloud pubsub topics create pkslow-test
Created topic .

$ gcloud pubsub subscriptions create pkslow-sub --topic=pkslow-test
Created subscription .检查是否创建成功:
$ gcloud pubsub topics list
---
name: projects/pkslow/topics/pkslow-test


$ gcloud pubsub subscriptions list
---
ackDeadlineSeconds: 10
expirationPolicy:
ttl: 2678400s
messageRetentionDuration: 604800s
name: projects/pkslow/subscriptions/pkslow-sub
pushConfig: {}
topic: projects/pkslow/topics/pkslow-test在浏览器查看,发现已经成功创建了:
https://img2023.cnblogs.com/other/946674/202301/946674-20230113092345070-731219349.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 初始化一个GCP项目并用gcloud访问操作