diff --git a/test/test.ts b/test/test.ts index 8fe1397..ebacac4 100644 --- a/test/test.ts +++ b/test/test.ts @@ -33,7 +33,7 @@ tap.test('should get a bucket', async () => { // Fast operations tap.test('should store data in bucket fast', async () => { - await myBucket.fastStore('hithere/socool.txt', 'hi there!'); + await myBucket.fastPut('hithere/socool.txt', 'hi there!'); }); tap.test('should get data in bucket', async () => { @@ -49,12 +49,12 @@ tap.test('should delete data in bucket', async () => { // fs operations tap.test('prepare for directory style tests', async () => { - await myBucket.fastStore('dir1/file1.txt', 'dir1/file1.txt content'); - await myBucket.fastStore('dir1/file2.txt', 'dir1/file2.txt content'); - await myBucket.fastStore('dir2/file1.txt', 'dir2/file1.txt content'); - await myBucket.fastStore('dir3/file1.txt', 'dir3/file1.txt content'); - await myBucket.fastStore('dir3/dir4/file1.txt', 'dir3/dir4/file1.txt content'); - await myBucket.fastStore('file1.txt', 'file1 content'); + await myBucket.fastPut('dir1/file1.txt', 'dir1/file1.txt content'); + await myBucket.fastPut('dir1/file2.txt', 'dir1/file2.txt content'); + await myBucket.fastPut('dir2/file1.txt', 'dir2/file1.txt content'); + await myBucket.fastPut('dir3/file1.txt', 'dir3/file1.txt content'); + await myBucket.fastPut('dir3/dir4/file1.txt', 'dir3/dir4/file1.txt content'); + await myBucket.fastPut('file1.txt', 'file1 content'); }); tap.test('should get base directory', async () => { diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e817570..cbe67d0 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbucket', - version: '2.0.4', - description: 'simple cloud independent object storage' + version: '2.0.5', + description: 'A TypeScript library for simple cloud independent object storage with support for buckets, directories, and files.' } diff --git a/ts/smartbucket.classes.bucket.ts b/ts/smartbucket.classes.bucket.ts index bbf4e2e..62335f2 100644 --- a/ts/smartbucket.classes.bucket.ts +++ b/ts/smartbucket.classes.bucket.ts @@ -49,7 +49,7 @@ export class Bucket { /** * store file */ - public async fastStore(pathArg: string, fileContent: string | Buffer): Promise { + public async fastPut(pathArg: string, fileContent: string | Buffer): Promise { const streamIntake = new plugins.smartstream.StreamIntake(); const putPromise = this.smartbucketRef.minioClient .putObject(this.name, pathArg, streamIntake.getReadable()) @@ -115,6 +115,59 @@ export class Bucket { return replaySubject; } + /** + * store file as stream + */ + public async fastPutStream(optionsArg: { + pathArg: string; + dataStream: plugins.stream.Readable; + metadata?: { [key: string]: string }; + }): Promise { + await this.smartbucketRef.minioClient.putObject( + this.name, + optionsArg.pathArg, + optionsArg.dataStream, + null, + ...(optionsArg.metadata + ? (() => { + const returnObject: any = {}; + return returnObject; + })() + : {}) + ); + } + + public async updateMetadata( + bucket: string, + objectKey: string, + metadata: { [key: string]: string } + ): Promise { + try { + // Retrieve current object information to use in copy conditions + const currentObjInfo = await this.smartbucketRef.minioClient.statObject(bucket, objectKey); + + // Setting up copy conditions + const copyConditions = new plugins.minio.CopyConditions(); + + // Prepare new metadata, merging current and new metadata + const newMetadata = { + ...currentObjInfo.metaData, + ...metadata, + }; + + // Define the copy operation as a Promise + await this.smartbucketRef.minioClient.copyObject( + bucket, + objectKey, + `/${bucket}/${objectKey}`, + copyConditions, + ); + } catch (err) { + console.error('Error updating metadata:', err); + throw err; // rethrow to allow caller to handle + } + } + /** * removeObject */ diff --git a/ts/smartbucket.classes.directory.ts b/ts/smartbucket.classes.directory.ts index 39f6aa4..ba82265 100644 --- a/ts/smartbucket.classes.directory.ts +++ b/ts/smartbucket.classes.directory.ts @@ -183,7 +183,7 @@ export class Directory { // file operations public async fastStore(pathArg: string, contentArg: string | Buffer) { const path = plugins.path.join(this.getBasePath(), pathArg); - await this.bucketRef.fastStore(path, contentArg); + await this.bucketRef.fastPut(path, contentArg); } public async fastGet(pathArg: string) { diff --git a/ts/smartbucket.plugins.ts b/ts/smartbucket.plugins.ts index f40d1ba..2fb4845 100644 --- a/ts/smartbucket.plugins.ts +++ b/ts/smartbucket.plugins.ts @@ -1,7 +1,8 @@ // node native import * as path from 'path'; +import * as stream from 'stream'; -export { path }; +export { path, stream }; // @push.rocks scope import * as smartpath from '@push.rocks/smartpath';