How to remove an object from Google cloud storage using PHP?

Can someone tell me how to remove an object from Google Cloud Storage using PHP?

I found how to add an object through

move_uploaded_file($gs_name, "gs://sample-storage/myfolder/new_file2.jpg");

get public URL through

$public_url = CloudStorageTools::getPublicUrl("gs://sample-storage/myfolder/new_file2.jpg", true);

Also, by importing the following:

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 anyone share this code in PHP? Even using JavaScript if PHP doesn't support it implicitly.

+5
source share
2 answers

You can use unlink ( http://www.php.net/manual/en/function.unlink.php ) to do this, for example.

unlink("gs://sample-storage/foo.jpg");
+5
source

delete_object :

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);
}

, , :

delete_object('sample-storage', 'myfolder/new_file2.jpg');
0

Source: https://habr.com/ru/post/1524570/


All Articles