ag-common 0.0.737 → 0.0.739
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/dist/api/helpers/s3.d.ts +5 -2
- package/dist/api/helpers/s3.js +21 -14
- package/package.json +1 -1
package/dist/api/helpers/s3.d.ts
CHANGED
|
@@ -7,13 +7,16 @@ export type StorageConfig = {
|
|
|
7
7
|
region?: string;
|
|
8
8
|
endpoint?: string;
|
|
9
9
|
accountId?: string;
|
|
10
|
+
accessKeyId?: string;
|
|
11
|
+
secretAccessKey?: string;
|
|
10
12
|
} | {
|
|
11
13
|
provider: 'r2';
|
|
12
14
|
accountId: string;
|
|
15
|
+
accessKeyId?: string;
|
|
16
|
+
secretAccessKey?: string;
|
|
13
17
|
};
|
|
14
18
|
export declare const createStorageClient: (config: StorageConfig) => S3Client;
|
|
15
|
-
export declare const setS3: (config: StorageConfig) =>
|
|
16
|
-
export declare const s3: S3Client;
|
|
19
|
+
export declare const setS3: (config: StorageConfig) => void;
|
|
17
20
|
export declare const getS3Object: ({ fileurl: { Bucket, Key }, }: {
|
|
18
21
|
fileurl: {
|
|
19
22
|
Bucket: string;
|
package/dist/api/helpers/s3.js
CHANGED
|
@@ -22,7 +22,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
|
|
|
22
22
|
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.
|
|
25
|
+
exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.setS3 = exports.createStorageClient = void 0;
|
|
26
26
|
exports.getS3Objects = getS3Objects;
|
|
27
27
|
exports.listFiles = listFiles;
|
|
28
28
|
exports.getPresignedPostURL = getPresignedPostURL;
|
|
@@ -59,10 +59,14 @@ const createStorageClient = (config) => {
|
|
|
59
59
|
if (!accountId) {
|
|
60
60
|
throw new Error('Account ID is required for R2. Set CLOUDFLARE_ACCOUNT_ID env var or provide accountId in config');
|
|
61
61
|
}
|
|
62
|
-
client = new client_s3_1.S3Client({
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
client = new client_s3_1.S3Client(Object.assign({ endpoint: `https://${accountId}.r2.cloudflarestorage.com`, forcePathStyle: true }, (config.accessKeyId && config.secretAccessKey
|
|
63
|
+
? {
|
|
64
|
+
credentials: {
|
|
65
|
+
accessKeyId: config.accessKeyId,
|
|
66
|
+
secretAccessKey: config.secretAccessKey,
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
: {})));
|
|
66
70
|
}
|
|
67
71
|
else {
|
|
68
72
|
client = new client_s3_1.S3Client({
|
|
@@ -73,15 +77,18 @@ const createStorageClient = (config) => {
|
|
|
73
77
|
return client;
|
|
74
78
|
};
|
|
75
79
|
exports.createStorageClient = createStorageClient;
|
|
76
|
-
|
|
80
|
+
let s3;
|
|
81
|
+
const setS3 = (config) => {
|
|
82
|
+
s3 = (0, exports.createStorageClient)(config);
|
|
83
|
+
};
|
|
77
84
|
exports.setS3 = setS3;
|
|
78
|
-
|
|
85
|
+
(0, exports.setS3)({
|
|
79
86
|
provider: 's3',
|
|
80
87
|
region: 'ap-southeast-2',
|
|
81
88
|
});
|
|
82
89
|
const getS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fileurl: { Bucket, Key }, }) {
|
|
83
90
|
try {
|
|
84
|
-
const r = yield
|
|
91
|
+
const r = yield s3.send(new client_s3_1.GetObjectCommand({ Bucket, Key }));
|
|
85
92
|
if (!r.Body) {
|
|
86
93
|
throw new Error('no body returned');
|
|
87
94
|
}
|
|
@@ -120,7 +127,7 @@ function getS3Objects(_a) {
|
|
|
120
127
|
}
|
|
121
128
|
const putS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Body, Bucket, Key, ContentType, CacheControl = 'public, max-age=300', }) {
|
|
122
129
|
try {
|
|
123
|
-
yield
|
|
130
|
+
yield s3.send(new client_s3_1.PutObjectCommand({ Body, Bucket, Key, ContentType, CacheControl }));
|
|
124
131
|
return {};
|
|
125
132
|
}
|
|
126
133
|
catch (e) {
|
|
@@ -130,7 +137,7 @@ const putS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Body, B
|
|
|
130
137
|
exports.putS3Object = putS3Object;
|
|
131
138
|
const deleteFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket, Key, }) {
|
|
132
139
|
try {
|
|
133
|
-
yield
|
|
140
|
+
yield s3.send(new client_s3_1.DeleteObjectCommand({ Bucket, Key }));
|
|
134
141
|
return {};
|
|
135
142
|
}
|
|
136
143
|
catch (e) {
|
|
@@ -146,7 +153,7 @@ const deleteFiles = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket,
|
|
|
146
153
|
while (toDelete.length > 0) {
|
|
147
154
|
const { part, rest } = (0, array_1.take)(toDelete, 900);
|
|
148
155
|
toDelete = rest;
|
|
149
|
-
const res = yield
|
|
156
|
+
const res = yield s3.send(new client_s3_1.DeleteObjectsCommand({
|
|
150
157
|
Bucket,
|
|
151
158
|
Delete: { Objects: part },
|
|
152
159
|
}));
|
|
@@ -166,7 +173,7 @@ exports.deleteFiles = deleteFiles;
|
|
|
166
173
|
const copyFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket, fromKey, toKey, deleteSource = false, }) {
|
|
167
174
|
try {
|
|
168
175
|
(0, log_1.debug)(`copying s3 file from ${Bucket}- ${fromKey} to ${toKey}`);
|
|
169
|
-
yield
|
|
176
|
+
yield s3.send(new client_s3_1.CopyObjectCommand({
|
|
170
177
|
//incl bucket
|
|
171
178
|
CopySource: Bucket + '/' + fromKey,
|
|
172
179
|
//dest
|
|
@@ -193,7 +200,7 @@ function listFiles(bucketName, opt) {
|
|
|
193
200
|
const ret = [];
|
|
194
201
|
let response;
|
|
195
202
|
do {
|
|
196
|
-
response = yield
|
|
203
|
+
response = yield s3.send(new client_s3_1.ListObjectsV2Command({
|
|
197
204
|
Bucket: bucketName,
|
|
198
205
|
ContinuationToken: response === null || response === void 0 ? void 0 : response.NextContinuationToken,
|
|
199
206
|
Prefix: opt === null || opt === void 0 ? void 0 : opt.prefix,
|
|
@@ -226,7 +233,7 @@ function listFiles(bucketName, opt) {
|
|
|
226
233
|
function getPresignedPostURL(_a) {
|
|
227
234
|
return __awaiter(this, arguments, void 0, function* ({ bucket, key, maxMb = 5, }) {
|
|
228
235
|
try {
|
|
229
|
-
const ps = yield (0, s3_presigned_post_1.createPresignedPost)(
|
|
236
|
+
const ps = yield (0, s3_presigned_post_1.createPresignedPost)(s3, {
|
|
230
237
|
Bucket: bucket,
|
|
231
238
|
Key: key,
|
|
232
239
|
Expires: 600,
|
package/package.json
CHANGED