com.google.cloud.storage.Bucket.list()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(262)

本文整理了Java中com.google.cloud.storage.Bucket.list()方法的一些代码示例,展示了Bucket.list()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bucket.list()方法的具体详情如下:
包路径:com.google.cloud.storage.Bucket
类名称:Bucket
方法名:list

Bucket.list介绍

[英]Returns the paginated list of Blob in this bucket.

Example of listing the blobs in the bucket.

Page blobs = bucket.list();}

[中]返回此存储桶中已分页的Blob列表。
在bucket中列出blob的示例。

Page blobs = bucket.list();}

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/** Example of listing the blobs in the bucket. */
// [TARGET list(BlobListOption...)]
public Page<Blob> listBlobs() {
 // [START listBlobs]
 Page<Blob> blobs = bucket.list();
 for (Blob blob : blobs.iterateAll()) {
  // do something with the blob
 }
 // [END listBlobs]
 return blobs;
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
public void run(Storage storage, String bucketName) {
 if (bucketName == null) {
  // list buckets
  for (Bucket bucket : storage.list().iterateAll()) {
   System.out.println(bucket);
  }
 } else {
  // list a bucket's blobs
  Bucket bucket = storage.get(bucketName);
  if (bucket == null) {
   System.out.println("No such bucket");
   return;
  }
  for (Blob blob : bucket.list().iterateAll()) {
   System.out.println(blob);
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

public static void main(String... args) {
  // Create a service object
  // Credentials are inferred from the environment.
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // Create a bucket
  String bucketName = "my_unique_bucket"; // Change this to something unique
  Bucket bucket = storage.create(BucketInfo.of(bucketName));

  // Upload a blob to the newly created bucket
  Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");

  // Read the blob content from the server
  String blobContent = new String(blob.getContent(), UTF_8);

  // List all your buckets
  System.out.println("My buckets:");
  for (Bucket currentBucket : storage.list().iterateAll()) {
   System.out.println(currentBucket);
  }

  // List the blobs in a particular bucket
  System.out.println("My blobs:");
  for (Blob currentBlob : bucket.list().iterateAll()) {
   System.out.println(currentBlob);
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testList() throws Exception {
 initializeExpectedBucket(4);
 PageImpl<Blob> expectedBlobPage = new PageImpl<>(null, "c", blobResults);
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.list(BUCKET_INFO.getName())).andReturn(expectedBlobPage);
 replay(storage);
 initializeBucket();
 Page<Blob> blobPage = bucket.list();
 Iterator<Blob> blobInfoIterator = blobPage.getValues().iterator();
 Iterator<Blob> blobIterator = blobPage.getValues().iterator();
 while (blobInfoIterator.hasNext() && blobIterator.hasNext()) {
  assertEquals(blobInfoIterator.next(), blobIterator.next());
 }
 assertFalse(blobInfoIterator.hasNext());
 assertFalse(blobIterator.hasNext());
 assertEquals(expectedBlobPage.getNextPageToken(), blobPage.getNextPageToken());
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
 public void run() {
  Page<Bucket> buckets =
    storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
  for (Bucket bucket : buckets.iterateAll()) {
   if (bucket.getCreateTime() < olderThan) {
    try {
     for (Blob blob :
       bucket
         .list(
           BlobListOption.fields(
             Storage.BlobField.EVENT_BASED_HOLD,
             Storage.BlobField.TEMPORARY_HOLD))
         .iterateAll()) {
      if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
       storage.update(
         blob.toBuilder()
           .setTemporaryHold(false)
           .setEventBasedHold(false)
           .build());
      }
     }
     forceDelete(storage, bucket.getName());
    } catch (Exception e) {
     // Ignore the exception, maybe the bucket is being deleted by someone else.
    }
   }
  }
 }
};

代码示例来源:origin: GoogleCloudPlatform/java-docs-samples

com.google.api.gax.paging.Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));

代码示例来源:origin: spotify/spydra

public Page<Blob> listBucket(String bucketName, String directory) {
 Bucket bucket = requireNonNull(
   storage.get(bucketName, Storage.BucketGetOption.fields()),
   "Please provide bucket name.");
 Page<Blob> blobs = bucket.list(Storage.BlobListOption.prefix(directory));
 return blobs;
}

代码示例来源:origin: ai.h2o/h2o-persist-gcs

@Override
 public List<String> load(String key) {
  final List<String> blobs = new ArrayList<>();
  for (Blob b : storageProvider.getStorage().get(key).list().iterateAll()) {
   blobs.add(b.getName());
  }
  return blobs;
 }
});

代码示例来源:origin: io.konig/konig-gcp-common

