2
votes

I upload to google cloud storage bucket via the storage c# api (Google.Cloud.Storage.V1). These are public files accessed by client pages.

Problem: the files are sent with "private, max-age= 0".

Question: I would like to set custom cache headers instead while or after uploading the files via the api itself. Is this possible to sent the cache header or other metadata via the c# google storage api call?

I am also curious: since I have not set any cache header, why does google storage serve these files with max-age=0, instead of not sending any cache header at all?

1

1 Answers

3
votes

You can set the cache control when you call UploadObject, if you specify an Object instead of just the bucket name and object name. Here's an example:

var client = StorageClient.Create();
var obj = new Google.Apis.Storage.v1.Data.Object
{
    Bucket = bucketId,
    Name = objectName,
    CacheControl = "public,max-age=3600"
};
var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello world"));
client.UploadObject(obj, stream);

You can do it after the fact as well using PatchObject:

var patch = new Google.Apis.Storage.v1.Data.Object
{
    Bucket = bucketId,
    Name = objectName,
    CacheControl = "public,max-age=7200"
};
client.PatchObject(patch);

I don't know about the details of cache control if you haven't specified anything though, I'm afraid.