async uploadAsFile()

in src/QLDBKVS.ts [206:251]


    async uploadAsFile(key: string, filePath: string): Promise<UpsertResult> {
        const fcnName = "[QLDBKVS.uploadAsFile]";
        const self: QLDBKVS = this;
        const ledgerName: string = self.ledgerName;
        const tableName: string = self.tableName;
        const paramId: string = key;
        const startTime: number = new Date().getTime();
        try {
            if (!paramId) {
                throw new Error(`${fcnName}: Please specify key`);
            }
            if (!filePath) {
                throw new Error(`${fcnName}: Please specify filePath`);
            }
            logger.debug(`${fcnName} Start uploading file ${filePath} to ledger ${ledgerName} and table ${tableName} under the key ${paramId}`);

            let doc: { [k: string]: any } = {};
            doc[KEY_ATTRIBUTE_NAME] = key;
            const fileBuffer = await readFile(filePath);
            doc[VALUE_ATTRIBUTE_NAME] = fileBuffer.toString("base64");

            const documentObjectSize: number = doc[KEY_ATTRIBUTE_NAME].length + doc[VALUE_ATTRIBUTE_NAME].length;

            if (documentObjectSize > MAX_QLDB_DOCUMENT_SIZE) {
                logger.info(`${fcnName} Unable to upload files larger than ${MAX_QLDB_DOCUMENT_SIZE} bytes. Current size: ${documentObjectSize}`);
                return null;
            }
            logger.debug(`${fcnName} Length of an object is ${documentObjectSize}`);

            await this.assureTableExists(tableName);

            return await this.qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
                return await upsert(txn, tableName, KEY_ATTRIBUTE_NAME, doc).catch((err) => {
                    throw err
                });
            })
        } catch (err) {
            const msg = `Could not upload the file`;
            logger.error(`${fcnName} ${msg}: ${err}`);
            throw new Error(msg);
        } finally {
            const endTime: number = new Date().getTime();
            logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        }

    }