Skip to content

Commit b3a4e69

Browse files
committed
add uploading to s3 buckets
1 parent cbcf351 commit b3a4e69

File tree

5 files changed

+262
-29
lines changed

5 files changed

+262
-29
lines changed

package-lock.json

Lines changed: 103 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
],
4141
"dependencies": {
4242
"await-lock": "^2.0.1",
43+
"aws-sdk": "^2.619.0",
4344
"axios": "^0.19.2",
4445
"chalk": "^3.0.0",
4546
"env-paths": "^2.2.0",
@@ -62,6 +63,7 @@
6263
"url": "[email protected]:ScriptSmith/instamancer.git"
6364
},
6465
"devDependencies": {
66+
"@types/aws-sdk": "^2.7.0",
6567
"@types/concat-stream": "^1.6.0",
6668
"@types/express": "^4.17.2",
6769
"@types/jest": "^24.9.1",

src/cli.ts

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
import * as aws from "aws-sdk";
34
import * as fs from "fs";
45
import * as readline from "readline";
56
import * as winston from "winston";
@@ -10,8 +11,9 @@ import * as plugins from "../plugins";
1011
import {createApi, IOptions} from "./api/api";
1112
import {TFullApiPost, TPost} from "./api/types";
1213
import {GetPool} from "./getpool/getPool";
14+
import * as depotUpload from "./http/depot";
1315
import {download, toCSV, toJSON} from "./http/download";
14-
import * as upload from "./http/upload";
16+
import * as s3Upload from "./http/s3";
1517

1618
const getLogger = (args) => {
1719
const transports = [];
@@ -217,13 +219,6 @@ function buildParser(args, callback) {
217219
implies: "full",
218220
group: "Download",
219221
},
220-
upload: {
221-
alias: ["u"],
222-
string: true,
223-
default: undefined,
224-
describe: "Upload files to a URL with a PUT request",
225-
group: "Download",
226-
},
227222
sync: {
228223
boolean: true,
229224
default: false,
@@ -234,7 +229,7 @@ function buildParser(args, callback) {
234229
alias: "k",
235230
number: true,
236231
default: 4,
237-
describe: "Parallel download / upload threads",
232+
describe: "Parallel download / depot threads",
238233
group: "Download",
239234
},
240235
waitDownload: {
@@ -244,6 +239,18 @@ function buildParser(args, callback) {
244239
describe: "Download media after scraping",
245240
group: "Download",
246241
},
242+
bucket: {
243+
string: true,
244+
default: undefined,
245+
describe: "Upload files to an AWS S3 bucket",
246+
group: "Upload",
247+
},
248+
depot: {
249+
string: true,
250+
default: undefined,
251+
describe: "Upload files to a URL with a PUT request (depot)",
252+
group: "Upload",
253+
},
247254
file: {
248255
alias: ["o"],
249256
string: true,
@@ -351,33 +358,48 @@ async function spawn(args) {
351358
.replace("[id]", args["id"])
352359
.replace("[endpoint]", args["_"]);
353360

354-
// Replace upload url
355-
let uploadUrl = args["upload"];
356-
if (uploadUrl && uploadUrl.includes("[uuid]")) {
357-
uploadUrl = uploadUrl.replace("[uuid]", uuid());
361+
// Replace depot url
362+
let depotUrl = args["depot"];
363+
if (depotUrl && depotUrl.includes("[uuid]")) {
364+
depotUrl = depotUrl.replace("[uuid]", uuid());
358365
if (!args["quiet"]) {
359-
process.stdout.write(uploadUrl + "\n");
366+
process.stdout.write(depotUrl + "\n");
360367
}
361368
}
362369

370+
// Get s3 bucket
371+
const s3Bucket = args["bucket"];
372+
363373
// Check if outputting to stdout
364374
const printOutput = args["file"] === "-";
365375

366376
// Connect to object storage
367377
let downloadUpload;
368378
let toCSVFunc = toCSV;
369379
let toJSONFunc = toJSON;
370-
if (uploadUrl) {
371-
// Upload
372-
const uploadConfig = {
380+
if (depotUrl) {
381+
// Depot
382+
const depotConfig = {
383+
directory: downdir,
384+
url: depotUrl,
385+
logger,
386+
};
387+
388+
downloadUpload = depotUpload.depot.bind(depotConfig);
389+
toCSVFunc = depotUpload.toCSV.bind(depotConfig);
390+
toJSONFunc = depotUpload.toJSON.bind(depotConfig);
391+
} else if (s3Bucket) {
392+
// s3
393+
const s3Config = {
394+
bucket: s3Bucket,
373395
directory: downdir,
374-
url: uploadUrl,
396+
s3: new aws.S3(),
375397
logger,
376398
};
377399

378-
downloadUpload = upload.upload.bind(uploadConfig);
379-
toCSVFunc = upload.toCSV.bind(uploadConfig);
380-
toJSONFunc = upload.toJSON.bind(uploadConfig);
400+
downloadUpload = s3Upload.s3.bind(s3Config);
401+
toCSVFunc = s3Upload.toCSV.bind(s3Config);
402+
toJSONFunc = s3Upload.toJSON.bind(s3Config);
381403
} else {
382404
// Download
383405
downloadUpload = download.bind({
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface IUpload {
1212
logger: winston.Logger;
1313
}
1414

15-
export async function upload(
15+
export async function depot(
1616
this: IUpload,
1717
url: string,
1818
name: string,
@@ -34,7 +34,7 @@ export async function upload(
3434
const filePath = path.join(this.directory, name + "." + extension);
3535
const uploadUrl = resolve(this.url, filePath);
3636

37-
// Axios upload
37+
// Axios depot
3838
await axios({
3939
data: downloadStream.data,
4040
headers: {
@@ -45,12 +45,10 @@ export async function upload(
4545
method: "PUT",
4646
...authURL(uploadUrl),
4747
}).catch((error) => {
48-
this.logger.info(`Downloading ${url} failed`);
49-
this.logger.debug(error);
48+
this.logger.error(`Uploading ${url} failed`, error);
5049
});
5150
} catch (e) {
52-
this.logger.info(`Downloading ${url} failed`);
53-
this.logger.debug(e);
51+
this.logger.error(`Uploading ${url} failed`, e);
5452
}
5553
}
5654

0 commit comments

Comments
 (0)