在Google Compute Engine实例磁盘上设置标签

时间:2019-06-11 08:55:44

标签: google-cloud-platform

我将尝试在Google Compute Engine实例上设置磁盘标签。基本上这里记录了什么:

https://cloud.google.com/compute/docs/reference/rest/v1/disks/setLabels

不幸的是,还使用了Google提供的简单代码:

require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('Google-ComputeSample/0.1');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Compute($client);
$project = 'my-project';
$zone = 'my-zone';
$resource = 'my-resource';  // here i set the disk name
$requestBody = new Google_Service_Compute_ZoneSetLabelsRequest();
$response = $service->disks->setLabels($project, $zone, $resource, $requestBody);
echo '<pre>', var_export($response, true), '</pre>', "\n";
?>

我总是遇到500错误:

  

未捕获的Google_Service_Exception:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“ conditionNotMet”,“消息”:“标签指纹无效或资源标签已更改”, “ locationType”:“标头”,“ location”:“如果匹配”}],“代码”:412,“ message”:“标签指纹无效或资源标签已更改”“}}

我认为我的标签语法错误。但是在标签方法中,我尝试了几种语法:

$requestBody->setLabels(array("mylabel"=>"1"));
$requestBody->setLabels(serialize(array("mylabel"=>"1")));
$requestBody->setLabels('"mylabel":"1"');
$requestBody->setLabels('{"mylabel":"1"}');

,但无济于事。并没有任何改变(总是500错误,同样的异常)。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您收到的错误响应表明labelFingerprint错误或未设置。请求主体应同时包含标签和labelFingerprint,看起来您只在设置前者:

user

您链接的文档解释了lableFingerprint是什么:

  

此资源的上一组标签的指纹,用于检测冲突。指纹最初由Compute Engine生成,并在每次修改或更新标签的请求后更改。您必须始终提供最新的指纹哈希,才能更新或更改标签。对资源进行get()请求以获取最新的指纹。

     

base64编码的字符串。

相关问题