Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Add getMetadata functino to gcs (#158)
Browse files Browse the repository at this point in the history
* Add getMetadata functino to gcs

Co-authored-by: Ebba Lindström <[email protected]>

---------

Co-authored-by: Ebba Lindström <[email protected]>
  • Loading branch information
MrGuzior and ebbalindstrom authored Jun 27, 2024
1 parent 8ed694d commit 4b5fdf1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/utils/gcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -148,4 +157,5 @@ export {
metadata,
toPath,
pipeline,
getFileMetadata,
};
10 changes: 10 additions & 0 deletions test/unit/utils/gcs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
list,
metadata,
toPath,
getFileMetadata,
} from "../../../lib/utils/gcs.js";

const { Readable, Writable, promises: { pipeline } } = stream;
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 4b5fdf1

Please sign in to comment.