如何使用node.js在Azure文件存储中上传文件?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (186)

我们正在尝试使用node.js服务创建一个web服务来将文件上传到Azure文件存储。

以下是node.js服务器代码。

exports.post = function(request, response){
var shareName = request.headers.sharename;
var dirPath = request.headers.directorypath;
var fileName = request.headers.filename;

var body;
var length;

request.on("data", function(chunk){
    body += chunk;
    console.log("Get data");
});


request.on("end", function(){
    try{
        console.log("end");
        var data = body;
        length = data.length;

console.log(body); // This giving the result as undefined
console.log(length);

        fileService.createFileFromStream(shareName, dirPath, fileName, body, length, function(error, result, resp) {
            if (!error) {
                // file uploaded
                response.send(statusCodes.OK, "File Uploaded");
            }else{
                response.send(statusCodes.OK, "Error!");
            }
        });

    }catch (er) {
response.statusCode = 400;
return res.end('error: ' + er.message);
}

});

}

以下是我们的客户端上传文件。

private static void sendPOST() throws IOException {
    URL obj = new URL("https://crowdtest-fileservice.azure-mobile.net/api/files_stage/");
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("sharename", "newamactashare");
    con.setRequestProperty("directorypath", "MaheshApp/TestLibrary/");
    con.setRequestProperty("filename", "temp.txt");


    Path path = Paths.get("C:/Users/uma.maheshwaran/Desktop/Temp.txt");
    byte[] data = Files.readAllBytes(path);

    // For POST only - START
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(data);
    os.flush();
    os.close();
    // For POST only - END

    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { // success
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
            System.out.println(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("POST request not worked");
    }
}

它显示错误

The request 'POST /api/files_stage/' has timed out. This could be caused by a script that fails to write to the response, or otherwise fails to return from an asynchronous call in a timely manner.

尝试了下面的代码。

  var body = new Object();
  body = request.body;
  var length = body.length;

  console.log(request.body);
  console.log(body);
  console.log(length);

    try {
        fileService.createFileFromStream(shareName, dirPath, fileName, body, length, function(error, result, resp) {
            if (!error) {
                // file uploaded
                response.send(statusCodes.OK, "File Uploaded");
            }else{
                response.send(statusCodes.OK, "Error!");
            }
        });
    } catch (ex) {
            response.send(500, { error: ex.message });
    }

但面临这个问题

{"error":"Parameter stream for function createFileFromStream should be an object"}

请帮我解决这个问题。

提问于
用户回答回答于

这里有几个问题。让我们一个接一个地去看看他们。

1.在Java客户端中,不能将二进制数据转储到Azure移动服务连接。

这是因为Azure移动服务具有两个主体解析器,可以确保无论如何都可以为解析请求主体。因此,虽然可以通过指定一个不常见的内容类型来绕过Express body解析器,但仍然会击中Azure body解析器,这会认为它是UTF-8字符串来混淆数据流。

因此,唯一的选择是通过指定它无法处理的内容类型来跳过Express解析器,然后通过使用Base64编码对二进制数据进行编码来与Azure解析器一起播放。

所以,在Java客户端替换

Path path = Paths.get("C:/Users/uma.maheshwaran/Desktop/Temp.txt");
byte[] data = Files.readAllBytes(path);

con.setRequestProperty("content-type", "binary");    
Path path = Paths.get("C:/Users/uma.maheshwaran/Desktop/Temp.txt");
byte[] data = Files.readAllBytes(path);
data = Base64.getEncoder().encode(data);

如果不在Java 8上,请将java.util.Base64编码器替换为有权访问的任何其他Base64编码器。

2. createFileFromStream尝试使用的Azure存储空间API函数需要一个流。

同时,手动解析请求体时可以获得最好的结果是一个字节数组。不幸的是,Azure移动服务使用NodeJS 0.8版,这意味着没有简单的方法来从字节数组构建可读流,而且必须组装适用于Azure存储API的自己的流。

var base64 = require('base64-js'),
    Stream = require('stream'),
    fileService = require('azure-storage')
        .createFileService('yourStorageAccount', 'yourStoragePassword');

exports.post = function (req, res) {
    var data = base64.toByteArray(req.body),
        buffer = new Buffer(data),
        stream = new Stream();
        stream['_ended'] = false;
        stream['pause'] = function() {
            stream['_paused'] = true;
        };
        stream['resume'] = function() {
            if(stream['_paused'] && !stream['_ended']) {
                stream.emit('data', buffer);
                stream['_ended'] = true;
                stream.emit('end');
            }
        }; 
    try {
        fileService.createFileFromStream(req.headers.sharename, req.headers.directorypath, 
            req.headers.filename, stream, data.length, function (error, result, resp) {
                res.statusCode = error ? 500 : 200;
                res.end();
            }
        );
    } catch (e) {
        res.statusCode = 500;
        res.end();
    }
};

这些是需要用于此示例的依赖关系。

"dependencies": {   
    "azure-storage": "^0.7.0",
    "base64-js": "^0.0.8",
    "stream": "0.0.1"
}

如果在服务的package.json中指定它们不起作用,可以通过控制台手动安装它们。

cd site\wwwroot
npm install azure-storage
npm install base64-js
npm install stream@0.0.1

3.要增加1Mb的默认上传限制,请为服务指定MS_MaxRequestBodySizeKB。

不过请记住,由于将数据以Base64编码方式传输,因此必须考虑此开销。所以,要支持上传大小为20Mb的文件,必须将其设置MS_MaxRequestBodySizeKB为大约20 * 1024 * 4/3 = 27307。

用户回答回答于

我发现最简单的方法是使用pkgcloud,它抽象了云提供者之间的差异,并为上传和下载文件提供了一个干净的界面。它使用流,因此实现的内存效率也很高。

var pkgcloud = require('pkgcloud')
var fs = require('fs')
var client = pkgcloud.storage.createClient({
  provider: 'azure',
  storageAccount: 'your-storage-account',
  storageAccessKey: 'your-access-key'
});

var readStream = fs.createReadStream('a-file.txt');
var writeStream = client.upload({
  container: 'your-storage-container',
  remote: 'remote-file-name.txt'
});

writeStream.on('error', function (err) {
  // handle your error case
});

writeStream.on('success', function (file) {
  // success, file will be a File model
});

readStream.pipe(writeStream);

扫码关注云+社区

领取腾讯云代金券