gpt4 book ai didi

node.js - 使用 NodeJS 从 IBM Bluemix Object Storage 下载文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:39 26 4
gpt4 key购买 nike

我在使用 NodeJS 脚本从 IBM Bluemix Object Storage 下载文件时遇到问题,问题是从存储下载文件需要很长时间,100K 文件大约需要 21 秒,等待第一个 block 大约需要 8 秒,其余时间用于读取所有 block

我正在使用该服务来存储图像,并使用 NodeJS 脚本从存储中读取这些图像,并且图像是通过 HTML img 标签下载的,那么我在这里到底做错了什么?

nodeJS 代码:

app.get("/ostore/image/:filename", function(request, response) {
response.set('Content-Type', 'image/jpg');
response.set('cache-control', 'max-age=604800');
response.set('Last-Modified', 'Sat, 05 Dec 2015 03:17:48 GMT');
var credentials = app.appEnv.services['Object-Storage'][0].credentials
var pkgcloud = require("pkgcloud");
var client = pkgcloud.storage.createClient({
provider: 'openstack',
username: credentials.userId,
password: credentials.password,
authUrl: credentials.auth_url,
tenantId: credentials.projectId,
region: credentials.region,
version: "2"
});
client.download({
container: app.storageContainer,
remote: request.params.filename,
stream: response
}, function() {
response.end('done');
});
});

最佳答案

尝试这样的事情:

routes.js

var vcap_os = require(__dirname + '/../utils/vcap')('Object-Storage'),
os = require(__dirname + '/../modules/object-storage');

module.exports = function(app) {
var router = app.loopback.Router();

// proxy for object storage service
router.get('/api/Products/image/:container/:file', function(req, res) {
os(vcap_os.credentials).download(req.params.container, req.params.file, function(download) {
download.pipe(res);
});
});

app.use(router);
}

modules/object-storage.js

var pkgcloud = require('pkgcloud');

module.exports = function(creds) {
var config = {
provider: 'openstack',
useServiceCatalog: true,
useInternal: false,
keystoneAuthVersion: 'v3',
authUrl: creds.auth_url,
tenantId: creds.projectId,
domainId: creds.domainId,
username: creds.username,
password: creds.password,
region: creds.region
};

return {
download: function(container, file, cbk) {
var client = pkgcloud.storage.createClient(config);
client.auth(function (error) {
if(error) {
console.error("Authorization error for storage client (pkgcloud): ", error);
}
else {
var request = client.download({
container: container,
remote: file
});

cbk(request);
}
});
}
};
};

关于node.js - 使用 NodeJS 从 IBM Bluemix Object Storage 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239002/

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