从 azure 存储获取文件并通过 Post 方法发送它会抛出错误,但适用于本地文件

Getting file from azure storage and sending it via Post method throw error but works with local files

我已通过 post 方法向服务器发出请求。

文件在本地时有效

这是有效的请求

var options = {
    'method': 'POST',
    'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
    'headers': {
        'Content-Type': 'multipart/form-data',
        'Authorization': `Bearer ${tokenResponse.accessToken} `
    },
    formData: {
        '': {
            'value': fs.createReadStream('/Users/userName/Downloads/file.pbix'),
            'options': {
                'filename': 'file.pbix',
                'contentType': null
            }
        }
    }
};
request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
});

现在我将更改它以从 Azure 存储中获取文件

const containerClient = blobServiceClient.getContainerClient('tenants');
const baseUrl = containerClient.url
const blobClient = containerClient.getBlobClient('file/Url/On/AzureBlob')
let blobData
try {
    blobData = await blobClient.download(0)
    console.log(blobData)

} catch (error) {
    console.log(error)
}

var options = {
    'method': 'POST',
    'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
    'headers': {
        'Content-Type': 'multipart/form-data',
        'Authorization': `Bearer ${tokenResponse.accessToken} `
    },
    formData: {
        '': {
            'value': fs.createReadStream('blobData.blobDownloadStream'),
            'options': {
                'filename': 'file.pbix',
                'contentType': null
            }
        }
    }
};
request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
});

这里我从服务器 (MS Server) 收到一条错误消息

'{"error":{"code":"UnknownError","pbi.error":{"code":"UnknownError","parameters":{},"details":[],"exceptionCulprit":1}}}'

我猜错误来自于我下载文件并将其发送到服务器的方式。

有没有办法为 AzureBlobClient 获取与 fs.createReadStream 相同的文件 type/format?

您应该为 fs.createReadStream(some path) 指定文件路径,但在第二种情况下,您提供的字符串不是有效的文件路径。

所以基本上,如果您想在代码中使用 fs.createReadStream,您应该将文件作为临时文件下载到本地,然后再上传。试试下面的代码:

const fileName = "blob Name"
const containerClient = blobServiceClient.getContainerClient('tenants')
const blobClient = containerClient.getBlobClient(fileName)
const tempFilePath = "/some local temp path/" + fileName

blobClient.downloadToFile(tempFilePath).then(function(){
    console.log("file downloaded")
    var options = {
        'method': 'POST',
        'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
        'headers': {
            'Content-Type': 'multipart/form-data',
            'Authorization': `Bearer ${tokenResponse.accessToken} `
        },
        formData: {
            '': {
                'value': fs.createReadStream(tempFilePath),
                'options': {
                    'filename': 'file.pbix',
                    'contentType': null
                }
            }
        }
    };
    request(options, function (error, response) {
        if (error) throw new Error(error);
        console.log(response.body);
    });

    //remove temp file after upload
    fs.unlinkSync(tempFilePath)
})