ts-ag 1.0.25 → 1.0.26

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.
@@ -14,4 +14,14 @@ export declare const getObject: (bucketName: string, key: string) => ResultAsync
14
14
  * Convenience function to get an object from S3 and return it as a string.
15
15
  */
16
16
  export declare function getObjectString(bucketName: string, key: string): ResultAsync<string, typeof error_s3_get>;
17
+ /**
18
+ * Checks if an object exists in an s3 bucket by retrieving the HEAD data
19
+ *
20
+ * @param {string} bucketName - The name of the S3 bucket.
21
+ * @param {string} key - The key of the object to retrieve.
22
+ * @returns {Promise<Buffer>} A promise that resolves to a boolean.
23
+ */
24
+ export declare const objectExists: (bucketName: string, key: string) => ResultAsync<boolean, {
25
+ type: "s3_get";
26
+ }>;
17
27
  //# sourceMappingURL=object.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/s3/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;;GAMG;AACH,eAAO,MAAM,SAAS;;EAiBrB,CAAC;AAEF;;GAEG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,YAAY,CAAC,CAEzG"}
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/s3/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;;GAMG;AACH,eAAO,MAAM,SAAS;;EAiBrB,CAAC;AAEF;;GAEG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,YAAY,CAAC,CAEzG;AAED;;;;;;GAMG;AACH,eAAO,MAAM,YAAY;;EAkBxB,CAAC"}
package/dist/s3/object.js CHANGED
@@ -1,4 +1,4 @@
1
- import { GetObjectCommand } from '@aws-sdk/client-s3';
1
+ import { HeadObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
2
2
  import { ResultAsync } from 'neverthrow';
3
3
  import { getS3 } from './client.js';
4
4
  import { error_s3_get } from './errors.js';
@@ -30,3 +30,28 @@ export const getObject = ResultAsync.fromThrowable(async (bucketName, key) => {
30
30
  export function getObjectString(bucketName, key) {
31
31
  return getObject(bucketName, key).map((buffer) => buffer.toString('utf-8'));
32
32
  }
33
+ /**
34
+ * Checks if an object exists in an s3 bucket by retrieving the HEAD data
35
+ *
36
+ * @param {string} bucketName - The name of the S3 bucket.
37
+ * @param {string} key - The key of the object to retrieve.
38
+ * @returns {Promise<Buffer>} A promise that resolves to a boolean.
39
+ */
40
+ export const objectExists = ResultAsync.fromThrowable(async (bucketName, key) => {
41
+ const s3 = getS3();
42
+ try {
43
+ const cmd = new HeadObjectCommand({ Bucket: bucketName, Key: key });
44
+ const res = await s3.send(cmd);
45
+ return res.$metadata.httpStatusCode === 200;
46
+ }
47
+ catch (e) {
48
+ if (e.$metadata.httpStatusCode === 404) {
49
+ return false;
50
+ }
51
+ else
52
+ throw e;
53
+ }
54
+ }, (e) => {
55
+ console.error(`Error getting object head from S3: ${e}`);
56
+ return error_s3_get;
57
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ag",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "Useful TS stuff",
5
5
  "bugs": "https://github.com/ageorgeh/ts-ag/issues",
6
6
  "author": "Alexander Hornung",
package/src/s3/object.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { GetObjectCommand } from '@aws-sdk/client-s3';
1
+ import { HeadObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
2
2
  import { ResultAsync } from 'neverthrow';
3
3
 
4
4
  import { getS3 } from './client.js';
@@ -36,3 +36,30 @@ export const getObject = ResultAsync.fromThrowable(
36
36
  export function getObjectString(bucketName: string, key: string): ResultAsync<string, typeof error_s3_get> {
37
37
  return getObject(bucketName, key).map((buffer) => buffer.toString('utf-8'));
38
38
  }
39
+
40
+ /**
41
+ * Checks if an object exists in an s3 bucket by retrieving the HEAD data
42
+ *
43
+ * @param {string} bucketName - The name of the S3 bucket.
44
+ * @param {string} key - The key of the object to retrieve.
45
+ * @returns {Promise<Buffer>} A promise that resolves to a boolean.
46
+ */
47
+ export const objectExists = ResultAsync.fromThrowable(
48
+ async (bucketName: string, key: string) => {
49
+ const s3 = getS3();
50
+
51
+ try {
52
+ const cmd = new HeadObjectCommand({ Bucket: bucketName, Key: key });
53
+ const res = await s3.send(cmd);
54
+ return res.$metadata.httpStatusCode === 200;
55
+ } catch (e) {
56
+ if ((e as any).$metadata.httpStatusCode === 404) {
57
+ return false;
58
+ } else throw e;
59
+ }
60
+ },
61
+ (e) => {
62
+ console.error(`Error getting object head from S3: ${e}`);
63
+ return error_s3_get;
64
+ }
65
+ );