triangle-utils 1.4.2 → 1.4.4

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,8 +1,10 @@
1
+ import { Readable } from "stream";
1
2
  export declare class UtilsS3 {
2
3
  private readonly s3;
3
4
  constructor(region: string);
4
5
  file_exists(s3_filename: string): Promise<boolean | undefined>;
5
6
  get_file(s3_filename: string): Promise<string | undefined>;
7
+ get_file_stream(s3_filename: string): Promise<Readable | undefined>;
6
8
  create_file(s3_filename: string, content: string | Buffer, options?: {
7
9
  content_type?: string;
8
10
  }): Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput | undefined>;
@@ -1,5 +1,6 @@
1
1
  import { S3, NoSuchKey, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
2
2
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
3
+ import { Readable } from "stream";
3
4
  function parse_s3_filename(s3_filename) {
4
5
  if (s3_filename.substring(0, 5) !== "s3://") {
5
6
  return undefined;
@@ -48,6 +49,29 @@ export class UtilsS3 {
48
49
  throw error;
49
50
  }
50
51
  }
52
+ async get_file_stream(s3_filename) {
53
+ const s3_key = parse_s3_filename(s3_filename);
54
+ if (s3_key === undefined) {
55
+ return undefined;
56
+ }
57
+ try {
58
+ const response = await this.s3.getObject(s3_key);
59
+ const body = response.Body;
60
+ if (body === undefined) {
61
+ return undefined;
62
+ }
63
+ if (body instanceof Readable) {
64
+ return body;
65
+ }
66
+ return undefined;
67
+ }
68
+ catch (error) {
69
+ if (error instanceof NoSuchKey) {
70
+ return undefined;
71
+ }
72
+ throw error;
73
+ }
74
+ }
51
75
  async create_file(s3_filename, content, options = {}) {
52
76
  const content_type = options.content_type !== undefined ? options.content_type : undefined;
53
77
  const exists = await this.file_exists(s3_filename);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "main": "dist/src/index.js",
5
5
  "directories": {
6
6
  "test": "vitest"
package/src/UtilsS3.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { S3, NoSuchKey, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
2
2
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
3
+ import { Readable } from "stream"
3
4
 
4
5
  interface S3Key {
5
6
  Bucket : string,
@@ -58,6 +59,29 @@ export class UtilsS3 {
58
59
  }
59
60
  }
60
61
 
62
+ async get_file_stream(s3_filename : string) : Promise<Readable | undefined> {
63
+ const s3_key = parse_s3_filename(s3_filename)
64
+ if (s3_key === undefined) {
65
+ return undefined
66
+ }
67
+ try {
68
+ const response = await this.s3.getObject(s3_key)
69
+ const body = response.Body
70
+ if (body === undefined) {
71
+ return undefined
72
+ }
73
+ if (body instanceof Readable) {
74
+ return body
75
+ }
76
+ return undefined
77
+ } catch (error) {
78
+ if (error instanceof NoSuchKey) {
79
+ return undefined
80
+ }
81
+ throw error
82
+ }
83
+ }
84
+
61
85
  async create_file(s3_filename : string, content : string | Buffer, options : {
62
86
  content_type? : string
63
87
  } = {}) {