tweedegolf / storage-abstraction

Provides an abstraction layer for interacting with a storage; the storage can be local or in the cloud.
MIT License
109 stars 18 forks source link

Impossible to add parameters while storing a file #9

Closed lambertkevin closed 1 year ago

lambertkevin commented 4 years ago

At the moment the store method doesn't allow for any additional arguments, which means you can't add metadatas, use ACL, Expires, etc... at least for compatible StorageTypes.

A good way to implement it would probably be to allow an additional parameter to the store methods and use destruct to create the params variable.

protected async store(arg: string | Buffer | Readable, targetPath: string, additionalParams: object = {}): Promise<void> {
    if (!this.bucketName) {
      throw new Error("no bucket selected");
    }
    await this.createBucket(this.bucketName);
    let readable: Readable;
    if (typeof arg === "string") {
      readable = fs.createReadStream(arg);
    } else if (arg instanceof Buffer) {
      readable = new Readable();
      readable._read = (): void => {}; // _read is required but you can noop it
      readable.push(arg);
      readable.push(null);
    } else if (arg instanceof Readable) {
      readable = arg;
    }
    const params = {
      ...additionalParams,
      Bucket: this.bucketName,
      Key: targetPath,
      Body: readable,
    };
    await this.storage.upload(params).promise();
  }

additionalParams being at the top of the destruct to avoid conflicts with Bucket, Key and Body

airhorns commented 4 years ago

Plus one for this!

abudaan commented 1 year ago

In version 1.4.5 you can specify an optional options object to the methods addFileFromPath, addFileFromBuffer and addFileFromReadable

For example:

addFileFromPath(filePath: string, targetPath: string, options?: object): Promise<string>;