Skip to content

Commit

Permalink
vod: specify content-length when creating presigned urls
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelecerati committed Oct 14, 2023
1 parent ac6f7bd commit 1984ac9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
25 changes: 20 additions & 5 deletions packages/api/src/controllers/asset.ts
Expand Up @@ -508,12 +508,17 @@ async function genUploadUrl(
playbackId: string,
objectStoreId: string,
jwtSecret: string,
aud: string
aud: string,
contentLength?: number
) {
const uploadedObjectKey = `directUpload/${playbackId}`;
const os = await getActiveObjectStore(objectStoreId);

const presignedUrl = await getS3PresignedUrl(os, uploadedObjectKey);
const presignedUrl = await getS3PresignedUrl(
os,
uploadedObjectKey,
contentLength
);
const uploadToken = jwt.sign({ playbackId, presignedUrl, aud }, jwtSecret, {
algorithm: "HS256",
});
Expand Down Expand Up @@ -877,7 +882,8 @@ app.post(
playbackId,
vodObjectStoreId,
jwtSecret,
jwtAudience
jwtAudience,
req.body.contentLength
);

const ingests = await req.getIngest();
Expand Down Expand Up @@ -1049,7 +1055,6 @@ app.put("/upload/direct", async (req, res) => {
jwtSecret,
jwtAudience
);

// ensure upload exists and is pending
await getPendingAssetAndTask(playbackId);

Expand All @@ -1064,7 +1069,17 @@ app.put("/upload/direct", async (req, res) => {
);
}
});

proxy.on("error", function (err, req, proxyRes) {
console.error("Proxy error:", err);
if (!res.headersSent) {
res.status(403);
res.end();
proxyRes.end();
} else {
res.end();
proxyRes.end();
}
});
proxy.web(req, res, {
target: uploadUrl,
changeOrigin: true,
Expand Down
15 changes: 12 additions & 3 deletions packages/api/src/controllers/helpers.ts
Expand Up @@ -223,13 +223,22 @@ export async function getObjectStoreS3Config(
};
}

export async function getS3PresignedUrl(os: ObjectStore, objectKey: string) {
export async function getS3PresignedUrl(
os: ObjectStore,
objectKey: string,
contentLength?: number
) {
const config = await getObjectStoreS3Config(os);
const s3 = new S3Client(config);
const putCommand = new PutObjectCommand({
let putCommandConfig = {
Bucket: config.bucket,
Key: objectKey,
});
ContentLength: null,
};
if (contentLength) {
putCommandConfig.ContentLength = contentLength;
}
const putCommand = new PutObjectCommand(putCommandConfig);
const expiresIn = 12 * 60 * 60; // 12h in seconds
return getSignedUrl(s3, putCommand, { expiresIn });
}
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/schema/db-schema.yaml
Expand Up @@ -920,6 +920,8 @@ components:
description: Object store ID where the asset is stored
writeOnly: true
example: 09F8B46C-61A0-4254-9875-F71F4C605BC7
contentLength:
type: number
catalystPipelineStrategy:
$ref: "#/components/schemas/task/properties/params/properties/upload/properties/catalystPipelineStrategy"
ipfs-file-info:
Expand Down

0 comments on commit 1984ac9

Please sign in to comment.