@rytass/storages-adapter-gcs 0.2.4 → 0.2.5
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 +473 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,476 @@
|
|
|
1
|
-
# Rytass Utils -
|
|
1
|
+
# Rytass Utils - Google Cloud Storage Adapter
|
|
2
|
+
|
|
3
|
+
A powerful Google Cloud Storage adapter for the Rytass file storage system. Provides seamless integration with Google Cloud Storage buckets with support for signed URLs, stream processing, and automatic file type detection.
|
|
2
4
|
|
|
3
5
|
## Features
|
|
4
6
|
|
|
5
|
-
- [x] Google Cloud Storage bucket
|
|
7
|
+
- [x] Google Cloud Storage bucket integration
|
|
8
|
+
- [x] Signed URL generation for secure file access
|
|
9
|
+
- [x] Buffer and Stream file operations
|
|
10
|
+
- [x] Automatic content type detection
|
|
11
|
+
- [x] Batch file operations
|
|
12
|
+
- [x] File existence checking
|
|
13
|
+
- [x] GZIP compression support
|
|
14
|
+
- [x] Service account authentication
|
|
15
|
+
- [x] TypeScript support
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @rytass/storages-adapter-gcs @google-cloud/storage
|
|
21
|
+
# or
|
|
22
|
+
yarn add @rytass/storages-adapter-gcs @google-cloud/storage
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Basic Usage
|
|
26
|
+
|
|
27
|
+
### Service Configuration
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { StorageGCSService } from '@rytass/storages-adapter-gcs';
|
|
31
|
+
|
|
32
|
+
const storage = new StorageGCSService({
|
|
33
|
+
bucket: 'your-gcs-bucket-name',
|
|
34
|
+
projectId: 'your-gcp-project-id',
|
|
35
|
+
credentials: {
|
|
36
|
+
client_email: 'your-service-account@project.iam.gserviceaccount.com',
|
|
37
|
+
private_key: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Upload Files
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { readFileSync, createReadStream } from 'fs';
|
|
46
|
+
|
|
47
|
+
// Upload buffer
|
|
48
|
+
const imageBuffer = readFileSync('photo.jpg');
|
|
49
|
+
const result = await storage.write(imageBuffer, {
|
|
50
|
+
filename: 'uploads/photo.jpg',
|
|
51
|
+
contentType: 'image/jpeg'
|
|
52
|
+
});
|
|
53
|
+
console.log('Uploaded:', result.key);
|
|
54
|
+
|
|
55
|
+
// Upload stream
|
|
56
|
+
const fileStream = createReadStream('document.pdf');
|
|
57
|
+
const streamResult = await storage.write(fileStream, {
|
|
58
|
+
filename: 'documents/document.pdf',
|
|
59
|
+
contentType: 'application/pdf'
|
|
60
|
+
});
|
|
61
|
+
console.log('Uploaded:', streamResult.key);
|
|
62
|
+
|
|
63
|
+
// Auto-generated filename (based on file content)
|
|
64
|
+
const autoResult = await storage.write(imageBuffer);
|
|
65
|
+
console.log('Auto-generated filename:', autoResult.key);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Download Files
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// Download as buffer
|
|
72
|
+
const fileBuffer = await storage.read('uploads/photo.jpg', { format: 'buffer' });
|
|
73
|
+
console.log('Downloaded buffer:', fileBuffer.length, 'bytes');
|
|
74
|
+
|
|
75
|
+
// Download as stream
|
|
76
|
+
const fileStream = await storage.read('uploads/photo.jpg');
|
|
77
|
+
fileStream.pipe(process.stdout);
|
|
78
|
+
|
|
79
|
+
// Stream to file
|
|
80
|
+
import { createWriteStream } from 'fs';
|
|
81
|
+
const downloadStream = await storage.read('documents/document.pdf');
|
|
82
|
+
const writeStream = createWriteStream('downloaded-document.pdf');
|
|
83
|
+
downloadStream.pipe(writeStream);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Generate Signed URLs
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Default expiration (24 hours)
|
|
90
|
+
const url = await storage.url('uploads/photo.jpg');
|
|
91
|
+
console.log('Signed URL:', url);
|
|
92
|
+
|
|
93
|
+
// Custom expiration (1 hour from now)
|
|
94
|
+
const customUrl = await storage.url(
|
|
95
|
+
'uploads/photo.jpg',
|
|
96
|
+
Date.now() + 1000 * 60 * 60
|
|
97
|
+
);
|
|
98
|
+
console.log('1-hour URL:', customUrl);
|
|
99
|
+
|
|
100
|
+
// Use in HTML
|
|
101
|
+
const publicUrl = await storage.url('images/avatar.png');
|
|
102
|
+
// <img src="${publicUrl}" alt="User Avatar" />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### File Management
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
// Check if file exists
|
|
109
|
+
const exists = await storage.isExists('uploads/photo.jpg');
|
|
110
|
+
if (exists) {
|
|
111
|
+
console.log('File exists');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Remove file
|
|
115
|
+
await storage.remove('uploads/old-file.jpg');
|
|
116
|
+
console.log('File removed');
|
|
117
|
+
|
|
118
|
+
// Batch upload
|
|
119
|
+
const files = [
|
|
120
|
+
readFileSync('file1.jpg'),
|
|
121
|
+
readFileSync('file2.png'),
|
|
122
|
+
createReadStream('file3.pdf')
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
const batchResults = await storage.batchWrite(files);
|
|
126
|
+
batchResults.forEach(result => {
|
|
127
|
+
console.log('Uploaded:', result.key);
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Advanced Usage
|
|
132
|
+
|
|
133
|
+
### Environment-based Configuration
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// .env file
|
|
137
|
+
// GCS_BUCKET=your-bucket-name
|
|
138
|
+
// GCS_PROJECT_ID=your-project-id
|
|
139
|
+
// GCS_CLIENT_EMAIL=your-service-account@project.iam.gserviceaccount.com
|
|
140
|
+
// GCS_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
|
|
141
|
+
|
|
142
|
+
import { StorageGCSService } from '@rytass/storages-adapter-gcs';
|
|
143
|
+
|
|
144
|
+
const storage = new StorageGCSService({
|
|
145
|
+
bucket: process.env.GCS_BUCKET!,
|
|
146
|
+
projectId: process.env.GCS_PROJECT_ID!,
|
|
147
|
+
credentials: {
|
|
148
|
+
client_email: process.env.GCS_CLIENT_EMAIL!,
|
|
149
|
+
private_key: process.env.GCS_PRIVATE_KEY!.replace(/\\n/g, '\n')
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Service Account Key File
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { readFileSync } from 'fs';
|
|
158
|
+
|
|
159
|
+
// Load service account key from JSON file
|
|
160
|
+
const serviceAccount = JSON.parse(
|
|
161
|
+
readFileSync('path/to/service-account-key.json', 'utf8')
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
const storage = new StorageGCSService({
|
|
165
|
+
bucket: 'your-bucket-name',
|
|
166
|
+
projectId: serviceAccount.project_id,
|
|
167
|
+
credentials: {
|
|
168
|
+
client_email: serviceAccount.client_email,
|
|
169
|
+
private_key: serviceAccount.private_key
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### File Upload with Metadata
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// Upload with custom metadata
|
|
178
|
+
const result = await storage.write(fileBuffer, {
|
|
179
|
+
filename: 'uploads/document.pdf',
|
|
180
|
+
contentType: 'application/pdf'
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// The service automatically sets:
|
|
184
|
+
// - Content-Type based on file extension or provided contentType
|
|
185
|
+
// - GZIP compression for eligible files
|
|
186
|
+
// - Proper metadata for file identification
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Stream Processing for Large Files
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { createReadStream, createWriteStream } from 'fs';
|
|
193
|
+
import { pipeline } from 'stream/promises';
|
|
194
|
+
|
|
195
|
+
async function processLargeFile(inputPath: string, outputKey: string) {
|
|
196
|
+
// Upload large file as stream
|
|
197
|
+
const inputStream = createReadStream(inputPath);
|
|
198
|
+
const uploadResult = await storage.write(inputStream, {
|
|
199
|
+
filename: outputKey
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
console.log('Large file uploaded:', uploadResult.key);
|
|
203
|
+
|
|
204
|
+
// Download large file as stream
|
|
205
|
+
const downloadStream = await storage.read(outputKey);
|
|
206
|
+
const outputStream = createWriteStream('downloaded-large-file');
|
|
207
|
+
|
|
208
|
+
await pipeline(downloadStream, outputStream);
|
|
209
|
+
console.log('Large file downloaded');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
processLargeFile('large-video.mp4', 'videos/large-video.mp4');
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Error Handling
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import { StorageError, ErrorCode } from '@rytass/storages';
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
const result = await storage.read('non-existent-file.jpg');
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (error instanceof StorageError && error.code === ErrorCode.READ_FILE_ERROR) {
|
|
224
|
+
console.log('File not found');
|
|
225
|
+
} else {
|
|
226
|
+
console.error('Unexpected error:', error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Safe file operations
|
|
231
|
+
async function safeFileOperation(key: string) {
|
|
232
|
+
try {
|
|
233
|
+
// Check if file exists first
|
|
234
|
+
if (await storage.isExists(key)) {
|
|
235
|
+
const content = await storage.read(key, { format: 'buffer' });
|
|
236
|
+
return content;
|
|
237
|
+
} else {
|
|
238
|
+
console.log('File does not exist:', key);
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error('Error reading file:', error);
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Integration Examples
|
|
249
|
+
|
|
250
|
+
### Express.js File Upload
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
import express from 'express';
|
|
254
|
+
import multer from 'multer';
|
|
255
|
+
import { StorageGCSService } from '@rytass/storages-adapter-gcs';
|
|
256
|
+
|
|
257
|
+
const app = express();
|
|
258
|
+
const upload = multer({ storage: multer.memoryStorage() });
|
|
259
|
+
const storage = new StorageGCSService({
|
|
260
|
+
bucket: 'your-bucket',
|
|
261
|
+
projectId: 'your-project',
|
|
262
|
+
credentials: {
|
|
263
|
+
client_email: process.env.GCS_CLIENT_EMAIL!,
|
|
264
|
+
private_key: process.env.GCS_PRIVATE_KEY!
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
app.post('/upload', upload.single('file'), async (req, res) => {
|
|
269
|
+
try {
|
|
270
|
+
if (!req.file) {
|
|
271
|
+
return res.status(400).json({ error: 'No file uploaded' });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const result = await storage.write(req.file.buffer, {
|
|
275
|
+
filename: `uploads/${Date.now()}-${req.file.originalname}`,
|
|
276
|
+
contentType: req.file.mimetype
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const publicUrl = await storage.url(result.key);
|
|
280
|
+
|
|
281
|
+
res.json({
|
|
282
|
+
success: true,
|
|
283
|
+
key: result.key,
|
|
284
|
+
url: publicUrl
|
|
285
|
+
});
|
|
286
|
+
} catch (error) {
|
|
287
|
+
res.status(500).json({ error: 'Upload failed' });
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### NestJS Integration
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
import { Injectable } from '@nestjs/common';
|
|
296
|
+
import { StorageGCSService } from '@rytass/storages-adapter-gcs';
|
|
297
|
+
|
|
298
|
+
@Injectable()
|
|
299
|
+
export class FileService {
|
|
300
|
+
private storage: StorageGCSService;
|
|
301
|
+
|
|
302
|
+
constructor() {
|
|
303
|
+
this.storage = new StorageGCSService({
|
|
304
|
+
bucket: process.env.GCS_BUCKET!,
|
|
305
|
+
projectId: process.env.GCS_PROJECT_ID!,
|
|
306
|
+
credentials: {
|
|
307
|
+
client_email: process.env.GCS_CLIENT_EMAIL!,
|
|
308
|
+
private_key: process.env.GCS_PRIVATE_KEY!.replace(/\\n/g, '\n')
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async uploadFile(file: Buffer, filename: string): Promise<string> {
|
|
314
|
+
const result = await this.storage.write(file, { filename });
|
|
315
|
+
return this.storage.url(result.key);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async getFile(key: string): Promise<Buffer> {
|
|
319
|
+
return this.storage.read(key, { format: 'buffer' });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async deleteFile(key: string): Promise<void> {
|
|
323
|
+
await this.storage.remove(key);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async fileExists(key: string): Promise<boolean> {
|
|
327
|
+
return this.storage.isExists(key);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Image Processing Pipeline
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
import { StorageGCSService } from '@rytass/storages-adapter-gcs';
|
|
336
|
+
import { ConverterManager } from '@rytass/file-converter';
|
|
337
|
+
import { ImageResizer } from '@rytass/file-converter-adapter-image-resizer';
|
|
338
|
+
import { ImageTranscoder } from '@rytass/file-converter-adapter-image-transcoder';
|
|
339
|
+
|
|
340
|
+
class ImageProcessor {
|
|
341
|
+
constructor(
|
|
342
|
+
private storage: StorageGCSService,
|
|
343
|
+
private converter: ConverterManager
|
|
344
|
+
) {}
|
|
345
|
+
|
|
346
|
+
async processAndUpload(
|
|
347
|
+
imageBuffer: Buffer,
|
|
348
|
+
sizes: { width: number; height: number; suffix: string }[]
|
|
349
|
+
): Promise<{ [key: string]: string }> {
|
|
350
|
+
const results: { [key: string]: string } = {};
|
|
351
|
+
|
|
352
|
+
for (const size of sizes) {
|
|
353
|
+
// Create processor for this size
|
|
354
|
+
const processor = new ConverterManager([
|
|
355
|
+
new ImageResizer({
|
|
356
|
+
maxWidth: size.width,
|
|
357
|
+
maxHeight: size.height,
|
|
358
|
+
keepAspectRatio: true
|
|
359
|
+
}),
|
|
360
|
+
new ImageTranscoder({
|
|
361
|
+
format: 'webp',
|
|
362
|
+
quality: 85
|
|
363
|
+
})
|
|
364
|
+
]);
|
|
365
|
+
|
|
366
|
+
// Process image
|
|
367
|
+
const processedImage = await processor.convert<Buffer>(imageBuffer);
|
|
368
|
+
|
|
369
|
+
// Upload to GCS
|
|
370
|
+
const uploadResult = await this.storage.write(processedImage, {
|
|
371
|
+
filename: `images/processed-${size.suffix}.webp`,
|
|
372
|
+
contentType: 'image/webp'
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// Generate public URL
|
|
376
|
+
results[size.suffix] = await this.storage.url(uploadResult.key);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return results;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Usage
|
|
384
|
+
const processor = new ImageProcessor(storage, converter);
|
|
385
|
+
const urls = await processor.processAndUpload(originalImage, [
|
|
386
|
+
{ width: 150, height: 150, suffix: 'thumbnail' },
|
|
387
|
+
{ width: 800, height: 600, suffix: 'medium' },
|
|
388
|
+
{ width: 1920, height: 1080, suffix: 'large' }
|
|
389
|
+
]);
|
|
390
|
+
|
|
391
|
+
console.log('Generated URLs:', urls);
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## Configuration Options
|
|
395
|
+
|
|
396
|
+
### GCSOptions
|
|
397
|
+
|
|
398
|
+
| Option | Type | Required | Description |
|
|
399
|
+
|--------|------|----------|-------------|
|
|
400
|
+
| `bucket` | `string` | Yes | Google Cloud Storage bucket name |
|
|
401
|
+
| `projectId` | `string` | Yes | Google Cloud Project ID |
|
|
402
|
+
| `credentials` | `object` | Yes | Service account credentials |
|
|
403
|
+
| `credentials.client_email` | `string` | Yes | Service account email |
|
|
404
|
+
| `credentials.private_key` | `string` | Yes | Service account private key |
|
|
405
|
+
|
|
406
|
+
### WriteFileOptions
|
|
407
|
+
|
|
408
|
+
| Option | Type | Default | Description |
|
|
409
|
+
|--------|------|---------|-------------|
|
|
410
|
+
| `filename` | `string` | auto-generated | Custom filename for the uploaded file |
|
|
411
|
+
| `contentType` | `string` | auto-detected | MIME type of the file |
|
|
412
|
+
|
|
413
|
+
### ReadBufferFileOptions
|
|
414
|
+
|
|
415
|
+
| Option | Type | Default | Description |
|
|
416
|
+
|--------|------|---------|-------------|
|
|
417
|
+
| `format` | `'buffer'` | - | Return file as Buffer |
|
|
418
|
+
|
|
419
|
+
### ReadStreamFileOptions
|
|
420
|
+
|
|
421
|
+
| Option | Type | Default | Description |
|
|
422
|
+
|--------|------|---------|-------------|
|
|
423
|
+
| `format` | `'stream'` | - | Return file as Readable stream |
|
|
424
|
+
|
|
425
|
+
## Best Practices
|
|
426
|
+
|
|
427
|
+
### Security
|
|
428
|
+
- Store service account credentials securely using environment variables
|
|
429
|
+
- Use IAM roles with minimal required permissions
|
|
430
|
+
- Regularly rotate service account keys
|
|
431
|
+
- Enable audit logging for storage access
|
|
432
|
+
|
|
433
|
+
### Performance
|
|
434
|
+
- Use streams for large files to reduce memory usage
|
|
435
|
+
- Leverage GZIP compression for text-based files
|
|
436
|
+
- Implement proper error handling and retry logic
|
|
437
|
+
- Use batch operations for multiple file uploads
|
|
438
|
+
|
|
439
|
+
### Cost Optimization
|
|
440
|
+
- Choose appropriate storage classes for your use case
|
|
441
|
+
- Set up lifecycle policies for automatic data management
|
|
442
|
+
- Monitor storage usage and optimize file sizes
|
|
443
|
+
- Use signed URLs to reduce bandwidth costs
|
|
444
|
+
|
|
445
|
+
### File Organization
|
|
446
|
+
- Use consistent naming conventions
|
|
447
|
+
- Organize files in logical folder structures
|
|
448
|
+
- Implement proper versioning strategies
|
|
449
|
+
- Consider using metadata for file categorization
|
|
450
|
+
|
|
451
|
+
## Error Handling
|
|
452
|
+
|
|
453
|
+
The adapter throws `StorageError` instances for various error conditions:
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
import { StorageError, ErrorCode } from '@rytass/storages';
|
|
457
|
+
|
|
458
|
+
// Common error scenarios
|
|
459
|
+
try {
|
|
460
|
+
await storage.read('non-existent-file.jpg');
|
|
461
|
+
} catch (error) {
|
|
462
|
+
if (error instanceof StorageError) {
|
|
463
|
+
switch (error.code) {
|
|
464
|
+
case ErrorCode.READ_FILE_ERROR:
|
|
465
|
+
console.log('File not found or inaccessible');
|
|
466
|
+
break;
|
|
467
|
+
default:
|
|
468
|
+
console.log('Storage operation failed:', error.message);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
## License
|
|
475
|
+
|
|
476
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rytass/storages-adapter-gcs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Google Cloud Storage Adapter for @rytass/storages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gcp",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@google-cloud/storage": "^7.14.0",
|
|
25
|
-
"@rytass/storages": "^0.2.
|
|
25
|
+
"@rytass/storages": "^0.2.2",
|
|
26
26
|
"uuid": "^11.0.3"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|