@google-cloud/storage#Storage TypeScript Examples

The following examples show how to use @google-cloud/storage#Storage. 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: fileUploader.ts    From polkadot-watcher-csv-exporter with Apache License 2.0 6 votes vote down vote up
constructor(bucketUploadConfig: BucketUploadConfig, private readonly logger: Logger) {
    this.storageOptions = {
      keyFilename: bucketUploadConfig.gcpServiceAccount,
      projectId: bucketUploadConfig.gcpProject
    }
    
    this.storage = new Storage(this.storageOptions);
    this.bucket = this.storage.bucket(bucketUploadConfig.gcpBucketName);
  }
Example #2
Source File: gcs_store.ts    From CIAnalyzer with MIT License 6 votes vote down vote up
constructor(
    logger: Logger,
    service: string,
    projectId?: string,
    bucket?: string,
    filePath?: string
  ) {
    const fp = filePath ?? path.join(defaultDir, `${service}.json`)

    if (!projectId || !bucket) {
      throw new Error("Must need 'project' and 'bucket' params for lastRunStore in config")
    }

    this.logger = logger.getChildLogger({ name: GcsStore.name })

    const storage = new Storage({ projectId })
    this.file = storage.bucket(bucket).file(fp)
    this.gcsPath = `gs://${bucket}/${fp}`
  }
Example #3
Source File: index.ts    From Dimensions with MIT License 6 votes vote down vote up
/**
   * Initializer. Initializes the storage object and creates necessary buckets
   */
  async initialize(dimension: Dimension): Promise<void> {
    const bucketName =
      dimension.name.toLowerCase().replace(/ /g, '_') +
      '_' +
      dimension.id.toLowerCase();
    this.storage = new Storage({
      keyFilename: this.configs.keyFilename,
      projectId: this.configs.projectId,
    });
    const exists = await this.storage.bucket(bucketName).exists();
    if (!exists[0]) {
      this.log.system(`creating bucket ${bucketName}`);
      await this.storage.createBucket(bucketName);
    }
    this.dimensionBucket = this.storage.bucket(bucketName);
  }
Example #4
Source File: gcpProvider.service.ts    From amplication with Apache License 2.0 6 votes vote down vote up
getProvider(): GCPProvider | null {
    const gcpAppsProjectId = this.configService.get(GCP_APPS_PROJECT_ID_VAR);
    if (!gcpAppsProjectId) {
      return null;
    }
    const bucket = this.configService.get(GCS_BUCKET_VAR);
    return new GCPProvider(
      new CloudBuildClient(),
      new Storage(),
      gcpAppsProjectId,
      /** @todo prefix results */
      bucket
    );
  }
Example #5
Source File: gcp.ts    From airnode with MIT License 6 votes vote down vote up
export async function removeState(bucketName: string, cloudProvider: GcpCloudProvider) {
  logger.debug('Removing Terraform state from GCP');
  const { projectId } = cloudProvider;
  const storage = new Storage({ projectId });
  const bucket = storage.bucket(bucketName);

  await bucket.deleteFiles({ force: true, versions: true });
  await bucket.delete();
}
Example #6
Source File: google-cloud.service.ts    From aqualink-app with MIT License 6 votes vote down vote up
constructor(
    @InjectRepository(SurveyMedia)
    private surveyMediaRepository?: Repository<SurveyMedia>,
  ) {
    this.storage = new Storage({
      autoRetry: true,
      keyFilename: this.GCS_KEYFILE,
      projectId: this.GCS_PROJECT,
      maxRetries: 3,
    });
  }
Example #7
Source File: GoogleGcsUrlReader.ts    From backstage with Apache License 2.0 6 votes vote down vote up
static factory: ReaderFactory = ({ config, logger }) => {
    if (!config.has('integrations.googleGcs')) {
      return [];
    }
    const gcsConfig = readGoogleGcsIntegrationConfig(
      config.getConfig('integrations.googleGcs'),
    );
    let storage: Storage;
    if (!gcsConfig.clientEmail || !gcsConfig.privateKey) {
      logger.info(
        'googleGcs credentials not found in config. Using default credentials provider.',
      );
      storage = new Storage();
    } else {
      storage = new Storage({
        credentials: {
          client_email: gcsConfig.clientEmail || undefined,
          private_key: gcsConfig.privateKey || undefined,
        },
      });
    }
    const reader = new GoogleGcsUrlReader(gcsConfig, storage);
    const predicate = (url: URL) => url.host === GOOGLE_GCS_HOST;
    return [{ reader, predicate }];
  };
