【问题标题】:Setting label on Google Compute Engine instance disk在 Google Compute Engine 实例磁盘上设置标签
【发布时间】:2019-06-11 08:55:44
【问题描述】:

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

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

不幸的是也使用了谷歌提供的简单代码:

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: { "error": { "errors": [ { "domain": "global", "reason": "conditionNotMet", "message": "标签指纹无效或资源标签已更改", “locationType”:“header”,“location”:“If-Match”}],“code”:412,“message”:“标签指纹无效或资源标签已更改”}}

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

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

但没有任何工作。并且什么都没有改变(总是 500 错误,同样的例外)。我做错了什么?

【问题讨论】:

    标签: google-cloud-platform


    【解决方案1】:

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

    {
      "labels": {
        string: string,
        ...
      },
      "labelFingerprint": string
    }
    

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

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

    base64 编码的字符串。

    【讨论】:

    • 感谢您的反馈。你让我找到了解决问题的正确方向。
    • 太好了,很高兴我能帮上忙。喜欢的可以采纳答案!
    • 当然。对不起,我之前忘了做!
    猜你喜欢
    • 2020-08-28
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多