【问题标题】:js array to stream for azure blobservice createBlockBlobFromStreamjs 数组流式传输 azure blobservice createBlockBlobFromStream
【发布时间】:2017-09-24 23:24:33
【问题描述】:

我有一个数组,我想使用 createBlockBlobFromStream 将内容上传到 azure。

示例代码:

var myStream = getSomeStream();
var myStreamLength = getSomeStreamLength();
blobService.createBlockBlobFromStream(
    containerName,
    'my-awesome-stream-blob',
    myStream,
    myStreamLength,
    function(error, result, response){
        if(error){
            console.log("Couldn't upload stream");
            console.error(error);
        } else {
            console.log('Stream uploaded successfully');
        }
    });

但是我不知道如何从数组中生成 myStream 和 myStreamLength。

【问题讨论】:

  • 您可以做的一件事是将数组序列化为字符串 (JSON.stringify(array)),然后使用 createBlockBlobFromText 上传。这会是一个可以接受的解决方案吗?
  • @GauravMantri 不幸的是没有,因为数据超过了 FromText 函数的 64MB 限制

标签: javascript azure stream azure-blob-storage


【解决方案1】:

您可能希望使用 createWriteStreamToBlockBlob 函数,这可能更容易将流上传到 Azure 存储。您不再需要担心那里的流长度。然后您可以使用以下代码从数组生成流。

var fs = require('fs');
var azure = require('azure-storage');
var Readable = require('stream').Readable;

var accountName = "youraccountname";
var accessKey = "youraccountkey";
var host = "https://yourhost.blob.core.windows.net";
var blobSvc = azure.createBlobService(accountName, accessKey, host);

var myArray = ["Saab", "Volvo", "BMW", 788, 12.3];

var rs = Readable();

for(item of myArray) {
    rs.push(String(item));
}
rs.push(null);  // indicates end-of-file basically - the end of the stream

rs.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', 'mystream.txt'));

See here 了解有关如何正确使用流的更多信息。

【讨论】:

    猜你喜欢
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2019-11-08
    • 2021-10-04
    相关资源
    最近更新 更多