private void deleteBucket(Bucket bucket) {
  if (bucket != null) {
  
    Page<Blob> page = bucket.list();
    
    for (Blob blob : page.iterateAll()) {
      blob.delete();
    }
    bucket.delete();
  }
  
}

代码示例来源:origin: wepay/kafka-connect-bigquery

Page<Blob> list = bucket.list();
logger.trace("Finished GCS bucket list");

代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij

static GcsBucketVirtualFile setupVirtualFileWithBucketMocks(
   GcsBucketVirtualFile gcsBucketVirtualFile) {
  Bucket bucket = mock(Bucket.class);
  Page<Blob> page = mock(Page.class);
  Iterable<Blob> blobIterable = mock(Iterable.class);
  Iterator<Blob> blobIterator = mock(Iterator.class);

  when(gcsBucketVirtualFile.getBucket()).thenReturn(bucket);
  when(bucket.list()).thenReturn(page);
  when(bucket.list(any(BlobListOption.class), any(BlobListOption.class))).thenReturn(page);
  when(page.iterateAll()).thenReturn(blobIterable);
  when(blobIterable.iterator()).thenReturn(blobIterator);

  return gcsBucketVirtualFile;
 }
}

代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij

private void setBlobs(Blob... blobs) {
 List<Blob> blobList = Lists.newArrayList(blobs);
 Page<Blob> blobPage = bucketVirtualFile.getBucket().list();
 when(blobPage.iterateAll()).thenReturn(blobList);
}

代码示例来源:origin: ai.h2o/h2o-persist-gcs

private void parseBucket(String bucketId,
             ArrayList<String> files,
             ArrayList<String> keys,
             ArrayList<String> fails) {
 final Bucket bucket = storageProvider.getStorage().get(bucketId);
 for (Blob blob : bucket.list().iterateAll()) {
  final GcsBlob gcsBlob = GcsBlob.of(blob.getBlobId());
  Log.debug("Importing: " + gcsBlob.toString());
  try {
   final Key k = GcsFileVec.make(gcsBlob.getCanonical(), blob.getSize());
   keys.add(k.toString());
   files.add(gcsBlob.getCanonical());
  } catch (Throwable t) {
   Log.err(t);
   fails.add(gcsBlob.getCanonical());
  }
 }
}

代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij

@Test
public void testErrorMessageIsCleared_afterSuccessfulBucketList() {
 when(bucketVirtualFile.getBucket().list(any(BlobListOption.class), any(BlobListOption.class)))
   .thenThrow(StorageException.class);
 editorPanel = new GcsBucketContentEditorPanel(bucketVirtualFile.getBucket());
 editorPanel.initTableModel();
 assertTrue(editorPanel.getErrorPanel().isVisible());
 // Re-initialize mocks so the exception is not thrown and update the UI
 reset(bucketVirtualFile.getBucket());
 editorPanel.updateTableModel("");
 assertFalse(editorPanel.getErrorPanel().isVisible());
}

代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij

@Test
public void testBucketListException_showsErrorMessage() {
 when(bucketVirtualFile.getBucket().list(any(BlobListOption.class), any(BlobListOption.class)))
   .thenThrow(StorageException.class);
 editorPanel = new GcsBucketContentEditorPanel(bucketVirtualFile.getBucket());
 editorPanel.initTableModel();
 JTable bucketTable = editorPanel.getBucketContentTable();
 assertThat(bucketTable.getColumnCount()).isEqualTo(0);
 assertThat(bucketTable.getRowCount()).isEqualTo(0);
 assertFalse(editorPanel.getMessagePanel().isVisible());
 assertFalse(editorPanel.getLoadingPanel().isVisible());
 assertTrue(editorPanel.getErrorPanel().isVisible());
}

代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij

Lists.newArrayList(
     bucket
       .list(BlobListOption.currentDirectory(), BlobListOption.prefix(prefix))
       .iterateAll());
} catch (StorageException se) {

代码示例来源:origin: com.google.cloud/google-cloud-storage

@Override
 public void run() {
  Page<Bucket> buckets =
    storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
  for (Bucket bucket : buckets.iterateAll()) {
   if (bucket.getCreateTime() < olderThan) {
    try {
     for (Blob blob :
       bucket
         .list(
           BlobListOption.fields(
             Storage.BlobField.EVENT_BASED_HOLD,
             Storage.BlobField.TEMPORARY_HOLD))
         .iterateAll()) {
      if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
       storage.update(
         blob.toBuilder()
           .setTemporaryHold(false)
           .setEventBasedHold(false)
           .build());
      }
     }
     forceDelete(storage, bucket.getName());
    } catch (Exception e) {
     // Ignore the exception, maybe the bucket is being deleted by someone else.
    }
   }
  }
 }
};

相关文章