@occupop/lib-s3-storage
Version:
Tiny S3 storage helper (AWS & LocalStack) with injectable bucket, signed URLs and public URLs.
59 lines (48 loc) • 1.39 kB
Markdown
# @seu-escopo/s3-storage
Helper minimalista para S3 (AWS e LocalStack).
### Recursos
- URL pré-assinada **PUT**
- URL pré-assinada **GET**
- URL pública crua
- Bucket injetável (por instância ou chamada)
- Suporte a LocalStack e S3 Accelerate
- Checagem de CRC32 no upload
### Instalação
```bash
npm i @seu-escopo/s3-storage @aws-sdk/client-s3 @aws-sdk/s3-request-presigner @smithy/node-http-handler crc-32
```
### Uso
```ts
import { createS3Storage } from '@seu-escopo/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({
bucket: 'message',
path: 'uuid/file.pdf',
contentType: 'application/pdf',
fileName: 'file.pdf'
})
console.log(putUrl.url)
// 2) Upload direto
const uploaded = await storage.putObject({
bucket: 'message',
path: 'uuid/file.pdf',
contentType: 'application/pdf',
bodyBase64: Buffer.from('conteúdo').toString('base64')
})
console.log(uploaded.url)
// 3) URL pública
const url = await storage.getPublicUrl({
bucket: 'message',
path: 'uuid/file.pdf'
})
console.log(url)
```