gce启动实例python api使用示例指南

gce启动实例python api使用示例指南,python,api,google-cloud-platform,createinstance,google-cloud-python,Python,Api,Google Cloud Platform,Createinstance,Google Cloud Python,我试图复制谷歌网站上的以下指南 我想不出这个桶是什么意思。我需要将什么样的参数传递到脚本中? 我应该在getFromFamily里放什么( project='debian-cloud',family='debian-8').execute()是否要启动我的私有映像? 我是否像下面这样传入变量 create_instance(compute,projectname, us-east1-a, instance-1, bucket) 我将脚本简化如下。我去掉了我认为不需要的参数 def creat

我试图复制谷歌网站上的以下指南

我想不出这个桶是什么意思。我需要将什么样的参数传递到脚本中? 我应该在getFromFamily里放什么( project='debian-cloud',family='debian-8').execute()是否要启动我的私有映像? 我是否像下面这样传入变量

create_instance(compute,projectname, us-east1-a, instance-1, bucket)
我将脚本简化如下。我去掉了我认为不需要的参数

def create_instance(compute, project, zone, name, bucket):

image_response = compute.images().getFromFamily(
    project='debian-cloud', family='debian-8').execute()
source_disk_image = image_response['selfLink']

# Configure the machine
machine_type = "us-east1-b/%s/machineTypes/f1-micro" % zone
startup_script = open(
    os.path.join(
        os.path.dirname(__file__), 'startup-script.sh'), 'r').read()

config = {
    'name': instance-1,
    'machineType': f1-micro,

    # Specify the boot disk and the image to use as a source.
    'disks': [
        {
            'boot': True,
            'autoDelete': True,
            'initializeParams': {
                'sourceImage': /global/imagename,
            }
        }
    ],

    # Specify a network interface with NAT to access the public
    # internet.
    'networkInterfaces': [{
        'network': 'global/networks/default',
        'accessConfigs': [
            {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
        ]
    }],

    # Allow the instance to access cloud storage and logging.
    'serviceAccounts': [{
        'email': 'default',
        'scopes': [
            'https://www.googleapis.com/auth/devstorage.read_write',

        ]
    }],

    # Metadata is readable from the instance and allows you to
    # pass configuration from deployment scripts to instances.
    'metadata': {
        'items': [{
            # Startup script is automatically executed by the
            # instance upon startup.
            'key': 'startup-script',
            'value': startup_script
        },  {
            'key': 'bucket',
            'value': bucket
        }]
    }
}

return compute.instances().insert(
    project=project,
    zone=zone,
    body=config).execute()

这已经有一段时间了,但这里有一些信息

只有当您希望实例具有共享存储位置(doc)时,才需要
存储桶。如果不需要bucket,您应该删除元数据
bucket
键值(尽管我惊讶地发现创建一个具有假bucket名称的实例不会失败)

compute
是经过身份验证的客户端对象,用于与“计算引擎”服务交互。要获得客户机,您需要遵循以下步骤获得密钥、密钥和刷新令牌。下面是一个代码示例:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from oauth2client import GOOGLE_TOKEN_URI
credentials = GoogleCredentials(access_token=None, client_id='your-client-id', 
                                client_secret='your-client-secret',
                                refresh_token='your-refresh-token',
                                token_expiry=None, token_uri=GOOGLE_TOKEN_URI,
                                user_agent='Python client library')
compute = discovery.build('compute', 'v1', credentials=credentials)
至于私人图像,如果您想使用相同的代码,那么您需要在您的GCE中定位和识别它们,并提供
项目
系列
标识符。您还可以使用API按名称获取图像:


image=compute.images().get(project='your-project-id',image='your-image-name').execute()
,其中相同的
image['selfLink']
应作为实例
配置中的
sourceImage
提供。该示例已过时:注意,oauth2client现在已被弃用,而Google Auth是当前的客户端库。这里有一个更新的解决方案。关键步骤是生成正确的更新json。这可以通过在授权服务帐户下使用等效的REST命令转换您的预期实例来实现

导入google.auth
导入GoogleAppClient.discovery
项目ID=“”
ZONE=“”
凭据,uz=google.auth.default()
compute=googleapiclient.discovery.build('compute','v1',',
凭证=凭证)
config=“{从等效REST生成实例的json}”
compute.instances().insert(project=project\u ID,zone=zone,
body=config.execute()
import google.auth
import googleapiclient.discovery
PROJECT_ID = "<your project ID>"
ZONE = "<your zone>"

credentials, _ = google.auth.default()
compute = googleapiclient.discovery.build('compute', 'v1', 
credentials=credentials)

config = "{generate your instance's json from equivalent REST}"

compute.instances().insert(project=PROJECT_ID, zone=ZONE, 
body=config).execute()