diff --git a/lib/utils/gcs.js b/lib/utils/gcs.js index caf51d6..00c6b8a 100644 --- a/lib/utils/gcs.js +++ b/lib/utils/gcs.js @@ -136,6 +136,15 @@ function toPath( )}${subDirectories}/${fileName}`; } +async function getFileMetadata(gcsPath) { + const { Bucket, Key } = parseUri(gcsPath); + const storage = new Storage(getCredentials()); + + const [ md ] = await storage.bucket(Bucket).file(Key).getMetadata(); + + return md; +} + export { createReadStream, createWriteStream, @@ -148,4 +157,5 @@ export { metadata, toPath, pipeline, + getFileMetadata, }; diff --git a/test/unit/utils/gcs-test.js b/test/unit/utils/gcs-test.js index 69f5cf7..8a13767 100644 --- a/test/unit/utils/gcs-test.js +++ b/test/unit/utils/gcs-test.js @@ -14,6 +14,7 @@ import { list, metadata, toPath, + getFileMetadata, } from "../../../lib/utils/gcs.js"; const { Readable, Writable, promises: { pipeline } } = stream; @@ -91,6 +92,15 @@ describe("create write stream to GCS file", () => { }); }); +describe("get files metadata", () => { + before(fakeGcs.reset); + it("should return files metadata", async () => { + fakeGcs.mockFile(filePath, { content: "asd" }); + const metaData = await getFileMetadata(filePath); + metaData.should.eql({ name: "some-file.json", size: 3, contentEncoding: "utf-8", contentType: "application/json" }); + }); +}); + describe("check if GCS file exists", () => { beforeEach(fakeGcs.reset); it("should return that a non-existant file isn't there", async () => {