npm.io
1.1.1 • Published 2d ago

@occupop/lib-s3-storage

Licence
MIT
Version
1.1.1
Deps
6
Size
36 kB
Vulns
0
Weekly
0

@occupop/lib-s3-storage

Helper minimalista para S3 (AWS e LocalStack).

Recursos

  • URL pré-assinada PUT
  • URL pré-assinada GET
  • Upload direto com putObject (checksum CRC32)
  • Leitura de objeto em base64 com getObjectBase64
  • Bucket injetável (por instância ou chamada)
  • Suporte a LocalStack e S3 Accelerate

Instalação

npm i @occupop/lib-s3-storage @aws-sdk/client-s3 @aws-sdk/s3-request-presigner crc-32
Uso
import { createS3Storage } from '@occupop/lib-s3-storage'

const storage = createS3Storage({
  region: process.env.AWS_REGION!,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
  },
  endpoint: process.env.AWS_S3_ENDPOINT, // LocalStack: "http://localstack:4566"
  defaultBucket: process.env.AWS_S3_BUCKET || 'message',
})

// 1) Signed PUT
const putUrl = await storage.getSignedPutUrl({
  path: 'uuid/file.pdf',
  contentType: 'application/pdf',
})
console.log(putUrl.url)

// 2) Upload direto
const uploaded = await storage.putObject({
  path: 'uuid/file.pdf',
  contentType: 'application/pdf',
  bodyBase64: Buffer.from('conteúdo').toString('base64'),
})
console.log(uploaded.url)

// 3) Signed GET
const getUrl = await storage.getSignedGetUrl({
  path: uploaded.key,
  fileName: 'file.pdf',
  download: true,
})
console.log(getUrl.url)

// 4) Ler objeto e retornar base64
const base64 = await storage.getObjectBase64({
  key: uploaded.key,
})
console.log(base64)
API
createS3Storage(config: S3StorageConfig): S3Storage

S3Storage expoe os metodos abaixo:

  • getSignedPutUrl(input: SignedPutInput): Promise<SignedUrlResult>

    • input.path (obrigatorio)
    • input.bucket?
    • input.contentType?
    • input.expiresInSeconds?
  • putObject(input: PutObjectInput): Promise<PutObjectResult>

    • input.path (obrigatorio)
    • input.contentType (obrigatorio)
    • input.bodyBase64 (obrigatorio)
    • input.bucket?
    • input.cacheControl?
  • getSignedGetUrl(input: SignedGetInput): Promise<SignedUrlResult>

    • input.path (obrigatorio)
    • input.bucket?
    • input.expiresInSeconds?
    • input.fileName?
    • input.download?
    • input.contentType?
  • getObjectBase64(input: GetObjectBase64Input): Promise<string>

    • input.key (obrigatorio)
    • input.bucket?

Retornos:

  • SignedUrlResult: { url, bucket, key, expiresIn }
  • PutObjectResult: { url, bucket, key }