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

feat: support for providing s3 endpoint url #8

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Options:
--shaka-executable [shakaExecutable] Path to shaka-packager executable, defaults to 'packager'. Can also be set with environment variable SHAKA_PACKAGER_EXECUTABLE.
--no-implicit-audio [noImplicitAudio] Do not include audio unless audio input specified
-d, --destination-folder <dest> Destination folder URL (supported protocols: s3, local file). Defaults to CWD.
--endpoint-url [s3EndpointUrl] S3 endpoint URL
--dash-only Package only DASH format
--hls-only Package only HLS format
--segment-single-file Use byte range addressing and a single segment file per stream
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ cli
'-d, --destination-folder <dest>',
'Destination folder URL (supported protocols: s3, local file). Defaults to CWD.'
)
.option('--endpoint-url [s3EndpointUrl]', 'S3 endpoint URL')
.option('--dash-only', 'Package only DASH format')
.option('--hls-only', 'Package only HLS format')
.option(
Expand Down Expand Up @@ -94,6 +95,7 @@ cli
);
await doPackage({
dest: options.destinationFolder || '.',
s3EndpointUrl: options.s3EndpointUrl,
source: options.sourceFolder,
inputs: inputOptions,
stagingDir: options.stagingDir,
Expand Down
31 changes: 23 additions & 8 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export interface PackageOptions {
packageFormatOptions?: PackageFormatOptions;
shakaExecutable?: string;
serviceAccessToken?: string;
s3EndpointUrl?: string;
}

function validateOptios(opts: PackageOptions) {
function validateOptions(opts: PackageOptions) {
if (
opts?.packageFormatOptions?.hlsOnly &&
opts?.packageFormatOptions?.dashOnly
Expand All @@ -51,14 +52,14 @@ function validateOptios(opts: PackageOptions) {
}

export async function doPackage(opts: PackageOptions) {
validateOptios(opts);
validateOptions(opts);
const stagingDir = await prepare(opts.stagingDir);
await createPackage({ ...opts, stagingDir });
if (toUrl(opts.dest).protocol === 's3:') {
// We don't want to upload source files to S3
await removeDownloadedFiles(opts.inputs, stagingDir);
}
await uploadPackage(toUrl(opts.dest), stagingDir);
await uploadPackage(toUrl(opts.dest), stagingDir, opts.s3EndpointUrl);
await cleanup(stagingDir);
}

Expand All @@ -82,7 +83,8 @@ export async function download(
input: Input,
source?: URL,
stagingDir?: string,
serviceAccessToken?: string
serviceAccessToken?: string,
endpointUrl?: string
): Promise<string> {
if (!source) {
return input.filename;
Expand All @@ -99,6 +101,7 @@ export async function download(
const localFilename = join(stagingDir, input.filename);
const { status, stderr } = spawnSync('aws', [
's3',
endpointUrl ? `--endpoint-url=${endpointUrl}` : '',
birme marked this conversation as resolved.
Show resolved Hide resolved
'cp',
sourceFile.toString(),
localFilename
Expand Down Expand Up @@ -161,7 +164,11 @@ async function moveFile(src: string, dest: string) {
});
}

export async function uploadPackage(dest: URL, stagingDir: string) {
export async function uploadPackage(
dest: URL,
stagingDir: string,
s3EndpointUrl?: string
) {
if (!dest.protocol || dest.protocol === 'file:') {
await mkdir(dest.pathname, { recursive: true });
const files = await readdir(stagingDir);
Expand All @@ -176,6 +183,7 @@ export async function uploadPackage(dest: URL, stagingDir: string) {
console.log(`Uploading package to ${dest.toString()}`);
const { status, stderr, error } = spawnSync('aws', [
's3',
s3EndpointUrl ? `--endpoint-url=${s3EndpointUrl}` : '',
'cp',
'--recursive',
stagingDir,
Expand All @@ -197,16 +205,23 @@ export async function uploadPackage(dest: URL, stagingDir: string) {
}

export async function createPackage(opts: PackageOptions) {
const { inputs, source, stagingDir, noImplicitAudio, serviceAccessToken } =
opts;
const {
inputs,
source,
stagingDir,
noImplicitAudio,
serviceAccessToken,
s3EndpointUrl
} = opts;
const sourceUrl = toUrlOrUndefined(source);
const downloadedInputs: Input[] = await Promise.all(
inputs.map(async (input) => {
const filename = await download(
input,
sourceUrl,
stagingDir,
serviceAccessToken
serviceAccessToken,
s3EndpointUrl
);
return {
...input,
Expand Down
Loading