简体   繁体   中英

How to code PHP to display all files from Google Cloud Storage

Now I had done the php code to upload file to google cloud storage

 <?php use google\\appengine\\api\\cloud_storage\\CloudStorageTools; $bucket = '<your bucket name>'; // your bucket name $root_path = 'gs://' . $bucket . '/'; $_url = ''; if(isset($_POST['submit'])) { if(isset($_FILES['userfile'])) { $name = $_FILES['userfile']['name']; $file_size =$_FILES['userfile']['size']; $file_tmp =$_FILES['userfile']['tmp_name']; $original = $root_path .$name; move_uploaded_file($file_tmp, $original); $_url=CloudStorageTools::getImageServingUrl($original); } } ?> <html> <body> <form action="#" method="post" enctype="multipart/form-data"> Send these files: <p/> <input name="userfile" type="file" /> <p/> <input type="submit" name="submit" value="UPLOAD" /> </form> </body> </html> <?php echo $_url; ?> 

Then I would like to code another php to display all files in my bucket on Google Storage. But, I don't know how to Please help and guide me

This code will allow you to check all the files in a given bucket in your project, including the ones in a subfolder.

<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$projectId = 'PROJECT-ID';

$bucketName = 'YOUR-BUCKET';
$storage = new StorageClient([    
'projectId' => $projectId
]);
$bucket = $storage->bucket($bucketName);
$listObjects = $bucket->objects(['fields' => 
    'items/name,nextPageToken'
]);
foreach ($listObjects as $item) {           
    echo $item->name() . PHP_EOL;
}
?>

You can see more options for this client library in here

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