@vlsdev/s3-portable 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -45
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -1,43 +1,46 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @vlsdev/s3-portable
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Portable S3 service for Node.js microservices with TypeScript, DDD layers, and SOLID-oriented design.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm i @
|
|
8
|
+
npm i @vlsdev/s3-portable
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
- Node.js 18+
|
|
14
|
+
- AWS credentials with S3 permissions (`s3:GetObject`, `s3:ListBucket`, `s3:DeleteObject`)
|
|
15
|
+
|
|
16
|
+
## Environment variables
|
|
12
17
|
|
|
13
18
|
```env
|
|
14
19
|
AWS_REGION=us-east-1
|
|
15
|
-
AWS_ACCESS_KEY_ID
|
|
16
|
-
AWS_SECRET_ACCESS_KEY
|
|
17
|
-
AWS_S3_BUCKET_NAME
|
|
20
|
+
AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
|
|
21
|
+
AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
|
|
22
|
+
AWS_S3_BUCKET_NAME=your-bucket
|
|
18
23
|
```
|
|
19
24
|
|
|
20
|
-
##
|
|
25
|
+
## Quick start (from env)
|
|
21
26
|
|
|
22
27
|
```ts
|
|
23
28
|
import "dotenv/config";
|
|
24
|
-
import { createS3ServiceFromEnv } from "@
|
|
29
|
+
import { createS3ServiceFromEnv } from "@vlsdev/s3-portable";
|
|
25
30
|
|
|
26
31
|
const s3Service = createS3ServiceFromEnv();
|
|
27
32
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
await s3Service.deleteFile("meu-projeto", "documentos", "arquivo.pdf");
|
|
31
|
-
const url = await s3Service.generateSignedUrl("meu-projeto", "documentos", "arquivo.pdf", 3600);
|
|
33
|
+
const files = await s3Service.listFiles("billing-service", "invoices");
|
|
34
|
+
console.log(files);
|
|
32
35
|
```
|
|
33
36
|
|
|
34
|
-
##
|
|
37
|
+
## Explicit config
|
|
35
38
|
|
|
36
39
|
```ts
|
|
37
|
-
import { createS3Service } from "@
|
|
40
|
+
import { createS3Service } from "@vlsdev/s3-portable";
|
|
38
41
|
|
|
39
42
|
const s3Service = createS3Service({
|
|
40
|
-
region:
|
|
43
|
+
region: process.env.AWS_REGION!,
|
|
41
44
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
42
45
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
|
|
43
46
|
bucketName: process.env.AWS_S3_BUCKET_NAME!,
|
|
@@ -46,51 +49,78 @@ const s3Service = createS3Service({
|
|
|
46
49
|
|
|
47
50
|
## API
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
- `listFiles(project, path)`
|
|
51
|
-
- `deleteFile(project, path, fileName)`
|
|
52
|
-
- `generateSignedUrl(project, path, fileName, expiresIn?)`
|
|
52
|
+
### `downloadFile(project, path, fileName)`
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
Returns the S3 object body stream.
|
|
55
55
|
|
|
56
|
-
```
|
|
57
|
-
|
|
56
|
+
```ts
|
|
57
|
+
const body = await s3Service.downloadFile("billing-service", "invoices", "invoice-1.pdf");
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
### `listFiles(project, path)`
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
Returns object keys under `project/path/`.
|
|
63
63
|
|
|
64
|
-
```
|
|
65
|
-
|
|
64
|
+
```ts
|
|
65
|
+
const keys = await s3Service.listFiles("billing-service", "invoices");
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
### `deleteFile(project, path, fileName)`
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
Deletes a file and returns a success message.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
await s3Service.deleteFile("billing-service", "invoices", "invoice-1.pdf");
|
|
72
74
|
```
|
|
73
75
|
|
|
74
|
-
|
|
76
|
+
### `generateSignedUrl(project, path, fileName, expiresIn?)`
|
|
75
77
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
+
Generates a signed GET URL (default expiration: `3600` seconds).
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const url = await s3Service.generateSignedUrl(
|
|
82
|
+
"billing-service",
|
|
83
|
+
"invoices",
|
|
84
|
+
"invoice-1.pdf",
|
|
85
|
+
900
|
|
86
|
+
);
|
|
78
87
|
```
|
|
79
88
|
|
|
80
|
-
|
|
89
|
+
## Express example
|
|
81
90
|
|
|
82
|
-
|
|
91
|
+
```ts
|
|
92
|
+
import express from "express";
|
|
93
|
+
import "dotenv/config";
|
|
94
|
+
import { createS3ServiceFromEnv } from "@vlsdev/s3-portable";
|
|
83
95
|
|
|
84
|
-
|
|
96
|
+
const app = express();
|
|
97
|
+
const s3 = createS3ServiceFromEnv();
|
|
85
98
|
|
|
86
|
-
|
|
99
|
+
app.get("/files/:project/:path/:fileName/url", async (req, res) => {
|
|
100
|
+
const { project, path, fileName } = req.params;
|
|
101
|
+
const url = await s3.generateSignedUrl(project, path, fileName, 300);
|
|
102
|
+
res.json({ url });
|
|
103
|
+
});
|
|
87
104
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- Se usar token, ele precisa ser granular com permissao de publish para `@vs/*` e com bypass 2FA habilitado.
|
|
91
|
-
- Remova credenciais antigas e relogue:
|
|
105
|
+
app.listen(3000);
|
|
106
|
+
```
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
## Error handling
|
|
109
|
+
|
|
110
|
+
This package throws:
|
|
111
|
+
- `DomainError` for invalid input/configuration
|
|
112
|
+
- AWS SDK errors for infrastructure failures
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { DomainError } from "@vlsdev/s3-portable";
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
await s3Service.generateSignedUrl("p", "docs", "file.pdf", 0);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (error instanceof DomainError) {
|
|
121
|
+
console.error("Validation error:", error.message);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
96
124
|
```
|
|
125
|
+
|
|
126
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vlsdev/s3-portable",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Portable S3 service package for microservices.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"publishConfig": {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
},
|
|
18
|
+
"access": "public",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
21
|
"files": [
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
@@ -25,8 +25,10 @@
|
|
|
25
25
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
26
26
|
"typecheck": "tsc --noEmit",
|
|
27
27
|
"prepublishOnly": "npm run build && npm run typecheck",
|
|
28
|
-
"publish:
|
|
29
|
-
"
|
|
28
|
+
"publish:public": "npm publish --access public",
|
|
29
|
+
"version:patch": "npm version patch --no-git-tag-version",
|
|
30
|
+
"version:minor": "npm version minor --no-git-tag-version",
|
|
31
|
+
"version:major": "npm version major --no-git-tag-version"
|
|
30
32
|
},
|
|
31
33
|
"dependencies": {
|
|
32
34
|
"@aws-sdk/client-s3": "^3.870.0",
|