#Overcoming S3/R2 metadata prefix (x-amz-meta-cache-tag) to attach Cloudflare Cache Tags

3 messages · Page 1 of 1 (latest)

tepid wigeon
#

Hi everyone,

Looking for guidance on Cache Tags with AWS S3 as the origin. We currently do purge by URL (over 1M URLs/week) and want to switch to tags to dramatically reduce the number of purge requests. On S3 we add user metadata, so instead of a Cache-Tag header we only see x-amz-meta-cache-tag (which seems typical for object stores: R2 also uses x-amz-meta-*, Azure Blob uses x-ms-meta-*).

The only working pattern we have in a Cloudflare Worker is:

const tags = parse(head.headers.get('x-amz-meta-cache-tag') || '');
const resp = await fetch(s3Url, { cf: { cacheTags: tags, cacheEverything: true } });
return resp;```

Questions:
    1.    Is this the “right” approach for S3→Cloudflare (first `HEAD`, then `GET` with cf.cacheTags) or is there a way to do it with one request?
    2.    Is there any “pre-cache” hook/pattern to apply tags read from the origin response without an extra `HEAD`, i.e., at the moment of writing to cache?
    3.    We considered Snippets, but from the docs it looks like they won’t help: Snippets run outside the caching moment and can’t assign tags (cf.cacheTags isn’t available there; adding/renaming a header after caching won’t attach tags to the object). Is that understanding correct?

Thanks!
stable crown
# tepid wigeon Hi everyone, Looking for guidance on Cache Tags with AWS S3 as the origin. We c...

What about this?

const cache = caches.default;
let res = await cache.match(s3Url);
if(res) {
  return res;
}
res = await fetch(s3Url, { cf: { cacheEverything: true }});
const tags = parse(res.headers.get('x-amz-meta-cache-tag') || '');
let cacheRes = res.clone();
cacheRes = new Response(res.body, res);
cacheRes.headers.set("cache-tag", tags.join(','));
ctx.waitUntil(cache.put(s3Url, res));
return res;
#

I guess you could omit parse too if the x-amz-meta-cache-tag is already in the right format