简体   繁体   English

使用云存储选项从谷歌存储桶中的目录中删除所有对象

[英]Delete all objects from a directory in google storage bucket using cloud storage options

I'm using cloud storage options from google cloud to manage data in a GCP bucket.我正在使用来自谷歌云的云存储选项来管理 GCP 存储桶中的数据。 I've a directory under which I want to delete all the objects before I start writing new objects.我有一个目录,我想在开始编写新对象之前删除该目录下的所有对象。 sample directory: gs://bucket-name/directory1/subdirectory/示例目录:gs://bucket-name/directory1/subdirectory/

I'm able to delete a single object using the following code, but how do I get list of all the objects in a directory and delete all of them?我可以使用以下代码删除单个 object,但如何获取目录中所有对象的列表并删除所有对象?

import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class DeleteObject {
       public static void deleteObject(String projectId, String bucketName, String objectName) {
       // The ID of your GCP project
       // String projectId = "your-project-id";

      // The ID of your GCS bucket
      // String bucketName = "your-unique-bucket-name";

     // The ID of your GCS object
     // String objectName = "your-object-name";

     Storage storage = 
     StorageOptions.newBuilder().setProjectId(projectId).build().getService();
     storage.delete(bucketName, objectName);

     System.out.println("Object " + objectName + " was deleted from " + bucketName);
     }
   }

Iterate over objects in the bucket with a prefix of the directory name.使用目录名称的前缀迭代bucket中的objects

See the following snippet:请参阅以下代码段:

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Page<Blob> blobs =
storage.list(
        bucketName,
        Storage.BlobListOption.prefix(directoryPrefix), // directoryPrefix is the sub directory. 
        Storage.BlobListOption.currentDirectory());
    
for (Blob blob : blobs.iterateAll()) {
  blob.delete(Blob.BlobSourceOption.generationMatch());
}

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Storage 对存储桶中的对象进行分页 (PHP) - Google Cloud Storage paginate objects in a bucket (PHP) 从谷歌云存储桶中读取文件 - Reading files from google cloud storage bucket 从 Google Cloud Storage Bucket 下载文件夹 - Downloading folders from Google Cloud Storage Bucket 一旦使用 apache 光束 sdk 在 Google Cloud 中创建数据流作业,我们可以从云存储桶中删除 tmp 文件吗? - Once dataflow job is created in Google Cloud using apache beam sdk, can we delete the tmp files from cloud storage bucket? Google Cloud Storage 删除包含归档文件的对象 - Google Cloud Storage delete objects with archived files 将文件从 Azure blob 存储移动到 Google 云存储桶 - Moving Files from Azure blob storage to Google cloud storage bucket 防止 Google Cloud Storage 帐户所有者对用户存储桶对象的可见性 - Preventing Google Cloud Storage account owner visibility into user bucket objects Airflow 操作员将许多文件(目录、前缀)从谷歌云存储桶复制到本地文件系统 - Airflow operator to copy many files (directory, prefix) from Google Cloud Storage bucket to local filesystem 将文件从 /tmp 文件夹移动到 Google Cloud Storage 存储桶 - Move file from /tmp folder to Google Cloud Storage bucket 如何从谷歌云存储桶中获取项目名称/ID? - How to get project name/id from Google Cloud Storage bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM