GCP-php(google云)调用示例

记录一下GCP调用

首先需要

composer require google/apiclient:^2.0
然后直接上php代码
namespace App\Services;
use Google_Client;
use Google_Service_Compute_Instance;
use Google_Service_Compute;
use Google_Service_Compute_AttachedDisk;
use Google_Service_Compute_AttachedDiskInitializeParams;
use Google_Service_Compute_NetworkInterface;
use Google_Service_Compute_AccessConfig;

class GoogleService
{
    /**
     * @var Google_Client
     */
    private $client;

    /**
     * 填自己的projectID
     */
    private $project='xxxx-184506';

    public function __construct(){
        //以下主要是验证与授权,具体查询gcp文档
        $client = new Google_Client();
        //填json格式证书路径,具体查询gcp文档
        $path='xxxx-76d3a96430ed.json';
        putenv('GOOGLE_APPLICATION_CREDENTIALS='.$path);
        $client->useApplicationDefaultCredentials();
        $client->addScope('https://www.googleapis.com/auth/cloud-platform');

        $this->client=$client;
    }


    /**
     * 创建实例
     * @param string 实例名前缀
     * @param string zoneID 具体查gcp
     */
    public function createInstance($instanceNamePrefix,$typeZoneId){

        $service = new Google_Service_Compute($this->client);

        $requestBody = new Google_Service_Compute_Instance();
        if(isDevelopment()){
            //设置实例名,生产环境与开发环境名称不同,便于分辨
            $requestBody->setName($instanceNamePrefix."-dev-".date('YmdHis',time()).random_int(1000,9999));
        }else{
            //设置实例名
            $requestBody->setName($instanceNamePrefix."-prod-".date('YmdHis',time()).random_int(1000,9999));
        }
        //设置实例配置,这里用了最便宜的配置
        $requestBody->setMachineType("zones/{$typeZoneId}/machineTypes/f1-micro");

        $diskInitializeParams=new Google_Service_Compute_AttachedDiskInitializeParams();
        //设置镜像,生产环境与开发环境环境不同,从config从获取
        $diskInitializeParams->setSourceImage(config('app.gcp_source_image'));
        //设置硬盘类型
        $diskInitializeParams->setDiskType("zones/{$typeZoneId}/diskTypes/pd-standard");
        //设置硬盘大小
        $diskInitializeParams->setDiskSizeGb(10);

        $disk=new Google_Service_Compute_AttachedDisk();
        $disk->setInitializeParams($diskInitializeParams);
        //设置为启动盘
        $disk->setBoot(true);
        //设置删除实例时自动删除硬盘
        $disk->setAutoDelete(true);

        $requestBody->setDisks([$disk]);


        //以下的网络配置都是默认设置
        $accessConfig=new Google_Service_Compute_AccessConfig();
        $accessConfig->setName('External NAT');
        $accessConfig->setType('ONE_TO_ONE_NAT');

        $networkInterface=new Google_Service_Compute_NetworkInterface();
        $networkInterface->setNetwork('global/networks/default');
        $networkInterface->setAccessConfigs([$accessConfig]);

        $requestBody->setNetworkInterfaces([$networkInterface]);


        $service->instances->insert($this->project, $typeZoneId, $requestBody);
    }

    /**
     * 销毁实例
     */
    public function destroyInstance($zoneId,$instanceName){
        $service = new Google_Service_Compute($this->client);

        $service->instances->delete($this->project, $zoneId, $instanceName);

    }

    /**
     * 重启实例
     */
    public function reset($zoneId,$instanceName){
        $service = new Google_Service_Compute($this->client);

        $service->instances->reset($this->project, $zoneId,$instanceName);
    }

}

猜你喜欢

转载自blog.csdn.net/a459909293/article/details/79391310
gcp
今日推荐