LauraEld
LauraEld

Reputation: 349

Google Cloud Storage cache management with PHP

I have a PHP file to upload images to Google cloud storage, but everytime an user changes their profile image, it shows the previous one for a long time, so they think the image is not updated at all. I would like to know what cache and metadata configuration is required to avoid this issue.

I would prefer not to use "custom metadata" because this message on the Cloud documentation https://cloud.google.com/storage/docs/metadata -> "Note that using custom metadata incurs storage and network costs."

This is my code:

require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient; 
$storage = new StorageClient([
    'keyFilePath' => $keypath
]);
$storage->registerStreamWrapper();
$bucket = $storage->bucket($url);

$filename    = $_FILES['profile_picture']['name'];
$newFileName = $_POST['user_id'].'_profile.jpg';
$bucket->upload(
    fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
    [
       'name' => $dir.$newFileName,
       'metadata' => [
          'cacheControl' => 'Cache-Control: no-cache, max-age=0',
        ]
    ]
);

I dont know if CacheControl is enough (and if the sintax is correct) or I need to include Custom-Time and what is the sintax for it.

My try:

$bucket->upload(
    fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
    [
       'name' => $dir.$newFileName,
       'metadata' => [
          'cacheControl' => 'Cache-Control: no-cache, max-age=0',
          'Custom-Time' => date('Y-m-d\TH:i:s.00\Z');
        ]
    ]
);

The documentation dont have anything abour PHP syntax: https://cloud.google.com/storage/docs/metadata

Please help

Upvotes: 0

Views: 305

Answers (2)

LauraEld
LauraEld

Reputation: 349

My solution with the correct metadata syntax:

$bucket->upload(
   fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
   [
        'name' => $_SESSION['idcompany'].'/uploads/'.$newFileName,
        'metadata' => [
             'cacheControl' => 'no-cache, max-age=0',
             'customTime' => gmdate('Y-m-d\TH:i:s.00\Z')
        ]
   ]
);

Upvotes: 0

Methkal Khalawi
Methkal Khalawi

Reputation: 2477

I believe you should use:

$bucket->upload(
    fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
    [
       'name' => $dir.$newFileName,
       'metadata' => [
          'cacheControl' => 'no-cache, max-age=0',
        ]
    ]
);

I have noticed that you repeating the cacheControl in your code. Also, remember that:

If you allow caching, downloads may continue to receive older versions of an object, even after uploading a newer version. This is because the older version remains "fresh" in the cache for a period of time determined by max-age. Additionally, because objects can be cached at various places on the Internet, there is no way to force a cached object to expire globally. If you want to prevent serving cached versions of publicly readable objects, set Cache-Control:no-cache, max-age=0 on the object.

for further read, refer to this documentation

Upvotes: 1

Related Questions