File

src/cloud/bezos.service.ts

Index

Properties
Methods

Constructor

constructor(configService: ConfigService)
Parameters :
Name Type Optional
configService ConfigService No

Methods

Public Async removeFromCache
removeFromCache(object: string[])
Parameters :
Name Type Optional
object string[] No
Returns : Promise<void>
Public Async removeFromCdn
removeFromCdn(file: string, path: string)
Parameters :
Name Type Optional
file string No
path string No
Returns : Promise<void>
Public Async removeUploads
removeUploads(files: string | string[])
Parameters :
Name Type Optional
files string | string[] No
Returns : Promise<void>
Public Async upload
upload(file: Buffer, name: string, mime: string)
Parameters :
Name Type Optional
file Buffer No
name string No
mime string No
Returns : Promise<void>
Public Async uploadToCdn
uploadToCdn(file: Buffer, name: string, mime: string, path: string, ACL: S3.ObjectCannedACL)
Parameters :
Name Type Optional Default value
file Buffer No
name string No
mime string No
path string No
ACL S3.ObjectCannedACL No 'public-read'
Returns : Promise<void>

Properties

Private Readonly cloudFront
Type : CloudFront
Private Readonly logger
Default value : new Logger(BezosService.name)
Private Readonly s3
Type : S3
import { Injectable } from '@nestjs/common';
import S3 from 'aws-sdk/clients/s3';
import CloudFront from 'aws-sdk/clients/cloudfront';
import { ConfigService } from '@nestjs/config';
import { Logger } from '@nestjs/common';
import { Stream } from 'stream';

@Injectable()
export class BezosService {
  private readonly s3: S3;
  private readonly cloudFront: CloudFront;
  private readonly logger = new Logger(BezosService.name);
  constructor(private readonly configService: ConfigService) {
    this.s3 = new S3({
      endpoint: configService.get('AWS_EPOINT'),
      accessKeyId: configService.get('AWS_AKEY'),
      secretAccessKey: configService.get('AWS_SEC'),
    });

    this.cloudFront = new CloudFront({
      accessKeyId: configService.get('AWS_AKEY'),
      secretAccessKey: configService.get('AWS_SEC'),
      region: 'eu-central-1',
    });
  }

  public async upload(file: Buffer, name: string, mime: string): Promise<void> {
    return new Promise<void>((resolve) => {
      const start = Date.now();
      this.logger.debug(
        `Inputted file buffer? ${file instanceof Buffer} stream? ${
          file instanceof Stream
        }`,
      );
      const params: S3.PutObjectRequest = {
        Body: file,
        Bucket: 'fileglass-user-files',
        Key: name,
        ContentType: mime,
        ContentEncoding: 'buffer',
        ACL: 'public-read',
      };
      this.s3.putObject(params, (err, data) => {
        if (err) {
          throw err;
        }
        this.logger.log(`File ${name} uploaded with mimetype: ${mime}`);
        resolve();
        this.logger.debug(`Request to aws took ${Date.now() - start} ms`);
      });
    });
  }

  public async removeUploads(files: string | string[]): Promise<void> {
    files = typeof files === 'string' ? [files] : files;
    this.logger.log('Mapping deleted files', files);

    const params: S3.DeleteObjectsRequest = {
      Bucket: 'fileglass-user-files',
      Delete: {
        Objects: files.map((f) => {
          return { Key: f };
        }),
      },
    };
    const resp = this.s3.deleteObjects(params, (err, resp) => {
      if (err) {
        throw err;
      }
      this.logger.log('RESP', resp);
    });
    await this.removeFromCache(files);
  }

  public async uploadToCdn(
    file: Buffer,
    name: string,
    mime: string,
    path: string,
    ACL: S3.ObjectCannedACL = 'public-read',
  ): Promise<void> {
    return new Promise<void>((resolve) => {
      const params: S3.PutObjectRequest = {
        Body: file,
        Bucket: 'iodine-content-cdn',
        Key: `${path}/${name}`,
        ContentType: mime,
        ContentEncoding: 'buffer',
        ACL,
      };
      this.s3.putObject(params, (err) => {
        if (err) {
          throw err;
        }
        resolve();
      });
    });
  }

  public async removeFromCdn(file: string, path: string): Promise<void> {
    const params: S3.DeleteObjectRequest = {
      Bucket: 'iodine-content-cdn',
      Key: `${path}/${file}`,
    };
    this.s3.deleteObject(params);
    this.s3.deleteObjectTagging(params);
    return;
  }

  public async removeFromCache(object: string[]): Promise<void> {
    object = object.map((o) => `/${o}`);
    const params: CloudFront.CreateInvalidationRequest = {
      DistributionId: this.configService.get<string>('AWS_DISTROID'),
      InvalidationBatch: {
        CallerReference: (Date.now() * process.pid).toString(),
        Paths: {
          Quantity: object.length,
          Items: object,
        },
      },
    };
    this.cloudFront.createInvalidation(params, (err, resp) => {
      if (err) {
        throw err;
      }
    });
  }
}

results matching ""

    No results matching ""