@8ms/helpers 1.23.0 → 1.24.0
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/aws/s3/isFileExists.d.ts
CHANGED
|
@@ -5,5 +5,5 @@ type IsFileExists = {
|
|
|
5
5
|
/**
|
|
6
6
|
* Check to see if a file exists but don't get the contents.
|
|
7
7
|
*/
|
|
8
|
-
declare const isFileExists: (
|
|
8
|
+
declare const isFileExists: (props: IsFileExists) => Promise<boolean>;
|
|
9
9
|
export default isFileExists;
|
package/aws/s3/isFileExists.js
CHANGED
|
@@ -7,14 +7,14 @@ const isResponse200_1 = __importDefault(require("../isResponse200"));
|
|
|
7
7
|
/**
|
|
8
8
|
* Check to see if a file exists but don't get the contents.
|
|
9
9
|
*/
|
|
10
|
-
const isFileExists = async (
|
|
10
|
+
const isFileExists = async (props) => {
|
|
11
11
|
let response = false;
|
|
12
12
|
try {
|
|
13
|
-
const { HeadObjectCommand } = require(
|
|
13
|
+
const { HeadObjectCommand } = require("@aws-sdk/client-s3");
|
|
14
14
|
// Fetch from S3
|
|
15
15
|
const apiResponse = await global.awsS3Client.send(new HeadObjectCommand({
|
|
16
|
-
Bucket: bucket,
|
|
17
|
-
Key: key,
|
|
16
|
+
Bucket: props.bucket,
|
|
17
|
+
Key: props.key,
|
|
18
18
|
}));
|
|
19
19
|
// If the status is 200 then it does exist
|
|
20
20
|
response = (0, isResponse200_1.default)({ apiResponse });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getClient_1 = __importDefault(require("./getClient"));
|
|
7
|
+
/**
|
|
8
|
+
* Check to see if a file exists but don't get the contents.
|
|
9
|
+
*/
|
|
10
|
+
const isFileExists = async (props) => {
|
|
11
|
+
const { bucket, key, projectId, } = props;
|
|
12
|
+
const googleFile = await (0, getClient_1.default)({ projectId })
|
|
13
|
+
.bucket(bucket)
|
|
14
|
+
.file(key)
|
|
15
|
+
.exists();
|
|
16
|
+
const response = googleFile?.[0];
|
|
17
|
+
return response;
|
|
18
|
+
};
|
|
19
|
+
exports.default = isFileExists;
|
|
@@ -2,11 +2,10 @@ type WriteFile = {
|
|
|
2
2
|
bucket: string;
|
|
3
3
|
data: any;
|
|
4
4
|
key: string;
|
|
5
|
-
projectId?: string;
|
|
6
5
|
[options: string]: any;
|
|
7
6
|
};
|
|
8
7
|
/**
|
|
9
8
|
* Write a file to Google Cloud Storage.
|
|
10
9
|
*/
|
|
11
|
-
export declare const writeFile: (
|
|
10
|
+
export declare const writeFile: (props: WriteFile) => Promise<any>;
|
|
12
11
|
export default writeFile;
|
|
@@ -8,8 +8,9 @@ const getClient_1 = __importDefault(require("./getClient"));
|
|
|
8
8
|
/**
|
|
9
9
|
* Write a file to Google Cloud Storage.
|
|
10
10
|
*/
|
|
11
|
-
const writeFile = async (
|
|
12
|
-
const
|
|
11
|
+
const writeFile = async (props) => {
|
|
12
|
+
const { bucket, contentType, data, key, projectId, ...options } = props;
|
|
13
|
+
const stream = require("stream");
|
|
13
14
|
const dataStream = new stream.PassThrough();
|
|
14
15
|
const googleFile = (0, getClient_1.default)({ projectId })
|
|
15
16
|
.bucket(bucket)
|
|
@@ -18,16 +19,16 @@ const writeFile = async ({ bucket, contentType, data, key, projectId, ...options
|
|
|
18
19
|
dataStream.push(null);
|
|
19
20
|
return await new Promise((resolve, reject) => {
|
|
20
21
|
dataStream.pipe(googleFile.createWriteStream({
|
|
21
|
-
contentType:
|
|
22
|
+
contentType: "auto",
|
|
22
23
|
resumable: false,
|
|
23
24
|
validation: false,
|
|
24
25
|
metadata: {},
|
|
25
26
|
...options,
|
|
26
27
|
}))
|
|
27
|
-
.on(
|
|
28
|
+
.on("error", (error) => {
|
|
28
29
|
reject(error);
|
|
29
30
|
})
|
|
30
|
-
.on(
|
|
31
|
+
.on("finish", () => {
|
|
31
32
|
resolve(true);
|
|
32
33
|
});
|
|
33
34
|
});
|