Skip to content

Commit

Permalink
fix: s3 command line args without endpoint url
Browse files Browse the repository at this point in the history
  • Loading branch information
birme committed Nov 14, 2024
1 parent 59be845 commit 523ec26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path, { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, rmSync, unlinkSync } from 'node:fs';
import { readdir, mkdir } from 'node:fs/promises';
import { toUrl, toUrlOrUndefined } from './util';
import { createS3cmdArgs, toUrl, toUrlOrUndefined } from './util';
import mv from 'mv';

const DEFAULT_STAGING_DIR = '/tmp/data';
Expand Down Expand Up @@ -99,13 +99,11 @@ export async function download(
if (source.protocol === 's3:') {
const sourceFile = new URL(join(source.pathname, input.filename), source);
const localFilename = join(stagingDir, input.filename);
const { status, stderr } = spawnSync('aws', [
's3',
endpointUrl ? `--endpoint-url=${endpointUrl}` : '',
'cp',
sourceFile.toString(),
localFilename
]);
const args = createS3cmdArgs(
['cp', sourceFile.toString(), localFilename],
endpointUrl
);
const { status, stderr } = spawnSync('aws', args);
if (status !== 0) {
if (stderr) {
console.log(stderr.toString());
Expand Down Expand Up @@ -181,14 +179,11 @@ export async function uploadPackage(
}
if (dest.protocol === 's3:') {
console.log(`Uploading package to ${dest.toString()}`);
const { status, stderr, error } = spawnSync('aws', [
's3',
s3EndpointUrl ? `--endpoint-url=${s3EndpointUrl}` : '',
'cp',
'--recursive',
stagingDir,
dest.toString()
]);
const args = createS3cmdArgs(
['cp', '--recursive', stagingDir, dest.toString()],
s3EndpointUrl
);
const { status, stderr, error } = spawnSync('aws', args);
if (status !== 0) {
if (error) {
console.error(`Upload failed: ${error.message}`);
Expand Down
13 changes: 13 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createS3cmdArgs } from './util';

describe('cmd args helper', () => {
test('can construct s3cmd args without s3 endpoint url', () => {
const args = createS3cmdArgs(['ls']);
expect(args).toEqual(['s3', 'ls']);
});

test('can construct s3cmd args s3 endpoint url', () => {
const args = createS3cmdArgs(['ls'], 'https://my.api.url.com');
expect(args).toEqual(['s3', '--endpoint-url=https://my.api.url.com', 'ls']);
});
});
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ export function toUrl(url: string) {
? new URL(url)
: new URL(`file://${path.resolve(url)}`);
}

export function createS3cmdArgs(
cmdArgs: string[],
s3EndpointUrl?: string
): string[] {
const args = ['s3'];
if (s3EndpointUrl) {
args.push(`--endpoint-url=${s3EndpointUrl}`);
}
return args.concat(cmdArgs);
}

0 comments on commit 523ec26

Please sign in to comment.