简体   繁体   中英

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

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 ?

I've found an answer. I did not test it enough . It is strange we cannot find this on documentation for php.

            $storage = new Google_Service_Storage($this->gcsClient);
            $listObjects = $storage->objects->listObjects($this->bucket, array());
            $items = $listObjects->getItems();

            foreach ($items as $item) {
               print_r($item["name"]);
            }

Can anyone confirm that this is the correct way to list the files from a bucket?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM