Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating storage object with the download url, then deleting the object #639

Closed
goodrickstar opened this issue Aug 17, 2017 · 3 comments
Closed
Assignees
Labels
api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue.

Comments

@goodrickstar
Copy link

I have a table of image file public urls that once they reach a certain age need to be automatically deleted. So far my attempts have failed. The examples i've found only show creating file objects using bucket, folder, and filename. I'm needing to create the storage object via the public url (as done in the Android library. i.e. getReferenceFromUrl(fileUrl)). How is this done?

composer require google/cloud-storage

<?php
require '/home/channel1/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/channel1/.config/google.json');
$config = parse_ini_file('/home/channel1/.config/config.ini');
$storage = new StorageClient(['projectId' => $config['projectId']]);
$bucket = $storage->bucket($config['bucket']);

//obtains list of PUBLIC urls from db that link to image files in storage to delete
$pdo = new PDO('mysql://hostname=localhost;dbname=' . $config['dbname'], $config['username'], $config['password'], array(PDO::ATTR_PERSISTENT => true));
$urls = $pdo->query("SELECT `url` FROM `deletion` WHERE `stamp` < now() - INTERVAL 1 WEEK");
$urls = $urls->fetchAll(PDO::FETCH_NUM);
foreach ($urls as $url) {
  print_r($url[0] . "<br>");
  $object = $bucket->object($url[0]); //public url
  $object->delete(); //deletion fails
}

print_r($url[0]); gives valid public url but I'm getting this error on object creation/deletion

Fatal error: Uncaught Google\Cloud\Core\Exception\NotFoundException: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "Not Found" } ], "code": 404, "message": "Not Found" } } in /home/channel1/vendor/google/cloud-core/RequestWrapper.php:245 Stack trace: #0 /home/channel1/vendor/google/cloud-core/RequestWrapper.php(150): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 /home/channel1/vendor/google/cloud-core/RestTrait.php(95): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 /home/channel1/vendor/google/cloud-storage/Connection/Rest.php(148): Google\Cloud\Storage\Connection\Rest->send('objects', 'delete', Array) #3 /home/channel1/vendor/google/cloud-storage/StorageObject.php(181): Google\Cloud\Storage\Connection\Rest->deleteObject(Array) #4 /home/channel1/public_html/worker.php(16): Google\Cloud\Storage\StorageObject->delete() #5 {main} thrown in /home/channel1/vendor/google/cloud-core/RequestWrapper.php on line 245

@dwsupplee dwsupplee added api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue. labels Aug 17, 2017
@dwsupplee
Copy link
Contributor

Hey @goodrickstar,

The full public URL of an image may not be the same as the name of the object in your bucket. For example, with a public URL of https://storage.googleapis.com/mybucket/myobject.png, the name you would pass into $bucket->object($name) would be myobject.png.

It may also be helpful to take a look at object lifecycle management. These are settings that can be applied to a bucket to automatically delete an object for you after a determined amount of time.

Best,
Dave

@goodrickstar
Copy link
Author

the download url's all similar to this

https://firebasestorage.googleapis.com/v0/b/channel-19.appspot.com/o/profiles%2FSnapchat-693437634.jpg?alt=media&token=7baa5366-fab1-46b3-b6f8-b9efafff2e2b

I'm using object lifecycle management in other areas. This task needs a manual approach.
With the android library it is possible to get a file object using these public urls and so I never need to store the actual filename. Is this not possible with the PHP library?

@dwsupplee
Copy link
Contributor

We currently don't have support for that, no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants