gpt4 book ai didi

node.js - getSignedUrl 中的网址将在几周后过期

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:14 25 4
gpt4 key购买 nike

我有存储触发功能,可以调整上传的图像大小并将其替换到存储中,然后更新数据库中的 URL

    }).then(() => {
console.log('Original file deleted', filePath)
const logo = storageRef.file(JPEGFilePath)
return logo.getSignedUrl({ action: 'read', expires: date })

// const logo = storageRef.child(JPEGFilePath)
// return logo.getDownloadURL()

// return storageUrl.getDownloadURL(JPEGFilePath)
}).then((url) => {
const newRef = db.collection("user").doc(uid)
return newRef.set({
profile: { profileImg: url[0] }
}, {
merge: true
})
})

这是我设置到期日期的方法

const d = new Date()
const date = new Date(d.setFullYear(d.getFullYear() + 200)).toString()

但是图像将在几周后过期(大约两周)。有谁知道如何解决这个问题?我什至使用了 getDownloadURL,正如您从注释代码中看到的那样,但这似乎在触发器中不起作用

最佳答案

好吧,我已经尝试了一些方法,但我不知道这是否有效,所以我会在两周后回来将我的问题标记为已回答(如果有效)。对于那些有同样问题的人,我将尝试概括一下我所做的事情。

1/从控制台下载服务帐户 key 。这是链接

https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk

2/将下载的JSON文件保存到你的函数目录中

3/将 key 包含在您的函数存储中。但要小心如何设置文件的路径。这是我的问题

https://stackoverflow.com/a/56407592/11486115

UPDATE

我刚刚发现我的函数有错误。我的URL是云函数错误提供的(注释代码)

这里是完整的功能

const {
db
} = require('../../admin')


const projectId = "YOUR-PROJECT-ID"
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: 'PATH-TO-SERVICE-ACCOUNT'})

const os = require('os');
const fs = require('fs');
const path = require('path');

const spawn = require('child-process-promise').spawn

const JPEG_EXTENSION = '.jpg'

exports.handler = ((object) => {
const bucket = object.bucket;
const contentType = object.contentType;
const filePath = object.name
const JPEGFilePath = path.normalize(path.format({ dir: path.dirname(filePath), name: 'profileImg', ext: JPEG_EXTENSION }))
const destBucket = storage.bucket(bucket)
const tempFilePath = path.join(os.tmpdir(), path.basename(filePath))
const tempLocalJPEGFile = path.join(os.tmpdir(), path.basename(JPEGFilePath))
const metadata = {
contentType: contentType
}
const uid = filePath.split("/").slice(1, 2).join("")
const d = new Date()
const date = new Date(d.setFullYear(d.getFullYear() + 200)).toString()

if (!object.contentType.startsWith('image/')) {
return destBucket.file(filePath).delete().then(() => {
console.log('File is not an image ', filePath, ' DELETED')
return null
});
}

if (object.metadata.modified) {
console.log('Image processed')
return null
}

return destBucket.file(filePath).download({
destination: tempFilePath
})
.then(() => {
console.log('The file has been downloaded to', tempFilePath)
return spawn('convert', [tempFilePath, '-resize', '100x100', tempLocalJPEGFile])
}).then(() => {
console.log('JPEG image created at', tempLocalJPEGFile)
metadata.modified = true
return destBucket.upload(tempLocalJPEGFile,
{
destination: JPEGFilePath,
metadata: { metadata: metadata }
})
}).then(() => {
console.log('JPEG image uploaded to Storage at', JPEGFilePath)
return destBucket.file(filePath).delete()
}).then(() => {
console.log('Original file deleted', filePath)
//const logo = storageRef.file(JPEGFilePath)
const logo = destBucket.file(JPEGFilePath)
return logo.getSignedUrl({ action: 'read', expires: date })
}).then((url) => {
const newRef = db.collection("user").doc(uid)
return newRef.set({
profile: { profileImg: url[0] }
}, {
merge: true
})
}).then(() => {
fs.unlinkSync(tempFilePath);
fs.unlinkSync(tempLocalJPEGFile)
console.log(uid, 'user database updated ')
return null
})
})

我非常有信心这现在会起作用。

关于node.js - getSignedUrl 中的网址将在几周后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56304431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com