@google-cloud/storage#CreateWriteStreamOptions TypeScript Examples

The following examples show how to use @google-cloud/storage#CreateWriteStreamOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gcloud-storage.service.ts    From nestjs-gcloud-storage with MIT License 6 votes vote down vote up
async upload(
    fileMetadata: UploadedFileMetadata,
    perRequestOptions: Partial<GCloudStoragePerRequestOptions> = null,
  ): Promise<string> {
    const filename = uuid();
    const gcFilename =
      perRequestOptions && perRequestOptions.prefix ? join(perRequestOptions.prefix, filename) : filename;
    const gcFile = this.bucket.file(gcFilename);

    // override global options with the provided ones for this request
    perRequestOptions = {
      ...this.options,
      ...perRequestOptions,
    };

    const writeStreamOptions = perRequestOptions && perRequestOptions.writeStreamOptions;

    const { predefinedAcl = 'publicRead' } = perRequestOptions;
    const streamOpts: CreateWriteStreamOptions = {
      predefinedAcl: predefinedAcl,
      ...writeStreamOptions,
    };

    const contentType = fileMetadata.mimetype;

    if (contentType) {
      streamOpts.metadata = { contentType };
    }

    return new Promise((resolve, reject) => {
      gcFile
        .createWriteStream(streamOpts)
        .on('error', (error) => reject(error))
        .on('finish', () => resolve(this.getStorageUrl(gcFilename, perRequestOptions)))
        .end(fileMetadata.buffer);
    });
  }