async downloadAsFile()

in src/QLDBKVS.ts [151:196]


    async downloadAsFile(key: string, localFilePath: string): Promise<string> {
        const fcnName = "[QLDBKVS.downloadAsFile]";
        const self: QLDBKVS = this;
        const ledgerName: string = self.ledgerName;
        const tableName: string = self.tableName;
        const paramId: string = key;
        const filePath: string = localFilePath ? localFilePath : DEFAULT_DOWNLOADS_PATH + key;
        const startTime: number = new Date().getTime();
        //const qldbHelper: QLDBHelper = this.qldbHelper;
        try {
            if (!paramId) {
                throw new Error(`${fcnName}: Please specify key`);
            }
            if (!localFilePath) {
                throw new Error(`${fcnName}: Please specify localFilePath`);
            }

            logger.debug(`${fcnName} Getting ${paramId} from ledger ${ledgerName} and table ${tableName} to ${filePath}`);

            if (!localFilePath && !fs.existsSync(DEFAULT_DOWNLOADS_PATH)) {
                await mkdir(DEFAULT_DOWNLOADS_PATH);
            }

            const result: GetDocumentResult[] = await this.qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
                return await getByKeyAttribute(txn, tableName, KEY_ATTRIBUTE_NAME, paramId).catch((err) => {
                    throw `Unable to get file By Key Attribute: ${err}`;
                });
            });

            const valueBase64: string = result[0].data.get(VALUE_ATTRIBUTE_NAME).stringValue();
            const valueObject: Buffer = Buffer.from(valueBase64, "base64");

            await writeFile(localFilePath, valueObject);

            return localFilePath;
        } catch (err) {
            //throw `${fcnName}: ${err}`;
            const msg = `Requested document does not exist`;
            logger.error(`${fcnName} ${msg}: ${err}`);
            throw new Error(msg);
        } finally {
            const endTime: number = new Date().getTime();
            logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        }

    }