Skip to content

Commit

Permalink
Download meta struct as text from S3
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Nov 16, 2024
1 parent a8e8b93 commit 2c70e18
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
7 changes: 7 additions & 0 deletions packages/app/src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface ImageProps {
src: string;
}

export function Image({ src }: ImageProps) {
return <img src={src} />;
}
13 changes: 6 additions & 7 deletions packages/artisan/src/lib/file-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from "node:fs/promises";
import { getS3SignedUrl } from "./s3";
import { getS3SignedUrl, getTextFromS3 } from "./s3";
import type { PartialInput, Stream } from "bolt";

export async function getBinaryPath(name: string) {
Expand Down Expand Up @@ -41,11 +40,11 @@ export interface MetaStruct {
}

/**
* Will fetch meta file when meta.json is found in path.
* @param path S3 dir
* Will fetch meta file when meta.json
* @param assetId
* @returns
*/
export async function getMetaStruct(path: string): Promise<MetaStruct> {
const text = await fs.readFile(`${path}/meta.json`, "utf8");
return JSON.parse(text.toString());
export async function getMetaStruct(assetId: string): Promise<MetaStruct> {
const text = await getTextFromS3(`transcode/${assetId}/meta.json`);
return JSON.parse(text);
}
13 changes: 7 additions & 6 deletions packages/artisan/src/lib/s3.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { createReadStream } from "node:fs";
import { writeFile } from "node:fs/promises";
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { ConfiguredRetryStrategy } from "@smithy/util-retry";
import { lookup } from "mime-types";
import parseFilePath from "parse-filepath";
import { S3SyncClient } from "s3-sync-client";
import { assert } from "shared/assert";
import { env } from "../env";
import type { PutObjectCommandInput } from "@aws-sdk/client-s3";
import type { CommandInput } from "s3-sync-client";
Expand Down Expand Up @@ -119,13 +118,15 @@ export async function getS3SignedUrl(
return url;
}

export async function getFromS3(remoteFilePath: string, localDir: string) {
const filePath = parseFilePath(remoteFilePath);
export async function getTextFromS3(remoteFilePath: string) {
const command = new GetObjectCommand({
Bucket: env.S3_BUCKET,
Key: remoteFilePath,
});
const response = await client.send(command);
// @ts-expect-error Body is a Readable
await writeFile(`${localDir}/${filePath.base}`, response.Body);

const text = response.Body?.transformToString("utf-8");
assert(text, `Failed to get text from S3 "${remoteFilePath}"`);

return text;
}
8 changes: 4 additions & 4 deletions packages/artisan/src/workers/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ export const packageCallback: WorkerCallback<
async function handleStepInitial(job: Job<PackageData>, dir: WorkerDir) {
const inDir = await dir.createTempDir();

await syncFromS3(`transcode/${job.data.assetId}`, inDir);
const meta = await getMetaStruct(job.data.assetId);

job.log(`Synced folder in ${inDir}`);
job.log(`Got meta: "${JSON.stringify(meta)}"`);

const meta = await getMetaStruct(inDir);
await syncFromS3(`transcode/${job.data.assetId}`, inDir);

job.log(`Got meta: "${JSON.stringify(meta)}"`);
job.log(`Synced folder in ${inDir}`);

// If we do not specify the segmentSize, grab it from the meta file.
const segmentSize = job.data.segmentSize ?? meta.segmentSize;
Expand Down
10 changes: 2 additions & 8 deletions packages/artisan/src/workers/thumbnails.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { ffmpeg } from "../lib/ffmpeg";
import { getMetaStruct } from "../lib/file-helpers";
import { getFromS3, getS3SignedUrl, syncToS3 } from "../lib/s3";
import { getS3SignedUrl, syncToS3 } from "../lib/s3";
import type { MetaStruct } from "../lib/file-helpers";
import type { ThumbnailsData, ThumbnailsResult, WorkerCallback } from "bolt";

export const thumbnailsCallback: WorkerCallback<
ThumbnailsData,
ThumbnailsResult
> = async ({ job, dir, progressTracker }) => {
const tmpDir = await dir.createTempDir();

// TODO: Do not write meta file to local disk but stream it instead.
// Do the same for package job.
await getFromS3(`transcode/${job.data.assetId}/meta.json`, tmpDir);

const metaStruct = await getMetaStruct(tmpDir);
const metaStruct = await getMetaStruct(job.data.assetId);
const name = findStreamInputName(metaStruct);

if (!name) {
Expand Down

0 comments on commit 2c70e18

Please sign in to comment.