google storage api | fetch file or stream | copy a file in storage bucket | Search

This code provides a utility function called streamToGoogle that uploads files or streams to a specified Google Cloud Storage bucket, handling bucket creation and metadata.

Run example

npm run import -- "upload files to google cloud"

upload files to google cloud

var mime = require('mime-types');
var path = require('path');
var {Storage} = require("@google-cloud/storage");
var importer = require('../Core');
var createBucket = importer.import("create a bucket");
var streamToOutput = importer.import("test stream to output");
var fetchOrStream = importer.import("fetch file or stream");

var storage = new Storage({  
  projectId: process.env.GOOGLE_STORAGE_PROJECT,  
  keyFilename: process.env.GOOGLE_STORAGE_CREDENTIALS
});

function streamToGoogle(fileUrl, bucketName, stream, metadata) {
    var gcsname = (stream ? fileUrl : path.basename(fileUrl)).replace(/\?.*/ig, '');
    console.log('project', storage.projectId, process.env.GOOGLE_STORAGE_PROJECT)
    return createBucket(storage.projectId, bucketName)
        .then(() => storage.bucket(bucketName).file(gcsname)
            .createWriteStream({
                metadata: Object.assign({
                    contentType: mime.lookup(gcsname)
                }, metadata || {})
            }))
        .then(writeStream => fetchOrStream(stream || fileUrl, writeStream))
        .then(() => `https://storage.googleapis.com/${bucketName}/${gcsname}`)
}

module.exports = streamToGoogle;

What the code could have been:

const mime = require('mime-types');
const path = require('path');
const { Storage } = require('@google-cloud/storage');
const importer = require('../Core');
const { createBucket, streamToOutput, fetchOrStream } = importer;

const storageConfig = {
  projectId: process.env.GOOGLE_STORAGE_PROJECT,
  keyFilename: process.env.GOOGLE_STORAGE_CREDENTIALS
};

class GoogleStorage {
  constructor(storageConfig) {
    this.storage = new Storage(storageConfig);
  }

  async createBucket(bucketName) {
    try {
      await createBucket(this.storage.projectId, bucketName);
      return this.storage.bucket(bucketName);
    } catch (error) {
      console.error(`Error creating bucket ${bucketName}:`, error);
      throw error;
    }
  }

  async streamToGoogle(fileUrl, bucketName, stream, metadata) {
    const gcsname = (stream? fileUrl : path.basename(fileUrl)).replace(/\?.*/ig, '');
    const bucket = await this.createBucket(bucketName);
    const file = bucket.file(gcsname);
    const writeStream = file.createWriteStream({
      metadata: Object.assign({
        contentType: mime.lookup(gcsname)
      }, metadata || {})
    });

    await fetchOrStream(stream || fileUrl, writeStream);
    const url = `https://storage.googleapis.com/${bucketName}/${gcsname}`;
    await streamToOutput(writeStream);
    return url;
  }
}

const googleStorage = new GoogleStorage(storageConfig);

module.exports = { googleStorage, streamToGoogle: googleStorage.streamToGoogle };

This code defines a utility function called streamToGoogle that uploads a file or stream to a Google Cloud Storage bucket.

Here's a breakdown:

  1. Dependencies:

  2. Imported Functions:

  3. Google Cloud Storage Setup:

  4. streamToGoogle Function:

  5. Export:

In essence, this code provides a reusable function for uploading files or streams to Google Cloud Storage, handling bucket creation and metadata setting.