Set Google Storage Bucket's default cache control

前端 未结 6 1934
孤城傲影
孤城傲影 2021-01-11 16:41

Is there any way to set Bucket\'s default cache control (trying to override the public, max-age=3600 in bucket level every time creating a new object)

S

6条回答
  •  庸人自扰
    2021-01-11 17:31

    I know that this is quite an old question and you're after a default action (which I'm not sure exists), but the below worked for me on a recent PHP project after much frustration:

    $object = $bucket->upload($tempFile, [
                'predefinedAcl' => "PUBLICREAD",
                'name' => $destination,
                'metadata' => [
                    'cacheControl' => 'Cache-Control: private, max-age=0, no-transform',
                ]
            ]);
    

    Same can be applied in node:

    const storage = new Storage();
    
    const bucket = storage.bucket(BUCKET_NAME);
    
    const blob = bucket.file(FILE_NAME);
    
    const uploadProgress = new Promise((resolve, reject) => {
        const blobStream = blob.createWriteStream();
    
        blobStream.on('error', err => {
          reject(err);
    
          throw new Error(err);
        });
    
        blobStream.on('finish', () => {
          resolve();
        });
    
        blobStream.end(file.buffer);
    });
    
    await uploadProgress;
    
    if (isPublic) {
      await blob.makePublic();
    }
    
    blob.setMetadata({ cacheControl: 'public, max-age=31536000' });
    

提交回复
热议问题