0

I secured some file in an s3 bucket using s3fs. My problem is : the "Expires" argument send to build the file URI is not reset each time the object is call in my API.

time doesn't change if I don't clear the cache with drush

$expires = time() + 10;

So I tried to use hook_entity_view :

if ($entity->bundle() === 'my_bundle') {
    foreach ($entity->getCacheTags() as $tag) {
      $tags[] = $tag;
    }
  }

Doesn't work

Drupal::entityTypeManager()->getStorage($entityType)->resetCache();

Nop

\Drupal::service('cache.render')->deleteAll();
\Drupal::service('cache.entity')->deleteAll();
\Drupal::service('cache.data')->deleteAll();

deleteAll or invalidateAll, none of it work.

drupal_flush_all_caches();

Nothing, I'm starting to think I have config problem... If someone can save me !

UPDATE

It seems hook_entity_load works with my API, it clear the cache correctly. The problem remains when i'm trying to watch the content in my Drupal website, in the content view.

4
  • 1
    So you want to disable caching for my_bundle? Then see drupal.stackexchange.com/questions/232607/…
    – 4k4
    Oct 9 '20 at 11:22
  • I can't figure out what exactly your problem is. Nor do I understand the context of where any of your code snippets are used. Can you please update your question. Can you more clearly define what you mean by "My problem is : the "Expires" argument send to build the file URI is not reset each time the object is call in my API."? Can you also explain what entity is in your code examples? What's the relationship between the s3 file and $entity. Also, where is any of this code being called from.
    – sonfd
    Oct 9 '20 at 11:37
  • @4k4 Thx it fix my last render problem :)
    – Aastal
    Oct 9 '20 at 12:39
  • @sonfd I just updated my question. Everything is ok now
    – Aastal
    Oct 9 '20 at 12:48
0

Ok everything is fixed, there were 2 points :

  • Clean the cache when the media 'masterclass_private_video' is called, so the request to AWS s3 has the correct "Expires" timestamp when call by my VueJs API.
  • Clean the render cache of the content view in my drupal backend.

Clean the media cache for the API :

function mymodule_entity_load(array $entities, $entity_type_id)
{
  $tags = [];
  foreach ($entities as $entity) {
    if ($entity->bundle() === 'masterclass_private_video') {
      foreach ($entity->getCacheTags() as $tag) {
        $tags[] = $tag;
      }
    }
  }

  if (!empty($tags)) {
    Cache::invalidateTags($tags);
  }
}

Clean the page cache :

function mymodule_node_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display)
{
  if ($entity->bundle() === 'masterclass' || $entity->bundle() === 'masterclass_chapter') {
    $build['#cache']['max-age'] = 0;
    \Drupal::service('page_cache_kill_switch')->trigger();
  }
}

To be clear, the goal is to reset the $expires, so the AWS s3 works :

function mymodule_s3fs_url_settings_alter(array &$url_settings, $s3_file_path)
{
  $split = explode('/', $s3_file_path);

  if (in_array('private', $split)) {
    $file_path = getenv('AWS_CLOUDFRONT_ENDPOINT') . $s3_file_path;

    $expires = time() + 86400;
    $private_key_filename = '/var/www/my-drupal/keys/pk-cloudfront.pem';
    $key_pair_id = getenv('AWS_S3_KEY_ID');

    $canned_policy_getters = Drupal::service('mymodule.aws.signature')
      ->getCannedPolicyGetters($file_path, $private_key_filename, $key_pair_id, $expires);

    $url_settings['custom_GET_args'] = $canned_policy_getters;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.