【问题标题】:Editing a file in Azure Blob Storage with an API使用 API 编辑 Azure Blob 存储中的文件
【发布时间】:2020-05-17 14:10:24
【问题描述】:

这是我的场景。

我已将配置文件 (.xml) 放入 Azure Blob 存储容器中

我想编辑那个 xml 文件并更新/添加内容。

我想将一个 api 部署到一个 azure 应用程序服务来执行此操作。

我构建了一个在本地运行的 api 来处理这个问题,但这并不能完全将它作为一个云应用程序来削减。这个特定的迭代是一个 NODEjs api,它使用 Cheerio 和 File-System 模块来分别操作和读取文件。

我如何重新调整它以使用 Azure blob 存储中的文件?

注意:azure blob 是不是从文件开始的最佳位置?有更好的地方放吗?

我找到了这个,但这不是我想要的.....Azure Edit blob

【问题讨论】:

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


    【解决方案1】:

    考虑到存储在 blob 中的数据是 XML(也就是字符串类型),您可以使用 getBlobToText 方法,而不是使用 getBlobToStream 方法,操作字符串,然后使用 createBlockBlobFromText 上传更新后的字符串。

    这是伪代码:

    blobService.getBlobToText('mycontainer', 'taskblob', function(error, result, response) {
        if (error) {
            console.log('Error in reading blob');
            console.error(error);
        } else {
            var blobText = result;//It 
            var xmlContent = someMethodToConvertStringToXml(blobText);//Convert string to XML if it is easier to manipulate
            var updatedBlobText = someMethodToEditXmlContentAndReturnString(xmlContent);
            //Reupload blob
            blobService.createBlockBlobFromText('mycontainer', 'taskblob', updatedBlobText, function(error, result, response) {
                if (error) {
                    console.log('Error in updating blob');
                    console.error(error);
                } else {
                    console.log('Blob updated successfully');
                }
            });
        }
    });
    

    【讨论】:

      【解决方案2】:

      只需重构代码以使用 Azure Storage SDK for Node.js https://github.com/Azure/azure-storage-node

      【讨论】:

      • 所以我看到了,我认为这可能有效,但我可以确切地看到如何。把这块放在这里.... blobService.getBlobToStream('mycontainer', 'taskblob', fs.createWriteStream('output.txt'), function(error, result, response)) 我可以这样做并从中取出文件blob 存储,但它最终只是作为我机器上的本地文件,就像我下载它一样。我在那里误解/做错了什么吗?
      猜你喜欢
      • 2021-07-09
      • 2021-02-16
      • 2019-05-15
      • 2020-12-12
      • 2021-05-06
      • 2015-06-20
      • 2017-03-26
      • 2012-05-20
      • 1970-01-01
      相关资源
      最近更新 更多