2016-09-24 84 views
2

我正在研究一个将图像存储在Google云端存储(GCS)中的项目。我使用下面的代码运行NodeJS服务器。使用签名的URL将文件发送到服务器,以便在GCS中恢复可上载。抛光整个文件后,服​​务器将请求发送到该地址。Google云端存储似乎忽略客户提供的加密密钥标题

该部分工作。但是,当我向请求中添加指定customer-supplied encryption key的标头时,GCS将忽略它们。上传完成,返回200,并应用默认加密密钥。

当我尝试了一个无效值x-goog-encryption-algorithm,其中我应该已得到400,它上传罚款。

我应该非常接近,但是我错过什么标题或配置可以指定客户提供的加密密钥?

var crypto = require("crypto"); 
var fs = require("fs"); 
var Request = require("request"); 

var hashKey = function(key) { 
    var hash = crypto.createHash("sha256"); 
    key = new Buffer(key); 
    hash.update(key); 
    return hash.digest("base64"); 
}; 

var GCS = module.exports = function GCS(env) { 
    // config contains the encryption key I'd like to specify. 
    this.config = env.config 
}; 

GCS.prototype.upload = function (url, buffer) { 
    var self = this; 
    return new Promise(function(resolve, reject) { 
    var headers = { 
     "Content-Length": buffer.size, 
     "x-goog-encryption-algorithm": "AES256", 
     "x-goog-encryption-key": self.config.encryption.key, 
     "x-goog-encryption-key-sha256": hashKey(self.config.encryption.key) 
    }; 

    var options = { 
     url: url, 
     method: "PUT", 
     headers: headers 
    }; 

    fs.createReadStream(buffer.path) 
    .pipe(Request(options)) 
    .on("error", function(e) { 
     self.logger.error("Failed to upload asset to slot.", e); 
     reject(e); 
    }) 
    .on("response", function(res){ 
     if (res.statusCode != 200) { 
     reject(new Error("Unexpected response code" + res)); 
     } 
     resolve(res); 
    }); 
    }); 
}; 

回答

1

对于可恢复的上传,您需要发送与初始POST相同的加密标头以创建上传URL。

+1

这样做。谢谢! – freeformflow

相关问题