· 7 years ago · Aug 17, 2018, 07:52 PM
1const Minio = require("minio")
2const yargs = require("yargs")
3const bunyan = require("bunyan")
4const { Writable } = require("stream")
5const filepath = require("path")
6const tmp = require("tmp")
7
8const argv = yargs // eslint-disable-line
9 .usage("Usage: $0 [options] - list all objects in the given path")
10 .option("host", {
11 alias: "H",
12 type: "string",
13 describe: "minio s3 host",
14 })
15 .option("port", {
16 alias: "P",
17 type: "number",
18 describe: "minio server port",
19 })
20 .option("secret", {
21 type: "string",
22 describe: "server secret key",
23 })
24 .option("access", {
25 type: "string",
26 describe: "server access key",
27 })
28 .option("bucket", {
29 alias: "b",
30 type: "string",
31 describe: "storage bucket",
32 })
33 .option("path", {
34 alias: "p",
35 type: "string",
36 describe: "folder path",
37 })
38 .demandOption(["H", "P", "secret", "access", "p", "b"])
39 .help("h")
40 .example(
41 "$0 -H play.minio.io --port 9000 --secret 48kjpqr3u --access furiwer02 -b mybucket -p /content",
42 ).argv
43
44class S3Writer extends Writable {
45 constructor({ client, folder, bucket, logger }) {
46 super({ objectMode: true })
47 this.folder = folder
48 this.client = client
49 this.bucket = bucket
50 this.logger = logger
51 }
52
53 async _write(object, _, done) {
54 try {
55 const fullPath = filepath.join(
56 this.folder,
57 filepath.basename(object.name),
58 )
59 await this.client.fGetObject(this.bucket, object.name, fullPath)
60 this.logger.info("saved %s", fullPath)
61 } catch (error) {
62 this.logger.error("unable to write the file %s", object.name)
63 }
64 done()
65 }
66}
67
68const getLogger = () => {
69 return bunyan.createLogger({ name: "listobject" })
70}
71
72const getS3Client = options => {
73 return new Minio.Client({
74 endPoint: options.host,
75 port: options.port,
76 accessKey: options.access,
77 secretKey: options.secret,
78 useSSL: false,
79 })
80}
81
82const listObjects = options => {
83 const logger = getLogger()
84 const client = getS3Client(options)
85 const { bucket, path } = options
86 const stream = client.listObjects(bucket, path, true)
87 const tmpObj = tmp.dirSync({ prefix: "minio-" })
88 const folder = tmpObj.name
89 stream.pipe(
90 new S3Writer({
91 client,
92 bucket,
93 folder,
94 logger,
95 }),
96 )
97}
98
99listObjects(argv)