ReadableStream 未定义

A B*_*A B 5 javascript node.js

我想在 azure 中加密一些 blob,并且我正在使用 Streams API。我正在从容器中读取 blob 存储,并尝试将加密的 blob 推送到其他容器中。

尽管new ReadableStream()的实例已被识别,但我还是记录了错误“ReadableStream 未定义” 任何对此的帮助,我们都非常欢迎!

代码

const { BlobServiceClient } = require('@azure/storage-blob');
const openpgp = require('openpgp');
const {privateKey, publicKey} = require('./keys')


async function main() {

    const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
    const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
    const containerClient = blobServiceClient.getContainerClient("uploadebs");
    const containerEncryptedFiles = blobServiceClient.getContainerClient("encrypted-dide");
    await containerEncryptedFiles.createIfNotExists("encrypted-dide")
    // RSA keys
    // console.log(`private key => ${privateKey}`)
    // console.log(`public key => ${publicKey}`)
    // .then((keyPair) => {console.log(`${JSON.stringify(keyPair)}` )}).catch((error) => console.log(`error in generating the keys: ${error}`));


    for await (const blob of containerClient.listBlobsFlat()) {

        if (blob.name.match('^DIDE*')) {
            const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
            const encryptedblockBlobClient = containerEncryptedFiles.getBlockBlobClient(blob.name)
            const downloadBlockBlobResponse = await blockBlobClient.download(0);
            let blobData = await streamToString(downloadBlockBlobResponse.readableStreamBody)
            const readableStream = new ReadableStream({
                start(controller) {
                    controller.enqueue(blobData);
                    controller.close();
                }
            });
            const encrypted = await openpgp.encrypt({
                message: await openpgp.createMessage({ text: readableStream }), // input as Message object
                publicKeys: publicKey, // for encryption
                privateKeys: privateKey // for signing (optional)
            });
            console.log(encrypted);
            await encryptedblockBlobClient.upload(encrypted, encrypted.length)// the blob should be already crypted

        }
    }

}

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);
    });
}

main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));

Run Code Online (Sandbox Code Playgroud)

包.json

{   .
    .
    .
    "dependencies": {
        "@azure/storage-blob": "^12.0.0",
        "@types/dotenv": "^4.0.3",
        "dotenv": "^6.0.0",
        "node": "^16.4.0",
        "openpgp": "^5.0.0-3"
    }

Run Code Online (Sandbox Code Playgroud)

这里是 azure webjob 的完整代码,用于使用 Node.Js 将 blob 加密到存储帐户中