Example #8
Source File: googleStorage.ts    From backstage with Apache License 2.0 6 votes vote down vote up
constructor(options: {
    storageClient: Storage;
    bucketName: string;
    legacyPathCasing: boolean;
    logger: Logger;
    bucketRootPath: string;
  }) {
    this.storageClient = options.storageClient;
    this.bucketName = options.bucketName;
    this.legacyPathCasing = options.legacyPathCasing;
    this.logger = options.logger;
    this.bucketRootPath = options.bucketRootPath;
  }
Example #9
Source File: google-cloud.service.ts    From aqualink-app with MIT License 5 votes vote down vote up
private storage: Storage;
Example #10
Source File: fileUploader.ts    From polkadot-watcher-csv-exporter with Apache License 2.0 5 votes vote down vote up
private storage: Storage;
Example #11
Source File: createPiggybank.ts    From coindrop with GNU General Public License v3.0 5 votes vote down vote up
storage = new Storage({
  projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
  credentials: {
      client_email: process.env.FIREBASE_CLIENT_EMAIL,
      private_key: process.env.FIREBASE_PRIVATE_KEY,
  },
})
Example #12
Source File: storage.config.ts    From msclub-backend with GNU General Public License v3.0 5 votes vote down vote up
storage = new Storage({
	projectId: "ms-storage-server-fb22b",
	credentials: {
		client_email: configs.firebase.clientEmail,
		private_key: configs.firebase.privateKey.replace(/\\n/gm, "\n"),
	},
})
Example #13
Source File: storage.ts    From mail-my-ballot with Apache License 2.0 5 votes vote down vote up
storage = new Storage()
Example #14
Source File: artifacts.ts    From kfp-tekton-backend with Apache License 2.0 5 votes vote down vote up
function getGCSArtifactHandler(options: { key: string; bucket: string }, peek: number = 0) {
  const { key, bucket } = options;
  return async (_: Request, res: Response) => {
    try {
      // Read all files that match the key pattern, which can include wildcards '*'.
      // The way this works is we list all paths whose prefix is the substring
      // of the pattern until the first wildcard, then we create a regular
      // expression out of the pattern, escaping all non-wildcard characters,
      // and we use it to match all enumerated paths.
      const storage = new Storage();
      const prefix = key.indexOf('*') > -1 ? key.substr(0, key.indexOf('*')) : key;
      const files = await storage.bucket(bucket).getFiles({ prefix });
      const matchingFiles = files[0].filter(f => {
        // Escape regex characters
        const escapeRegexChars = (s: string) => s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
        // Build a RegExp object that only recognizes asterisks ('*'), and
        // escapes everything else.
        const regex = new RegExp(
          '^' +
            key
              .split(/\*+/)
              .map(escapeRegexChars)
              .join('.*') +
            '$',
        );
        return regex.test(f.name);
      });

      if (!matchingFiles.length) {
        console.log('No matching files found.');
        res.send();
        return;
      }
      console.log(
        `Found ${matchingFiles.length} matching files: `,
        matchingFiles.map(file => file.name).join(','),
      );
      let contents = '';
      // TODO: support peek for concatenated matching files
      if (peek) {
        matchingFiles[0]
          .createReadStream()
          .pipe(new PreviewStream({ peek }))
          .pipe(res);
        return;
      }

      // if not peeking, iterate and append all the files
      matchingFiles.forEach((f, i) => {
        const buffer: Buffer[] = [];
        f.createReadStream()
          .on('data', data => buffer.push(Buffer.from(data)))
          .on('end', () => {
            contents +=
              Buffer.concat(buffer)
                .toString()
                .trim() + '\n';
            if (i === matchingFiles.length - 1) {
              res.send(contents);
            }
          })
          .on('error', () => res.status(500).send('Failed to read file: ' + f.name));
      });
    } catch (err) {
      res.status(500).send('Failed to download GCS file(s). Error: ' + err);
    }
  };
}
Example #15
Source File: filestorage.ts    From foodie with MIT License 5 votes vote down vote up
storage = new Storage(config.gCloudStorage)
Example #16
Source File: googleStorage.ts    From backstage with Apache License 2.0 5 votes vote down vote up
private readonly storageClient: Storage;
Example #17
Source File: googleStorage.ts    From backstage with Apache License 2.0 5 votes vote down vote up
static fromConfig(config: Config, logger: Logger): PublisherBase {
    let bucketName = '';
    try {
      bucketName = config.getString('techdocs.publisher.googleGcs.bucketName');
    } catch (error) {
      throw new Error(
        "Since techdocs.publisher.type is set to 'googleGcs' in your app config, " +
          'techdocs.publisher.googleGcs.bucketName is required.',
      );
    }

    const bucketRootPath = normalizeExternalStorageRootPath(
      config.getOptionalString('techdocs.publisher.googleGcs.bucketRootPath') ||
        '',
    );

    // Credentials is an optional config. If missing, default GCS environment variables will be used.
    // Read more here https://cloud.google.com/docs/authentication/production
    const credentials = config.getOptionalString(
      'techdocs.publisher.googleGcs.credentials',
    );
    let credentialsJson: any = {};
    if (credentials) {
      try {
        credentialsJson = JSON.parse(credentials);
      } catch (err) {
        throw new Error(
          'Error in parsing techdocs.publisher.googleGcs.credentials config to JSON.',
        );
      }
    }

    const storageClient = new Storage({
      ...(credentials && {
        projectId: credentialsJson.project_id,
        credentials: credentialsJson,
      }),
    });

    const legacyPathCasing =
      config.getOptionalBoolean(
        'techdocs.legacyUseCaseSensitiveTripletPaths',
      ) || false;

    return new GoogleGCSPublish({
      storageClient,
      bucketName,
      legacyPathCasing,
      logger,
      bucketRootPath,
    });
  }
