Google计算引擎.NET API示例/示例/教程

时间:2014-09-10 16:02:15

标签: google-compute-engine google-api-dotnet-client

我无法通过.net API(特别是c#)找到能够清楚解释如何使用谷歌计算引擎的任何内容。有没有人可以指点我什么?

P.S。我知道API参考(https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html

2 个答案:

答案 0 :(得分:1)

我找不到任何包含代码示例的详细教程,但[1]中提供的官方文档包含代码示例。

在[2]上有一个针对Google云端硬盘的C#示例教程。

供参考API文档可在[3]中找到,带注释的库参考可在[4]获得。

链接: [1] - https://developers.google.com/api-client-library/dotnet/get_started#examples
[2] - http://conficient.wordpress.com/2014/06/18/using-google-drive-api-with-c-part-1/
[3] - https://developers.google.com/compute/docs/reference/latest/
[4] - https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html

答案 1 :(得分:0)

您需要遵循的步骤列表: 具体来说,您可以修改并使用以下代码来创建Google Compute Engine的vmInstance 以下是可以创建实例的c#(使用Google api SDK)功能

UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = "ClientId",
            ClientSecret = "ClientSecret"
        },
         new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform },
        "user",
         CancellationToken.None, null);



`ComputeService service = new ComputeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "ApplicationName",
            ApiKey = "ApiKey"
        });


 public IEnumerable<CreateInstanceResult> CreateInstances(params CreateInstanceRequest[] instances)
    {
        IList<Instance> vmInstances = new List<Instance>();
        ComputeService service = assign GoogleComputeServiceObject;

        if (instances != null)
        {
            foreach (CreateInstanceRequest requestInstance in instances)
            {
                #region Meatadata Setting

                Metadata metaData = new Metadata();
                metaData.Items = new List<Metadata.ItemsData>();
                Metadata.ItemsData itemData = new Metadata.ItemsData();
                itemData.Key = "Expiration";
                itemData.Value = requestInstance.Expiration.ToString();
                metaData.Items.Add(itemData);
                itemData = new Metadata.ItemsData();
                itemData.Key = "AccountId";
                itemData.Value = requestInstance.AccountId;
                metaData.Items.Add(itemData);

                if (requestInstance.Data != null)
                {
                    foreach (KeyValuePair<string, string> keyValue in requestInstance.Data)
                    {
                        Metadata.ItemsData otherItemData = new Metadata.ItemsData();
                        otherItemData.Key = keyValue.Key;
                        otherItemData.Value = keyValue.Value;
                        metaData.Items.Add(otherItemData);
                    }
                }
                #endregion Meatadata Setting

                #region DiskSetting

                IList<AttachedDisk> attachedDisks = new List<AttachedDisk>();
                AttachedDisk attachedDisk = new AttachedDisk();
                AttachedDiskInitializeParams attachedDiskInitializeParams = new AttachedDiskInitializeParams();
                attachedDiskInitializeParams.DiskSizeGb = googleCloudServerSetting.DiskSize;
                attachedDiskInitializeParams.DiskType = service.BaseUri + "Your_ProjectId" + "/zones/" + "specifyZone" + "/diskTypes/" + "specify_DiskType";

                // for example
                attachedDiskInitializeParams.SourceImage = service.BaseUri + "/debian-cloud/global/images/specify_imagesourceImage";


                attachedDisk.AutoDelete = true;
                attachedDisk.Boot = true;
                attachedDisk.Interface__ = "SCSI";//for example
                attachedDisk.InitializeParams = attachedDiskInitializeParams;
                attachedDisks.Add(attachedDisk);

                IList<NetworkInterface> networkInterfaces = new List<NetworkInterface>();
                NetworkInterface networkInterface = new NetworkInterface();
                networkInterface.Network = service.BaseUri + ProjectId + "/global/networks/default";

                networkInterfaces.Add(networkInterface);

                Tags tags = new Tags();
                IList<string> stringList = new List<string>();

                tags.Items = new List<string>();
                tags.Items.Add("http-server");
                tags.Items.Add("https-server");

                #endregion DiskSetting

                #region Creating Instance object

                Instance instance = new Instance()
                {
                    MachineType = requestInstance.SizeId ?? service.BaseUri + "ProjectId" + "/zones/" + "specify_Zone" + "/machineTypes/" + "specify_machineType",
                    Metadata = metaData,
                    Name = "InstanceName",
                    Tags = tags,
                    NetworkInterfaces = networkInterfaces,
                    Disks = attachedDisks
                };
                #endregion Creating Instance object

                vmInstances.Add(instance);
            }

       var batchRequest = new BatchRequest(service);

        foreach (Instance instance in instances)
        {
            batchRequest.Queue<Instance>(service.Instances.Insert(instance, ProjectId, Zone),
                 (content, error, i, message) =>
                 {

                 });
        }
        await batchRequest.ExecuteAsync();

        }
        else
        {
            throw new Exception("null");
        }
    }