我正在尝试从 GCP 中的云存储搜索和对话数据存储应用中清除所有数据,但无法清除

问题描述 投票:0回答:1

我需要从数据存储应用程序中删除所有数据,以便随后将其删除。每次我尝试时,它都会说它已成功删除了斑点,但它们仍然存在。我在 jupyterlab 中使用 python 笔记本

想法?

from google.cloud import storage

def purge_gcs_data(bucket_name, folder):
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
print(f"Storage bucket is '{bucket.name}'")

    # Delete individual record files
    record_blobs = bucket.list_blobs(prefix=f"{folder}/")
    for blob in record_blobs:
        print(f"Deleting {blob.name}...")
        blob.delete()

    # Delete the metadata file
    metadata_blob = bucket.blob(f"{folder}/metadata.jsonl")
    if metadata_blob.exists():
        print("Deleting metadata.jsonl...")
        metadata_blob.delete()

    # Check if the folder is now empty
    remaining_blobs = list(bucket.list_blobs(prefix=f"{folder}/"))
    if not remaining_blobs:
        print(f"Confirmation: All objects in '{folder}' of bucket '{bucket_name}' have been      successfully purged.")
    else:    
        for blob in remaining_blobs:
            print(blob.name)
    
            try:
                remaining_blobs = list(bucket.list_blobs(prefix=f"{folder}/"))
                if not remaining_blobs:
                    print(f"Confirmation: All objects in '{folder}' of bucket '{bucket_name}' have been successfully purged.")
                else:
                    print("Warning: Some objects were not deleted:")
                for blob in remaining_blobs:
                    print(f"Remaining Object: {blob.name}")
            except Exception as e:
                print(f"Error during fetching remaining blobs: {e}")

# Call the function 
PROJECT_ID_SUFFIX = PROJECT_ID.split("-")\[-1\]
GCS_BUCKET = f"shared-aif-bucket-{PROJECT_ID_SUFFIX}"
GCS_FOLDER = "clinical note summarization demo"

purge_gcs_data(GCS_BUCKET, GCS_FOLDER)

我尝试过使用和不使用 if 语句,我尝试过 try/ except 。它看起来总是成功的,直到我进入数据应用程序

python google-cloud-platform google-cloud-storage jupyter-lab
1个回答
0
投票

尝试使用 get_bucket 而不是 bucket:

bucket = storage_client.get_bucket(bucket_name)

官方文档中查看此参考。

© www.soinside.com 2019 - 2024. All rights reserved.