doubaran2438 2017-09-29 15:28
浏览 79
已采纳

如何列出PHP中Bucket子目录中的所有文件?

After reviewing the PHP Docs for the GCP Storage API and the Bookshelf tutorial (https://cloud.google.com/php/getting-started/using-cloud-storage) I'm lost on how to list the files located in a Bucket subdirectory.

I've viewed Listing files in Google Cloud Storage (nearline) - missing files, however, this code is adapted to Python. If it really is as simple as using an ls command, how would I run this command from PHP? I've dug through the repo's on Github from Google and I'm not sure which to use in this case.

I have both of these libraries included via composer. Just to clarify I'm running these remotely from a DigitalOcean Droplet, not from App Engine.

"google/appengine-php-sdk": "^1.9",
"google/cloud": "^0.39.2",
  • 写回答

1条回答 默认 最新

  • doufei4418 2017-09-29 18:38
    关注

    There's an "objects" method that'll do this for you.

    use Google\Cloud\Storage\StorageClient;
    
    $storage = new StorageClient();
    $bucket = $storage->bucket('my-bucket');
    $objects = $bucket->objects([
        'fields' => 'items/name,nextPageToken'
    ]);
    
    foreach ($objects as $object) {
        echo $object->name() . PHP_EOL;
    }
    

    The documentation for the PHP storage client is over here: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.39.2/storage/storageclient

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?