Node.js使用.createBlockBlobFromLocalFile()将base64图像上传到Azure Blob存储

Node.js使用.createBlockBlobFromLocalFile()将base64图像上传到Azure Blob存储

问题描述:

我想上传通过Base64表单从网络应用程序和移动应用程序发送的用户的个人资料图片.

I want to upload profile picture of a user sent from web app and mobile app via Base64 form.

POST请求上,他们需要在看起来像这样的正文上发送JSON.

On the POST request they need to send a JSON on the body that looks something like this.

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

现在在服务器端使用NodeExpress,我使用了名为azure-storage的npm模块,该模块提供了一种使用Web服务将文件上传到Azure Blob存储的好方法.

Now on the server side using Node and Express, I used this npm module called azure-storage which offers a nice way of uploading files to azure blob storage using web service.

但是对此我有些不了解.这是我的控制器中的一部分代码.我成功创建了所有必要的连接和键,并创建了一个有效的blobService:

But there's something that I cannot understand on this. Here's a part of the code from my controller. I successfully created all neccessary connections and keys and whatnot to create a working blobService :

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

    // ...
    // generated some sastoken up here
    // etc.
    // ...

    var uploadOptions = {
        container: 'mycontainer',
        blob: req.body.name, // im not sure about this
        path: req.body.file // im not sure about this either
    }

    sharedBlobService.createBlockBlobFromLocalFile(uploadOptions.container, uploadOptions.blob, uploadOptions.path, function(error, result, response) {
        if (error) {
            res.send(error);
        }
        console.log("result", result);
        console.log("response", response);
    });
}

出现此错误:

{
    "errno": 34,
    "code": "ENOENT",
    "path": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAAB..."
}

在这种情况下,您不应使用createBlockBlobFromLocalFile.相反,您应该使用createBlockBlobFromText,因为您不是上载本地文件,而是上载请求正文中的内容.

In this case, you should not use createBlockBlobFromLocalFile. Instead, you should use createBlockBlobFromText, because you are not uploading a local file, but content in the request body.

这是代码:

var uploadOptions = {
    container: 'mycontainer',
    blob: req.body.name, 
    text: req.body.file 
}

sharedBlobService.createBlockBlobFromText(uploadOptions.container, 
                                           uploadOptions.blob, 
                                           uploadOptions.text, 
                      {
                         contentType: 'image/jpeg',
                         contentEncoding: 'base64'
                      }, 
                      function(error, result, response) {
                           if (error) {
                               res.send(error);
                           }
                           console.log("result", result);
                           console.log("response", response);
                      });

blob只是文件名,在这种情况下为"profile-pic-123.jpg",而path是文件的本地路径.由于您不是在服务器端本地存储文件,因此path在这种情况下是没有意义的.

The blob is just the file name, which is "profile-pic-123.jpg" this case, and path is the local path to your file. Since you are not storing the file locally in the server side, path is meaningless in the case.

如果您需要有关存储的更多信息,请参见

If you need more information about Storage, see this, and this