secure-s3-storage 1.0.1 → 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.en.md +14 -6
- package/README.md +14 -6
- package/dist/index.d.ts +8 -8
- package/dist/index.js +5 -3
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -11,10 +11,10 @@ npm install secure-s3-storage
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import {
|
|
14
|
+
import { initStorage, type Storage, type BrowserFile, type UploadResult } from "secure-s3-storage";
|
|
15
15
|
import { readFile } from "node:fs/promises";
|
|
16
16
|
|
|
17
|
-
const storage =
|
|
17
|
+
const storage: Storage = initStorage({
|
|
18
18
|
bucket: "my-bucket",
|
|
19
19
|
region: "ap-northeast-2",
|
|
20
20
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
@@ -25,7 +25,15 @@ const storage = init({
|
|
|
25
25
|
},
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
const
|
|
28
|
+
const source = await readFile("./photo.png");
|
|
29
|
+
const file: BrowserFile = {
|
|
30
|
+
name: "photo.png",
|
|
31
|
+
type: "image/png",
|
|
32
|
+
arrayBuffer: async () =>
|
|
33
|
+
source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const result: UploadResult = await storage.upload(file);
|
|
29
37
|
|
|
30
38
|
const body = await readFile("./photo.png");
|
|
31
39
|
await storage.put("images", body, "image/png");
|
|
@@ -36,7 +44,7 @@ await storage.remove(result.key);
|
|
|
36
44
|
If you use temporary AWS credentials, pass `sessionToken` as well.
|
|
37
45
|
|
|
38
46
|
```ts
|
|
39
|
-
const storage =
|
|
47
|
+
const storage = initStorage({
|
|
40
48
|
bucket: "my-bucket",
|
|
41
49
|
region: "ap-northeast-2",
|
|
42
50
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
@@ -51,7 +59,7 @@ const storage = init({
|
|
|
51
59
|
|
|
52
60
|
## API
|
|
53
61
|
|
|
54
|
-
### `
|
|
62
|
+
### `initStorage(options)`
|
|
55
63
|
|
|
56
64
|
Creates a storage instance.
|
|
57
65
|
|
|
@@ -126,6 +134,6 @@ Return value from `storage.upload()` and `storage.put()`.
|
|
|
126
134
|
key: string;
|
|
127
135
|
filename: string;
|
|
128
136
|
extension: string;
|
|
129
|
-
contentType
|
|
137
|
+
contentType: string;
|
|
130
138
|
}
|
|
131
139
|
```
|
package/README.md
CHANGED
|
@@ -13,10 +13,10 @@ npm install secure-s3-storage
|
|
|
13
13
|
## Quick Start
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import {
|
|
16
|
+
import { initStorage, type Storage, type BrowserFile, type UploadResult } from "secure-s3-storage";
|
|
17
17
|
import { readFile } from "node:fs/promises";
|
|
18
18
|
|
|
19
|
-
const storage =
|
|
19
|
+
const storage: Storage = initStorage({
|
|
20
20
|
bucket: "my-bucket",
|
|
21
21
|
region: "ap-northeast-2",
|
|
22
22
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
@@ -27,7 +27,15 @@ const storage = init({
|
|
|
27
27
|
},
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const source = await readFile("./photo.png");
|
|
31
|
+
const file: BrowserFile = {
|
|
32
|
+
name: "photo.png",
|
|
33
|
+
type: "image/png",
|
|
34
|
+
arrayBuffer: async () =>
|
|
35
|
+
source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const result: UploadResult = await storage.upload(file);
|
|
31
39
|
|
|
32
40
|
const body = await readFile("./photo.png");
|
|
33
41
|
await storage.put("images", body, "image/png");
|
|
@@ -38,7 +46,7 @@ await storage.remove(result.key);
|
|
|
38
46
|
임시 AWS credential을 쓰는 경우에는 `sessionToken`도 함께 넣으면 됩니다.
|
|
39
47
|
|
|
40
48
|
```ts
|
|
41
|
-
const storage =
|
|
49
|
+
const storage = initStorage({
|
|
42
50
|
bucket: "my-bucket",
|
|
43
51
|
region: "ap-northeast-2",
|
|
44
52
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
@@ -53,7 +61,7 @@ const storage = init({
|
|
|
53
61
|
|
|
54
62
|
## API
|
|
55
63
|
|
|
56
|
-
### `
|
|
64
|
+
### `initStorage(options)`
|
|
57
65
|
|
|
58
66
|
스토리지 인스턴스를 생성합니다.
|
|
59
67
|
|
|
@@ -128,6 +136,6 @@ object key 기준으로 공개 URL을 생성합니다.
|
|
|
128
136
|
key: string;
|
|
129
137
|
filename: string;
|
|
130
138
|
extension: string;
|
|
131
|
-
contentType
|
|
139
|
+
contentType: string;
|
|
132
140
|
}
|
|
133
141
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -17,15 +17,15 @@ export type UploadResult = {
|
|
|
17
17
|
key: string;
|
|
18
18
|
filename: string;
|
|
19
19
|
extension: string;
|
|
20
|
-
contentType
|
|
20
|
+
contentType: string;
|
|
21
|
+
};
|
|
22
|
+
export type Storage = {
|
|
23
|
+
put(path: string, file: Buffer, contentType?: string): Promise<UploadResult>;
|
|
24
|
+
upload(file: BrowserFile): Promise<UploadResult>;
|
|
25
|
+
remove(key: string): Promise<void>;
|
|
26
|
+
getUrl(key: string): string;
|
|
21
27
|
};
|
|
22
|
-
export type Storage = ReturnType<typeof init>;
|
|
23
28
|
export declare class StorageValidationError extends Error {
|
|
24
29
|
constructor(message: string);
|
|
25
30
|
}
|
|
26
|
-
export declare function
|
|
27
|
-
put: (path: string, file: Buffer, contentType?: string) => Promise<UploadResult>;
|
|
28
|
-
upload: (file: BrowserFile) => Promise<UploadResult>;
|
|
29
|
-
remove: (key: string) => Promise<void>;
|
|
30
|
-
getUrl: (key: string) => string;
|
|
31
|
-
};
|
|
31
|
+
export declare function initStorage(options: StorageInitOptions): Storage;
|
package/dist/index.js
CHANGED
|
@@ -72,7 +72,7 @@ export class StorageValidationError extends Error {
|
|
|
72
72
|
this.name = "StorageValidationError";
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
export function
|
|
75
|
+
export function initStorage(options) {
|
|
76
76
|
const { bucket, categories, accessKeyId, secretAccessKey, sessionToken, ...s3Config } = options;
|
|
77
77
|
const normalizedAccessKeyId = typeof accessKeyId === "string" ? accessKeyId.trim() : undefined;
|
|
78
78
|
const normalizedSecretAccessKey = typeof secretAccessKey === "string" ? secretAccessKey.trim() : undefined;
|
|
@@ -126,7 +126,7 @@ export function init(options) {
|
|
|
126
126
|
const datePrefix = getDatePrefix();
|
|
127
127
|
const prefixWithDate = `${datePrefix}/${config.prefix}`;
|
|
128
128
|
const key = buildObjectKey(prefixWithDate, filename);
|
|
129
|
-
const finalContentType = contentType ?? detected?.mime ?? getContentTypeFromFilename(filename);
|
|
129
|
+
const finalContentType = contentType ?? detected?.mime ?? getContentTypeFromFilename(filename) ?? "application/octet-stream";
|
|
130
130
|
await client.send(new PutObjectCommand({
|
|
131
131
|
Bucket: bucket,
|
|
132
132
|
Key: key,
|
|
@@ -250,7 +250,9 @@ async function resolveUploadTarget(input) {
|
|
|
250
250
|
return {
|
|
251
251
|
extension,
|
|
252
252
|
filename: buildGeneratedFilename(extension),
|
|
253
|
-
contentType: input.sourceContentType ??
|
|
253
|
+
contentType: input.sourceContentType ??
|
|
254
|
+
getContentTypeFromFilename(`file.${extension}`) ??
|
|
255
|
+
"application/octet-stream",
|
|
254
256
|
};
|
|
255
257
|
}
|
|
256
258
|
throw new StorageValidationError(`Unable to validate file contents for .${extension}.`);
|