public storeImage(buffer: Buffer): Promise<string> { const blobStream = new ReadableStreamBuffer() blobStream.put(buffer) blobStream.stop() const storage = gcs({ credentials: process.env.GOOGLE_CLOUD_CREDENTIALS_JSON ? JSON.parse(process.env.GOOGLE_CLOUD_CREDENTIALS_JSON) : undefined, projectId: process.env.GOOGLE_CLOUD_PROJECT, }) const bucketName = process.env.GOOGLE_CLOUD_BUCKET const bucket = storage.bucket(bucketName) const key = this.randomPath() const file = bucket.file(key) return new Promise<string>((resolve, reject) => { blobStream .pipe(file.createWriteStream({public: true})) .on("error", (err: any) => { reject(`Google Cloud Storage Error: ${JSON.stringify(err)}`) }).on("finish", () => { resolve(`https://storage.googleapis.com/${bucketName}/${key}`) }) }) }
return new Promise<string>((resolve, reject) => { blobStream .pipe(file.createWriteStream({public: true})) .on("error", (err: any) => { reject(`Google Cloud Storage Error: ${JSON.stringify(err)}`) }).on("finish", () => { resolve(`https://storage.googleapis.com/${bucketName}/${key}`) }) })
myWritableStreamBuffer.size(); myWritableStreamBuffer.maxSize(); // Gets all held data as a Buffer. myWritableStreamBuffer.getContents(); // Gets all held data as a utf8 string. myWritableStreamBuffer.getContentsAsString('utf8'); // Gets first 5 bytes as a Buffer. myWritableStreamBuffer.getContents(5); // Gets first 5 bytes as a utf8 string. myWritableStreamBuffer.getContentsAsString('utf8', 5); var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({ frequency: 10, // in milliseconds. chunkSize: 2048 // in bytes. }); myReadableStreamBuffer.put('A String', 'utf8'); myReadableStreamBuffer.put(buffer); myReadableStreamBuffer.on('data', function(data) { // streams1.x style data // assert.isTrue(data instanceof Buffer); }); myReadableStreamBuffer.put('the last data this stream will ever see'); myReadableStreamBuffer.stop();