簡體   English   中英

getSignedUrl中的網址將在幾周后過期

[英]Url from getSignedUrl will expire in few weeks

我有存儲觸發功能,該功能可以調整大小並將替換的圖像替換為存儲,然后更新數據庫中的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()

但是,圖像會在幾周后(大約2周后)失效。 有誰知道該如何解決? 正如您從注釋代碼中看到的,我什至都玩過getDownloadURL,但這似乎在觸發器中不起作用

根據以下鏈接:

https://stackoverflow.com/a/42959262/370321

https://cloud.google.com/nodejs/docs/reference/storage/2.5.x/File#getSignedPolicy

不知道您使用的是哪個版本的@ google / cloud-storage,但假設它是2.5.x,則好像您在date字段中傳遞的任何值都傳遞給了new Date()一樣,因此看起來您的代碼應該可以工作當我在開發工具中嘗試過時。 我唯一能猜測的是,它不希望您將文件保留200年。

根據源代碼:

https://github.com/googleapis/nodejs-storage/blob/master/src/file.ts#L2358

您是否嘗試過更短的時間-或以日期格式將其格式化為mm-dd-yyyy?

好的,所以我嘗試了一些東西,但是我不知道這是否行得通,所以我將在2周內再次將我的問題標記為已回答。 對於那些同樣的問題,我將嘗試概括一下我所做的事情。

1 /從控制台下載服務帳戶密鑰。 鏈接在這里

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

2 /將下載的JSON文件保存在函數目錄中

3 /將密鑰包括在功能存儲器中。 但是請注意如何設置文件的路徑。 這是我的問題

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

UPDATE

我剛發現我的功能有誤。 我的網址是由雲功能錯誤提供的(注釋代碼)

這是完整的功能

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
    })
})

我非常有信心現在可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM