Can someone tell me how to delete an object from the Google Cloud Storage using PHP?
I found how to add an object via
move_uploaded_file($gs_name, "gs://sample-storage/myfolder/new_file2.jpg");
get the public URL via
$public_url = CloudStorageTools::getPublicUrl("gs://sample-storage/myfolder/new_file2.jpg", true);
By importing the following as well
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
But how do you delete a file using PHP?
Can someone please share the code in PHP that works? Even using JavaScript if PHP doesn't implicitly support it.
Navigate to the objects, which may be located in a folder. Click the checkbox for each object you want to delete. You can also click the checkbox for folders, which will delete all objects contained in that folder. Click the Delete button.
This option is implicitly set when running "gsutil -m rm ...". Causes gsutil to read the list of objects to remove from stdin. This allows you to run a program that generates the list of objects to remove. The -R and -r options are synonymous.
From the delete_object example, you could try adding a function like this to your project:
function delete_object($bucketName, $objectName, $options = [])
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->delete();
printf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
}
Then to delete the actual file, you could use the function like this:
delete_object('sample-storage', 'myfolder/new_file2.jpg');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With