Nodejs将base64图像上传到azure blob存储结果为" ResourceNotFound"错误

时间:2016-03-18 09:37:22

标签: node.js azure azure-storage-blobs azure-blob-storage

以下是帖子请求JSON

{
    "name":"images.jpg",
    "file":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhIUEhIUFBUV…K9rk8hCAEkjFMUYiEAI+nHIpsQh0AkisDYRTOiCAbWVtgCtI6IlkHh7LDTQXLH0EIQBj//2Q=="
}

此处是请求/upload路由下的节点控制器,我正在使用azure-storage中的createBlockBlobFromText()

var azure = require('azure-storage');
var blobSvc = azure.createBlobService('myblob.blob.core.windows.net/mycontainer', THE_KEY);

controllers.upload = function (req, res, next){

    var startDate = new Date();
    var expiryDate = new Date(startDate);

    var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.BlobUtilities.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: expiryDate
        }
    };

    var sharedAccessSignatureToken = blobSvc.generateSharedAccessSignature('resources', req.body.file, sharedAccessPolicy);
    var sharedBlobService = azure.createBlobServiceWithSas(blobSvc.host.primaryHost + '/' + 'mycontainer' + '?' + sharedAccessSignatureToken);

    sharedBlobService.createBlockBlobFromText('mycontainer', req.body.name, req.body.file, function(error, result, response) {
        if (error) {
            res.send(error);
            return;
        }
        res.send(result);
    });

} 

但是我收到了这个错误。

{
    "code": "ResourceNotFound",
    "statusCode": 404,
    "requestId": "bffa6099-0001-000b-53f9-80d18a000000"
}

1 个答案:

答案 0 :(得分:3)

需要做出一些改变:

  • 到期日期应大于开始日期。
  • 共享访问签名应使用相同的容器名称。
  • 共享访问签名应该以文件名传递,而不是文件内容。

完成这些更改后,代码将与此类似:

var sharedAccessSignatureToken = blobSvc.generateSharedAccessSignature('mycontainer', req.body.name, sharedAccessPolicy);
var sharedBlobService = azure.createBlobServiceWithSas(blobSvc.host, sharedAccessSignatureToken);

sharedBlobService.createBlockBlobFromText('mycontainer', req.body.name, req.body.file, function(error, result, response) {
        if (error) {
            res.send(error);
            return;
        }
        res.send(result);
    });