Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vod: specify content-length when creating presigned urls #1921

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work?

Suggested change
res.status(403);
res.status(proxyRes.status);

res.end();
proxyRes.end();
} else {
res.end();
proxyRes.end();
}
Comment on lines +1074 to +1081
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!res.headersSent) {
res.status(403);
res.end();
proxyRes.end();
} else {
res.end();
proxyRes.end();
}
if (!res.headersSent) {
res.status(403);
}
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be just ContentLength: 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
Comment on lines +923 to +924
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in api-schema since its public

catalystPipelineStrategy:
$ref: "#/components/schemas/task/properties/params/properties/upload/properties/catalystPipelineStrategy"
ipfs-file-info:
Expand Down