How to list all the files from a bucket in Google Cloud Storage using PHP?

Viewed 3866

Is there a way to list all the files from a bucket in Google Storage using php ? I was able to upload and download files by I can't find a way to list all these files ?

in the documentation there are examples of listing using java or python...but not PHP.

https://cloud.google.com/storage/docs/json_api/v1/objects/list

So any ideas ?

Thank You!

EDIT I found a solution . Can anyone confirm that this is the right way ?

2 Answers

This worked for me:

use Google\Cloud\Storage\StorageClient;

$projectId = 'my-project';

$config = [
'projectId' => $projectId,
];

$storage = new StorageClient($config);

$bucket = $storage->bucket('my-bucket-media-files');

foreach ($bucket->objects() as $object) {
  echo '<pre>';
  printf('Object: %s' . PHP_EOL, $object->name());
}

References: https://cloud.google.com/storage/docs/listing-objects?hl=pt-br#code-samples https://github.com/GoogleCloudPlatform/php-docs-samples/blob/HEAD/storage/src/list_objects.php

Related