使用 Apps 脚本,我需要删除特定 Google Cloud 项目的 Stackdriver 日志。这应该可以通过 Apps 脚本和 Cloud Logging REST API 使用DELETE请求来实现。Apps ScriptUrlFetchApp.fetch(url,options)可以使用该DELETE方法。下面的代码返回响应代码:404。我之前得到响应代码:400,但现在它正在接受 URL 结构,但它就是找不到 URL。如果 URL 需要日志 ID,那么我不确定从何处获取日志 ID。我想删除项目中的所有日志,而不仅仅是一个特定的。
错误信息:
The requested URL was not found on this server.
代码:
function deleteStackDriverLogs(po) {
var httpRz,options,url;
/*
po.id = GCP project ID - https://console.cloud.google.com/home/dashboard
*/
options = {};
options.method = "delete";
options.muteHttpExceptions = true;
options.headers = {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()};
options.contentType = "application/json";
url = 'https://logging.googleapis.com/v2/';
options.payload = JSON.stringify({
logName: 'projects/' + po.id + "/logs/*"
});
httpRz = UrlFetchApp.fetch(url,options);
Logger.log('response code: ' + httpRz.getResponseCode());
//Logger.log('httpRz.getAllHeaders(): ' + JSON.stringify(httpRz.getAllHeaders()))
//Logger.log(httpRz.getContentText())
}
function testDlet() {
deleteStackDriverLogs({"id":"project-id-your-GCP-id-here"});
}
文档: https ://cloud.google.com/logging/docs/reference/v2/rest/v2/logs/delete
如果只使用没有有效负载的 URL,那么我会得到响应代码 404,没有任何解释。
我已经尝试了许多不同的 url。
url = 'https://logging.googleapis.com/v2/logName={projects/' + po.id + '}';//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/';//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id;//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/logs/*/';//400
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/logs/';//404
文档声明日志 ID 必须是 URL 编码的。但是,我不确定日志 ID 使用什么。