Example #18
Source File: GoogleGcsUrlReader.ts    From backstage with Apache License 2.0 5 votes vote down vote up
constructor(
    private readonly integration: GoogleGcsIntegrationConfig,
    private readonly storage: Storage,
  ) {}
Example #19
Source File: gcloud-storage.service.ts    From nestjs-gcloud-storage with MIT License 5 votes vote down vote up
public storage: Storage = new Storage();
Example #20
Source File: gcp.ts    From airnode with MIT License 5 votes vote down vote up
export async function stateExists(bucketName: string, cloudProvider: GcpCloudProvider) {
  logger.debug('Checking Terraform state existence in GCP');
  const { projectId } = cloudProvider;
  const storage = new Storage({ projectId });
  const bucket = storage.bucket(bucketName);

  return (await bucket.exists())[0] as boolean;
}
Example #21
Source File: GCPProvider.ts    From amplication with Apache License 2.0 5 votes vote down vote up
constructor(
    readonly cloudBuild: CloudBuildClient,
    readonly storage: Storage,
    readonly projectId: string,
    readonly bucket: string,
    readonly logger: winston.Logger = defaultLogger
  ) {}
Example #22
Source File: gcp-cache.ts    From nx-extend with MIT License 5 votes vote down vote up
public constructor(bucket: string, messages: MessageReporter) {
    this.bucket = new Storage().bucket(bucket)
    this.messages = messages
  }
Example #23
Source File: index.ts    From Dimensions with MIT License 5 votes vote down vote up
public storage: Storage;
Example #24
Source File: exportDB.ts    From companion-kit with MIT License 5 votes vote down vote up
storage = new Storage()
Example #25
Source File: thumbs.ts    From keycaplendar with MIT License 5 votes vote down vote up
storage = new Storage()