gpt4 book ai didi

javascript - 如何获取通过 Node.js 上传到 Azure 存储的文件的 URL?

转载 作者:行者123 更新时间:2023-11-30 07:38:22 26 4
gpt4 key购买 nike

我正在尝试检索刚刚使用 node.js 上传到 Azure 存储的文件的 URL。我的上传代码是这样的:

[注意:我使用的是 Ember-droplet,因此“区域”是文件拖放的位置。这是服务器端代码,用于处理发送 POST 请求以上传文件的路由。 ]

// Responsible for the call to OPTIONS.
app.options('/upload', function(request, response) {
response.send(200);
});

/* Responsible for file upload. */
app.post('/fileUpload', function(request, response) {

/* Get the files dropped into zone. */
var files = request.files.file,
promises = [];

/**
* @method uploadFile
* @param file {Object}
* @return {Object}
* Function takes a general param file, creates a blob from it and returns the promise to upload it.
* Promise: jQuery promise = all done later.
*/
var uploadFile = function(file) {
var deferred = new Deferred();

// Actual upload code.
// Also replace 'profile-pictures with abstracted container name.'
blobService.createBlockBlobFromFile('profile-pictures', file.name, file.path, function(error, result, response) {
if (!error) {
deferred.resolve(file.name);
console.log("result:");
console.log(result);

console.log("response:");
console.log(response);
}
});

return deferred.promise;
};

if (!Array.isArray(files)) {

// We're dealing with only one file.
var promise = uploadFile(files);
promises.push(promise);

} else {

// We're dealing with many files.
files.forEach(function(file) {
var promise = uploadFile(file);
promises.push(promise);
});

}

var fileUrls = [];
// Send all files in the promise to execute.
promisedIo.all(promises).then(function(files) {
response.send({ files: files, success: true });
response.end();

});
});

我打印出了结果和响应,这就是我得到的:

result:
{ container: 'profile-pictures',
blob: 'robot.jpeg',
etag: '---blah---',
lastModified: 'Mon, 30 Jun 2014 14:38:09 GMT',
contentMD5: '---blah---',
requestId: '---blah---' }
response:
{ isSuccessful: true,
statusCode: 201,
body: '',
headers:
{ 'transfer-encoding': 'chunked',
'content-md5': '---blah---',
'last-modified': 'Mon, 30 Jun 2014 14:38:09 GMT',
etag: '"---blah---"',
server: 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0',
'x-ms-request-id': '---blah---',
'x-ms-version': '2014-02-14',
date: 'Mon, 30 Jun 2014 14:38:08 GMT' },
md5: undefined }

这些似乎都不包含我刚刚发布的文件的 URL;我根本不知道如何获取 URL。有没有我缺少的 blobservice 方法,或者类似的方法。

我发现的一个“解决方案”是对其进行硬编码:

http:///blob.core.windows.net//blob-name,

但我对此感到不舒服。有没有办法提取此 URL,如果可以,如何提取?

最佳答案

事实证明,连接字符串实际上是 Azure 库在幕后所做的事情 - 文件的目标路径是在这一行中计算的:

webResource.uri = url.resolve(host, url.format({pathname: webResource.path, query: webResource.queryString}));

...来自https://github.com/Azure/azure-storage-node/blob/master/lib/common/lib/services/storageserviceclient.jshost 是您在调用 createBlobService 时作为最后一个参数传入的内容的略微转换/规范化版本,而 webResource.path 是同样,您在调用 createBlockBlobFromFile 时传递的 Blob 容器名称和 Blob 名称的标准化串联。

让自己可靠地规范化所有这些东西会很痛苦,但幸运的是你不需要这样做!查看您正在调用 createBlockBlobFromFileblobService 对象,其中有一个方法 getUrl。如果您使用与用于创建文件的参数等效的参数来调用此函数,例如

var containerName = 'mycontainer';
var hostName = 'https://mystorageaccountname.blob.core.windows.net';

var url = blobService.getUrl(containerName, file.name, null, hostName);

...您将获得具有指定名称、主机和 Blob 容器名称的 Blob 最终的路径。不像在某些返回的响应对象上拥有路径那么方便,但这看起来确实会可靠地对发出请求时执行的路径部分执行所有相同的规范化和格式化。只需确保在该文件中的整个代码中共享 containerNamehostName 的单个实例,就可以了。

关于javascript - 如何获取通过 Node.js 上传到 Azure 存储的文件的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493144/

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