一直在尝试使用 Firebase 在文件上设置自定义时间?

问题描述

我正在尝试在前端的 firebase 中设置自定义时间属性。一切都可以设置,比如 contentdisposition、自定义元数据等,只是找不到任何关于设置自定义间的方法或任何信息。

您可以在此处看到它的引用https://cloud.google.com/storage/docs/metadata#custom-time

您可以在存储云控制台中手动设置文件自定义时间,但是即使您这样做并且您在前端的 firebase 中加载文件,它也不会从返回的对象中丢失! (让我觉得这是不可能实现的)

var storage = this.$firebase.app().storage("gs://my-files");
var storage2 = storage.ref().child(this.file);

//// Tried this
var md = {
    customTime: Now.$firebase.firestore.FieldValue.serverTimestamp()
};

//// & Tried this
var md = {
    Custom-Time: Now.$firebase.firestore.FieldValue.serverTimestamp()
};

storage2.updateMetadata(md).then((Metadata) => {
    console.log(Metadata);                     
}).catch((err) => {
    console.log(err); 
});

我问的原因是我试图在每次加载文件时推迟生命周期删除日期(这将基于自定义时间)。有谁知道答案或替代方法吗?

提前致谢

解决方法

无法使用 Firebase JavaScript SDK 更新 CustomTime 元数据,因为它未包含在文档中提到的文件元数据 properties list 中。因此,即使您将其指定为 customTime: 或 Custom-Time: updateMetadata() 方法也不会执行任何更改。

我建议您作为一种更好的做法,每次使用 set the CustomTime metadata from the cloud consoleCustomTimeBefore Lifecycle condition 加载文件时,addLifeCycleRule method 并从后端修改 GCP Node.js Client. /p>

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
//Imports your Google Cloud Storage bucket
const myBucket = storage.bucket('my_bucket');

//-
// Delete object that has a customTime before 2021-05-25.
//-
myBucket.addLifecycleRule({
  action: 'delete',condition: {
    customTimeBefore: new Date('2021-05-25')
  }
},function(err,apiResponse) {});