问题描述
我刚刚使用Google Cloud创建了一个虚拟实例,但是我有三个项目,并且将实例分配给了错误的项目.看来这确实是个错误,但我如何确定问题出在Google而不是我.另外,不可避免地会有人建议我使用云控制台来创建实例,因此,让我优先考虑该建议.我试图将我所有的云计算都简化为一个按钮的单击.我计划定期创建和销毁实例,并且我必须能够快速有效地做到这一点.因此,我所有的云计算都必须使用Python完成.无论如何,我有两个项目:"move_files"和"9920"(我没有写名字).我使用以下python语法:
I just created a virtual instance using Google Cloud but I have three projects and it assigned the instance to the wrong project. It really looks like this is a bug but how I can I be sure that the fault lies with Google and not me. Also, someone is inevitably going to advise me to use the cloud console to create instances so let me preempt that advice. I'm trying to get all of my cloud computing down to the click of one button. I plan on regularly creating and destroying instances and I have to be able to do this quickly and efficiently. Hence, all of my cloud computing has to be done with Python. In any case, I have two projects: 'move_files', and '9920' (I didn't make up the names). I used the following python syntax:
import subprocess
str1 = "/users/kylefoley/codes/move.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str1
def create_instance(name='', machine_type=''):
name = 'kfoley76'
machine_type = 'n1-standard-1'
subprocess.run(['gcloud', 'compute', 'instances', 'create',
name, f'--machine-type={machine_type}',
'--zone=us-west2-a'])
create_instance()
该代码导致在'atomic'项目而不是'move'项目上创建了一个实例,但我的Google应用程序凭据另有明确说明.我唯一能想到的是,我以某种方式不正确地命名了json
文件.但是我要如何验证呢?
That code resulted in an instance being created on the 'atomic' project rather than the 'move' project and yet my google application credentials clearly stated otherwise. The only thing I can think of is that somehow I named the json
file incorrectly. But how would I verify that?
此外,让我提供证明错误的项目ID容纳了实例的证据.我想我不能将名字保密,但我想除非有人输入我的密码,否则它们就不会被盗.原子项目真的叫9920:
Also, let me provide proof that the wrong project ID houses the instances. I guess I won't be able to keep the names secret but I suppose that they cannot be stolen unless someone has my password. The atomic project is really called 9920:
推荐答案
与使用Python进行gcloud
调用相比,我建议使用另一种(更好)的方法.尽管您当前的方法行得通,但它比必要的方法更为复杂,使错误处理更具挑战性,并且-如您所见-可能导致意外的行为.在这种情况下,因为您要调用gcloud
,所以您还将继承其配置.
I recommend a different (better) approach than using Python to make gcloud
calls. While your current approach will work, it's more complex than necessary, makes error-handling more challenging and -- as you've seen -- can result in unexpected behavior. In this case, because you're calling out to gcloud
, you're also inheriting its configuration.
替代方法是使用 Google的Python SDK .可通过Google提供的SDK访问所有Google的服务(例如Compute Engine).有2种主要的SDK.较旧的API客户端库和较新的(仅适用于[某些]云服务[不幸的是,不适用于Compute Engine])云客户端库.这是Google的解释器.
The alternative is to use Google's Python SDK. All Google's services (e.g. Compute Engine) are accessible via Google-provided SDKs. There are 2 primary flavors of SDK. The older, API Client Libraries and the newer (and only for [some] Cloud services [and unfortunately not Compute Engine]) Cloud Client Libraries. Here's Google's explainer.
NB gcloud
本身是用Python编写的,并使用API客户端库.
NB gcloud
is itself written in Python and uses the API Client Libraries.
Google的API文档非常出色.每个服务(不仅是Cloud)和每个方法都被完整(正确地|自动地)记录在案.这是Compute Engine的执行个体插入方法: https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
Google's API documentation is excellent. Every service (not just Cloud) and every method is fully (and correctly|automatically) documented. Here's Compute Engine's Instance Insert method:https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
注意事项:该页面还显示了Google的API Explorer,可让您直接进行API调用以观察行为.
NB The page also presents Google's APIs Explorer which enables you to make the API call directly to observe the behavior.
如果向下滚动此页面,您将找到Google支持的每种语言的示例: https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert#examples
If you scroll down this page, you will find example for each of the languages supported by Google:https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert#examples
也请参见此页面的示例: https://cloud.google.com/compute/docs/tutorials/python-指南
See this page for the example too:https://cloud.google.com/compute/docs/tutorials/python-guide
NB :直接使用(任何一种)API时,必须明确说明要使用的项目.这是因为gcloud
允许使用默认值,例如gcloud config set project
,但API本身是无状态的,并且不是.
NB When you use (any of) the APIs directly, you must explicitly state which project to use. This is because, gcloud
permits defaults e.g. gcloud config set project
but the APIs themselves are stateless and do not.
NB 我个人的建议是不要使用gcloud config set ...
,因为这可能会导致类似的问题.我认为最好总是明确引用例如使用gcloud
命令使用例如gcloud ... --project=${PROJECT} ...
NB My personal recommendation is to never use gcloud config set ...
as this can result in problems like this. I think it's better to always explicitly reference e.g. the project, with gcloud
commands using e.g. gcloud ... --project=${PROJECT} ...
这篇关于如何验证我在Google Cloud中使用的是正确的项目ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!