Dar*_*han 1 javascript azure node.js ember.js
我正在尝试使用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();
});
});
Run Code Online (Sandbox Code Playgroud)
我打印出了结果和响应,这就是我得到的:
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 }
Run Code Online (Sandbox Code Playgroud)
这些似乎都没有包含我刚刚发布的文件的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}));
Run Code Online (Sandbox Code Playgroud)
...来自https://github.com/Azure/azure-storage-node/blob/master/lib/common/lib/services/storageserviceclient.js。 host是的,你在为你的电话到最后一个参数传递正是一个稍微变换/标准化版本createBlobService,并且webResource.path是再次团块容器名和BLOB名称,你在调用中传递的标准化串联createBlockBlobFromFile。
可靠地对所有这些东西进行规范化将是一件痛苦的事情,但值得庆幸的是,您无需这样做!看一下blobService您正在调用的对象createBlockBlobFromFile-有一个方法getUrl。如果使用与用于创建文件的参数等效的参数进行调用,例如
var containerName = 'mycontainer';
var hostName = 'https://mystorageaccountname.blob.core.windows.net';
var url = blobService.getUrl(containerName, file.name, null, hostName);
Run Code Online (Sandbox Code Playgroud)
...您将获得具有指定名称,主机和Blob容器名称的Blob终止的路径。并不像在返回的响应对象上放置路径那样方便,但这看起来确实可以可靠地对发出请求时执行的路径部分执行所有相同的标准化和格式化操作。只要确保两者都有一个实例,containerName并hostName在该文件的整个代码中共享它们,就可以了。
| 归档时间: |
|
| 查看次数: |
2243 次 |
| 最近记录: |