简体   繁体   中英

Google Cloud Storage api (c#) - cache header metadata

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM