【问题标题】:google cloud storage serves old content to google compute instances谷歌云存储为谷歌计算实例提供旧内容
【发布时间】:2017-07-12 23:02:21
【问题描述】:

在网络应用程序中有一个谷歌存储桶,其中页面和布局与静态内容一起存储。 有一个带有 nodejs 应用程序的谷歌计算服务器,该应用程序使用布局和dust.js 为页面提供服务以生成标记。 问题: 谷歌云存储将旧的/不推荐使用的文件内容返回到谷歌计算实例,而不是返回到 GCE 之外本地启动的相同代码。本地启动的代码获得新鲜的内容。 即使进程或机器重启也无助于解决这个问题。 此项目使用标准包@google-cloud/storage 来访问存储桶内容。

【问题讨论】:

标签: node.js google-cloud-storage google-compute-engine


【解决方案1】:

我自己用这样的代码解决了这个问题:

const STORAGE_DOWNLOAD_BASE_URL = "https://storage.googleapis.com";

const bucketName = 'yoursuperbucket

/**
 *
 * @param {string} bucketName
 * @param {string} fileName
 * @return {string}
 */
function getUniqueDownloadUrl(bucketName, fileName) {
    return [STORAGE_DOWNLOAD_BASE_URL, bucketName, fileName].join('/') + '?no-cache=true&ignore-cache=true&anti-cache=' + (new Date().getTime());
}
            return new Promise(function (resolve, reject) {
                request({
                    url: getUniqueDownloadUrl(bucketName, filePath),
                    method: 'GET',
                    headers: {
                        'Cache-Control': 'no-cache'
                    }
                }, function (err, response) {
                    if (err) {
                        console.error('Failed load file content: ' + filePath + ' from bucket ' + bucketName + ' - ' + err);
                        return reject(err);
                    }
                    resolve(response.body);
                });
            });

对于没有公共访问规则的存储桶,这依赖于绑定到您的 GCE 实例的服务帐户

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2020-09-10
    • 1970-01-01
    • 2023-03-26
    • 2016-10-12
    相关资源
    最近更新 更多