【发布时间】:2019-05-31 00:59:49
【问题描述】:
我正在按照https://github.com/Azure/azure-storage-js/blob/master/blob/samples/basic.sample.js 此处的示例使用 Node.js SDK 从 Azure Blob 存储读取 Blob 到字符串。
我正在尝试读取的 blob 是 Append Blob。
首先将流读入字符串需要很长时间,最后我得到一个 HTTP 412 错误。
我也在这里问过这个问题:https://github.com/Azure/azure-storage-js/issues/51
我正在使用 Node.js v10.14.1 执行此操作,我使用的 SDK 是 @azure/storage-blob@10.3.0。
我的代码在这里:
const {
Aborter,
BlobURL,
ContainerURL,
SharedKeyCredential,
ServiceURL,
StorageURL,
} = require('@azure/storage-blob');
const format = require('date-fns/format');
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on('data', (data) => {
chunks.push(data.toString());
});
readableStream.on('end', () => {
resolve(chunks.join(''));
});
readableStream.on('error', reject);
});
}
async function run() {
const accountName = 'xxxstor';
const accountKey = 'omitted';
const credential = new SharedKeyCredential(accountName, accountKey);
const pipeline = StorageURL.newPipeline(credential);
const serviceURL = new ServiceURL(
`https://${accountName}.blob.core.windows.net`,
pipeline
);
const containerName = 'request-logs';
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blobName = `${format(new Date(), 'YYYY-MM-DD[.txt]')}`;
const blobURL = BlobURL.fromContainerURL(containerURL, blobName);
console.log('Downloading blob...');
const response = await blobURL.download(Aborter.none, 0);
console.log('Reading response to string...');
const body = await streamToString(response.);
console.log(body.length);
}
run().catch((err) => {
console.error(err);
});
我得到的错误是这样的:
{ Error: Unexpected status code: 412
at new RestError (C:\projects\xxx\RequestLogViewer\node_modules\@azure\ms-rest-js\dist\msRest.node.js:1397:28)
at C:\projects\xxx\RequestLogViewer\node_modules\@azure\ms-rest-js\dist\msRest.node.js:1849:37
at process._tickCallback (internal/process/next_tick.js:68:7)
code: undefined,
statusCode: 412,
request:
WebResource {
streamResponseBody: true,
url:
'https://xxxstor.blob.core.windows.net/request-logs/2019-01-04.txt',
method: 'GET',
headers: HttpHeaders { _headersMap: [Object] },
body: undefined,
query: undefined,
formData: undefined,
withCredentials: false,
abortSignal:
a {
_aborted: false,
children: [],
abortEventListeners: [Array],
parent: undefined,
key: undefined,
value: undefined },
timeout: 0,
onUploadProgress: undefined,
onDownloadProgress: undefined,
operationSpec:
{ httpMethod: 'GET',
path: '{containerName}/{blob}',
urlParameters: [Array],
queryParameters: [Array],
headerParameters: [Array],
responses: [Object],
isXML: true,
serializer: [Serializer] } },
response:
{ body: undefined,
headers: HttpHeaders { _headersMap: [Object] },
status: 412 },
body: undefined }
【问题讨论】:
标签: node.js azure azure-blob-storage