Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete an object from the Google Cloud Storage using PHP?

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.

like image 718
A.M.N.Bandara Avatar asked Jan 30 '14 18:01

A.M.N.Bandara


People also ask

How do I delete files from Google Cloud Storage?

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.

How do I delete a folder from my Google Cloud Shell?

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.


1 Answers

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');
like image 192
tyler.frankenstein Avatar answered Oct 06 '22 02:10

tyler.frankenstein