【问题标题】:Google cloud platform gcp delete snapshot 10 days older using python谷歌云平台 gcp 使用 python 删除 10 天前的快照
【发布时间】:2021-08-20 01:18:50
【问题描述】:

我想使用 python 删除 GCP 中 10 天前的快照。我尝试使用以下程序使用过滤器表达式,但不幸的是我遇到了以下错误

from datetime import datetime
from googleapiclient import discovery
import google.oauth2.credentials
from oauth2client.service_account import ServiceAccountCredentials
import sys
def get_disks(project,zone):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(r"D:\Users\ganeshb\Desktop\Json\auth.json",
                                                                   scopes='https://www.googleapis.com/auth/compute')
    service = discovery.build('compute', 'v1',credentials=credentials)
    
    request = service.snapshots().list(project='xxxx',FILTER="creationTimestamp<'2021-05-31'")
    response = request.execute()
    print (response)
output = get_disks("xxxxxxxx", "europe-west1-b")

【问题讨论】:

  • 错误是什么?
  • 对于scope,我推荐使用https://www.googleapis.com/auth/cloud-platform。实际权限应由分配给身份的角色确定。
  • 你能分享你遇到的错误吗?
  • 返回“字段 'filter' 的值无效:'creationTimestamp = >'2021-06-01''。列表过滤器表达式无效。”。详细信息:“[{'message':“字段'filter'的值无效:'creationTimestamp = >'2021-06-01''。列表过滤器表达式无效。", 'domain': 'global', 'reason': 'invalid'}]">
  • 嗨团队,我的想法是首先我想过滤最近 5 天的时间戳快照的创建。我想删除其余的快照文件

标签: python python-3.x google-cloud-platform snapshot


【解决方案1】:

您的问题是一个已知的 Google Cloud 错误。

请阅读这些问题跟踪器:132365111132676194

解决方案:

去掉过滤语句,处理返回结果:

from datetime import datetime
from dateutil import parser

request = service.snapshots().list(project=project)

response = request.execute()

# Watch for timezone issues here!
filter_date = '2021-05-31'

d1 = parser.parse(filter_date)

for item in response['items']:
    d2 = datetime.fromisoformat(item['creationTimestamp'])

    if d2.timestamp() < d1.timestamp():
        # Process the result here. This is a print statement stub.
        print("{} {}".format(item['name'], item['creationTimestamp']))

【讨论】:

  • 您好先生,感谢您的回复.....以下脚本适用于 2021 年 5 月 31 日......我如何每天运行这个?通过获取系统日期?
  • @balaganesh 创建一个新问题。
  • 我如何使用 filer_date 和系统日期来每天运行脚本?
  • @balaganesh - 您的原始问题已得到解答。按计划运行脚本是一个不同的问题,因此请在 Stackoverflow 上创建一个新问题。
  • 执行了下面的python它没有任何结果,,,,,我想删除10天以前的快照如何实现这个??********** ****************************************************** ************************************
猜你喜欢
  • 2019-08-16
  • 2020-06-22
  • 2018-12-06
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 2020-07-26
相关资源
最近更新 更多