@quatrain/storage-s3 0.3.0 → 1.1.7

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.
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
3
4
  import { AbstractStorageAdapter, FileType, FileResponseLinkType, StorageParameters, DownloadFileMetaType } from '@quatrain/storage';
4
5
  import { Readable, Stream } from 'stream';
5
6
  import { S3Client } from '@aws-sdk/client-s3';
package/package.json CHANGED
@@ -1,36 +1,37 @@
1
1
  {
2
- "name": "@quatrain/storage-s3",
3
- "version": "0.3.0",
4
- "description": "Storage adapter for S3 compatible services",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "files": [
8
- "lib/",
9
- "README.md"
10
- ],
11
- "author": "Quatrain Développement SAS <developers@quatrain.com>",
12
- "license": "MIT",
13
- "devDependencies": {
14
- "@tsconfig/recommended": "^1.0.1",
15
- "@types/jest": "^27.0.3",
16
- "jest": "^27.4.7",
17
- "jest-node-exports-resolver": "^1.1.6",
18
- "trace-unhandled": "^2.0.1",
19
- "ts-jest": "^27.1.2",
20
- "ts-node": "^10.4.0",
21
- "typescript": "^5.1.5"
22
- },
23
- "dependencies": {
24
- "@aws-sdk/client-s3": "^3.337.0",
25
- "@aws-sdk/s3-request-presigner": "^3.342.0",
26
- "@quatrain/storage": "^0.1.1",
27
- "fs-extra": "^11.2.0"
28
- },
29
- "scripts": {
30
- "test": "tsc && jest --verbose",
31
- "build": "tsc --watch",
32
- "patch": "yarn version --patch --no-git-tag-version",
33
- "push": "yarn publish --access public",
34
- "prepublish": "tsc"
35
- }
36
- }
2
+ "name": "@quatrain/storage-s3",
3
+ "version": "1.1.7",
4
+ "description": "Storage adapter for S3 compatible services",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": [
8
+ "lib/",
9
+ "README.md"
10
+ ],
11
+ "author": "Quatrain Développement SAS <developers@quatrain.com>",
12
+ "license": "MIT",
13
+ "devDependencies": {
14
+ "@tsconfig/recommended": "^1.0.1",
15
+ "@types/jest": "^27.0.3",
16
+ "@types/node": "^22.10.1",
17
+ "jest": "^27.4.7",
18
+ "jest-node-exports-resolver": "^1.1.6",
19
+ "trace-unhandled": "^2.0.1",
20
+ "ts-jest": "^27.1.2",
21
+ "ts-node": "^10.4.0",
22
+ "typescript": "^5.1.5"
23
+ },
24
+ "dependencies": {
25
+ "@aws-sdk/client-s3": "^3.337.0",
26
+ "@aws-sdk/s3-request-presigner": "^3.342.0",
27
+ "@quatrain/storage": "^1.1.7",
28
+ "fs-extra": "^11.2.0"
29
+ },
30
+ "scripts": {
31
+ "test": "tsc && jest --verbose",
32
+ "build": "tsc",
33
+ "wbuild": "tsc --watch",
34
+ "bump-to": "yarn version",
35
+ "publish": "yarn build && yarn npm publish --access public"
36
+ }
37
+ }
@@ -1,23 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { AbstractStorageAdapter, FileType, FileResponseLinkType, StorageParameters, DownloadFileMetaType } from '@quatrain/storage';
4
- import { Readable, Stream } from 'stream';
5
- import { S3Client } from '@aws-sdk/client-s3';
6
- export declare class S3StorageAdapter extends AbstractStorageAdapter {
7
- protected _client: S3Client;
8
- constructor(params: StorageParameters);
9
- getDriver(): S3Client;
10
- streamToBuffer(stream: Stream): Promise<Buffer>;
11
- create(file: FileType, stream: Readable): Promise<FileType>;
12
- copy(file: FileType, destFile: FileType): Promise<void>;
13
- move(file: FileType, destFile: FileType): Promise<void>;
14
- getUrl(file: FileType, expiresIn?: number): Promise<{
15
- url: string;
16
- expiresIn: number;
17
- }>;
18
- delete(file: FileType): Promise<boolean>;
19
- getReadable(file: FileType): Promise<Readable>;
20
- stream(file: FileType, res: any): Promise<any>;
21
- download(file: FileType, meta: DownloadFileMetaType): Promise<string>;
22
- getUploadUrl(file: FileType, expiresIn?: number): Promise<FileResponseLinkType>;
23
- }
@@ -1,168 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.S3StorageAdapter = void 0;
13
- const storage_1 = require("@quatrain/storage");
14
- const stream_1 = require("stream");
15
- const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
16
- const fs_1 = require("fs");
17
- const client_s3_1 = require("@aws-sdk/client-s3");
18
- class S3StorageAdapter extends storage_1.AbstractStorageAdapter {
19
- constructor(params) {
20
- super(params);
21
- const config = {
22
- forcePathStyle: true,
23
- region: this._params.config.region,
24
- endpoint: this._params.config.endpoint,
25
- credentials: {
26
- accessKeyId: this._params.config.accesskey,
27
- secretAccessKey: this._params.config.secret,
28
- },
29
- };
30
- this._client = new client_s3_1.S3Client(config);
31
- storage_1.Storage.log(`AWS Storage Adapter initialized in ${this._params.config.region}`);
32
- }
33
- getDriver() {
34
- return this._client;
35
- }
36
- streamToBuffer(stream) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- return new Promise((resolve, reject) => {
39
- const _buf = [];
40
- stream.on('data', (chunk) => _buf.push(chunk));
41
- stream.on('end', () => resolve(Buffer.concat(_buf)));
42
- stream.on('error', (err) => reject(err));
43
- });
44
- });
45
- }
46
- create(file, stream) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const input = {
49
- Bucket: file.bucket || this._params.config.bucket,
50
- Key: file.ref,
51
- Body: yield this.streamToBuffer(stream),
52
- ContentType: file.contentType,
53
- };
54
- storage_1.Storage.log(`Uploading ${file.ref} to ${file.bucket}`);
55
- const command = new client_s3_1.PutObjectCommand(input);
56
- yield this._client.send(command);
57
- return file;
58
- });
59
- }
60
- copy(file, destFile) {
61
- return __awaiter(this, void 0, void 0, function* () {
62
- storage_1.Storage.log(`Copying file ${file.ref} to ${destFile.ref}`);
63
- const command = new client_s3_1.CopyObjectCommand({
64
- CopySource: encodeURI(`${file.bucket}/${file.ref}`),
65
- Bucket: file.bucket,
66
- Key: encodeURI(destFile.ref),
67
- });
68
- try {
69
- const response = yield this._client.send(command);
70
- console.log(response);
71
- }
72
- catch (err) {
73
- console.error(err);
74
- }
75
- });
76
- }
77
- move(file, destFile) {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- try {
80
- yield this.copy(file, destFile);
81
- yield this.delete(file);
82
- }
83
- catch (err) {
84
- console.log(err);
85
- }
86
- });
87
- }
88
- getUrl(file, expiresIn = 3600) {
89
- return __awaiter(this, void 0, void 0, function* () {
90
- const command = new client_s3_1.GetObjectCommand({
91
- Bucket: file.bucket,
92
- Key: encodeURI(file.ref),
93
- });
94
- const url = yield (0, s3_request_presigner_1.getSignedUrl)(this._client, command, { expiresIn });
95
- return { url, expiresIn };
96
- });
97
- }
98
- delete(file) {
99
- var _a;
100
- return __awaiter(this, void 0, void 0, function* () {
101
- storage_1.Storage.log(`Deleting file ${file.ref}`);
102
- const command = new client_s3_1.DeleteObjectCommand({
103
- Bucket: file.bucket,
104
- Key: file.ref,
105
- });
106
- yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.send(command));
107
- storage_1.Storage.log('Object successfully deleted.');
108
- return true;
109
- });
110
- }
111
- getReadable(file) {
112
- var _a, _b;
113
- return __awaiter(this, void 0, void 0, function* () {
114
- storage_1.Storage.log('GET Readable :', file.ref);
115
- const command = new client_s3_1.GetObjectCommand({
116
- Bucket: file.bucket || this._params.config.bucket,
117
- Key: file.ref,
118
- });
119
- const item = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.send(command));
120
- const ByteArray = yield ((_b = item === null || item === void 0 ? void 0 : item.Body) === null || _b === void 0 ? void 0 : _b.transformToByteArray());
121
- const buffer = Buffer.from(ByteArray, 'base64');
122
- const readable = new stream_1.Readable();
123
- readable.push(buffer);
124
- readable.push(null);
125
- return readable;
126
- });
127
- }
128
- stream(file, res) {
129
- var _a;
130
- return __awaiter(this, void 0, void 0, function* () {
131
- const command = new client_s3_1.GetObjectCommand({
132
- Bucket: file.bucket,
133
- Key: file.ref,
134
- });
135
- const item = yield this._client.send(command);
136
- const ByteArray = yield ((_a = item.Body) === null || _a === void 0 ? void 0 : _a.transformToByteArray());
137
- const buffer = Buffer.from(ByteArray, 'base64');
138
- const readable = new stream_1.Readable();
139
- readable.push(buffer);
140
- readable.push(null);
141
- return readable.pipe(res);
142
- });
143
- }
144
- download(file, meta) {
145
- return __awaiter(this, void 0, void 0, function* () {
146
- const stream = yield this.getReadable(file);
147
- yield new Promise((res) => stream.pipe((0, fs_1.createWriteStream)(meta.path)).on('close', res));
148
- return meta.path;
149
- });
150
- }
151
- getUploadUrl(file, expiresIn = 3600) {
152
- return __awaiter(this, void 0, void 0, function* () {
153
- const input = {
154
- Bucket: file.bucket || this._params.config.bucket,
155
- Key: file.ref,
156
- };
157
- const command = new client_s3_1.PutObjectCommand(input);
158
- const url = yield (0, s3_request_presigner_1.getSignedUrl)(this._client, command, { expiresIn });
159
- return {
160
- href: url,
161
- type: 'PUT',
162
- accept: file.contentType || 'application/octet-stream',
163
- expiresIn,
164
- };
165
- });
166
- }
167
- }
168
- exports.S3StorageAdapter = S3StorageAdapter;
package/lib/s3.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import { FileType, FileResponseUrl } from '@quatrain/core';
2
- import { S3Client } from '@aws-sdk/client-s3';
3
- export declare const s3Bucket: any;
4
- export declare const s3Client: S3Client;
5
- /**
6
- * Move file to AWS
7
- * @param gcsFrom source file metadata
8
- * @param awsTo destination file metadata
9
- * @returns destination bucket name
10
- */
11
- export declare const moveFile: (gcsFrom: FileType, awsTo: FileType) => Promise<string>;
12
- /**
13
- * Returns a temporary public url matching the provided metadata
14
- * @param file
15
- * @returns
16
- */
17
- export declare const getPublicUrl: (file: FileType, expiresIn?: number) => Promise<FileResponseUrl>;
18
- /**
19
- * Delete the file matching the provided metadata
20
- * @param file
21
- * @returns
22
- */
23
- export declare const deleteFile: (file: FileType) => Promise<import("@aws-sdk/client-s3").DeleteObjectCommandOutput>;
24
- export declare const streamFile: (file: FileType, res: any) => Promise<any>;
package/lib/s3.js DELETED
@@ -1,93 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.streamFile = exports.deleteFile = exports.getPublicUrl = exports.moveFile = exports.s3Client = exports.s3Bucket = void 0;
13
- const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
14
- const client_s3_1 = require("@aws-sdk/client-s3");
15
- const storage_1 = require("@google-cloud/storage");
16
- const stream_1 = require("stream");
17
- const functions = require('firebase-functions');
18
- const { accesskey, secret, s3: { bucket, region }, } = functions.config().aws || {};
19
- exports.s3Bucket = bucket;
20
- const config = {
21
- region,
22
- credentials: {
23
- accessKeyId: accesskey,
24
- secretAccessKey: secret,
25
- },
26
- };
27
- exports.s3Client = new client_s3_1.S3Client(config);
28
- /**
29
- * Move file to AWS
30
- * @param gcsFrom source file metadata
31
- * @param awsTo destination file metadata
32
- * @returns destination bucket name
33
- */
34
- const moveFile = (gcsFrom, awsTo) => __awaiter(void 0, void 0, void 0, function* () {
35
- const storage = new storage_1.Storage();
36
- const fileRef = storage.bucket(gcsFrom.bucket).file(gcsFrom.ref);
37
- const fileStream = fileRef.createReadStream();
38
- if (!gcsFrom.size || gcsFrom.contentType) {
39
- const metadata = (yield fileRef.getMetadata())[0];
40
- gcsFrom.size = metadata.size;
41
- gcsFrom.contentType = metadata.contentType;
42
- }
43
- const input = {
44
- Bucket: awsTo.bucket,
45
- Key: awsTo.ref,
46
- Body: fileStream,
47
- ContentType: gcsFrom.contentType,
48
- ContentLength: gcsFrom.size,
49
- };
50
- console.log(`Moving ${gcsFrom.ref} from GCP to ${awsTo.ref} at AWS`);
51
- const command = new client_s3_1.PutObjectCommand(input);
52
- yield exports.s3Client.send(command);
53
- return awsTo.bucket;
54
- });
55
- exports.moveFile = moveFile;
56
- /**
57
- * Returns a temporary public url matching the provided metadata
58
- * @param file
59
- * @returns
60
- */
61
- const getPublicUrl = (file, expiresIn = 3600) => __awaiter(void 0, void 0, void 0, function* () {
62
- const command = new client_s3_1.GetObjectCommand({ Bucket: file.bucket, Key: file.ref });
63
- const url = yield (0, s3_request_presigner_1.getSignedUrl)(exports.s3Client, command, { expiresIn });
64
- return { url, expiresIn };
65
- });
66
- exports.getPublicUrl = getPublicUrl;
67
- /**
68
- * Delete the file matching the provided metadata
69
- * @param file
70
- * @returns
71
- */
72
- const deleteFile = (file) => __awaiter(void 0, void 0, void 0, function* () {
73
- const command = new client_s3_1.DeleteObjectCommand({
74
- Bucket: file.bucket,
75
- Key: file.ref,
76
- });
77
- const data = yield exports.s3Client.send(command);
78
- console.log('Success. Object deleted.', data);
79
- return data;
80
- });
81
- exports.deleteFile = deleteFile;
82
- const streamFile = (file, res) => __awaiter(void 0, void 0, void 0, function* () {
83
- var _a;
84
- const command = new client_s3_1.GetObjectCommand({ Bucket: file.bucket, Key: file.ref });
85
- const item = yield exports.s3Client.send(command);
86
- const ByteArray = yield ((_a = item.Body) === null || _a === void 0 ? void 0 : _a.transformToByteArray());
87
- const buffer = Buffer.from(ByteArray, 'base64');
88
- const readable = new stream_1.Readable();
89
- readable.push(buffer);
90
- readable.push(null);
91
- return readable.pipe(res);
92
- });
93
- exports.streamFile = streamFile;