Skip to content

Commit d6ea5ac

Browse files
committedAug 24, 2020
added minio interface
1 parent 19864c9 commit d6ea5ac

7 files changed

+790
-1
lines changed
 

‎.settings/org.eclipse.buildship.core.prefs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
build.commands=org.eclipse.jdt.core.javabuilder
22
connection.arguments=
3-
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
43
connection.java.home=null
54
connection.jvm.arguments=
65
connection.project.dir=

‎build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ dependencies {
142142
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
143143
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.+'
144144
compile group: 'org.jsoup', name: 'jsoup', version: '1.+'
145+
compile group: 'io.minio', name: 'minio', version: '7.1.0'
145146
}
146147

147148
tasks.withType(Javadoc) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* AbstractStorage
3+
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program in the file lgpl21.txt
17+
* If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package org.loklak.tools.storage;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.File;
25+
import java.io.FileInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.List;
31+
32+
import org.json.JSONObject;
33+
import org.json.JSONTokener;
34+
35+
public abstract class AbstractStorage implements Storage {
36+
37+
38+
@Override
39+
public String getString(String bucket, String object, Charset encoding, String dflt) throws IOException {
40+
byte[] b = getBytes(bucket, object, null);
41+
if (b == null) return dflt;
42+
return new String(b, encoding);
43+
}
44+
45+
@Override
46+
public byte[] getBytes(String bucket, String object, byte[] dflt) throws IOException {
47+
InputStream is;
48+
try {
49+
is = this.get(bucket, object);
50+
} catch (IOException e) {
51+
return dflt;
52+
}
53+
if (is == null) return dflt;
54+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
55+
int i;
56+
byte[] data = new byte[2048];
57+
while ((i = is.read(data, 0, data.length)) > 0) {
58+
buffer.write(data, 0, i);
59+
}
60+
byte[] b = buffer.toByteArray();
61+
return b;
62+
}
63+
64+
@Override
65+
public Boolean getBoolean(String bucket, String object, Boolean dflt) throws IOException {
66+
byte[] b = getBytes(bucket, object, null);
67+
if (b == null) return dflt;
68+
return Boolean.parseBoolean(new String(b, StandardCharsets.UTF_8));
69+
}
70+
71+
@Override
72+
public Double getDouble(String bucket, String object, Double dflt) throws IOException {
73+
byte[] b = getBytes(bucket, object, null);
74+
if (b == null) return dflt;
75+
return Double.parseDouble(new String(b, StandardCharsets.UTF_8));
76+
}
77+
78+
@Override
79+
public Float getFloat(String bucket, String object, Float dflt) throws IOException {
80+
byte[] b = getBytes(bucket, object, null);
81+
if (b == null) return dflt;
82+
return Float.parseFloat(new String(b, StandardCharsets.UTF_8));
83+
}
84+
85+
@Override
86+
public Integer getInt(String bucket, String object, Integer dflt) throws IOException {
87+
byte[] b = getBytes(bucket, object, null);
88+
if (b == null) return dflt;
89+
return Integer.parseInt(new String(b, StandardCharsets.UTF_8));
90+
}
91+
92+
@Override
93+
public Long getLong(String bucket, String object, Long dflt) throws IOException {
94+
byte[] b = getBytes(bucket, object, null);
95+
if (b == null) return dflt;
96+
return Long.parseLong(new String(b, StandardCharsets.UTF_8));
97+
}
98+
99+
@Override
100+
public JSONObject getJSON(String bucket, String object, JSONObject dflt) throws IOException {
101+
byte[] b = getBytes(bucket, object, null);
102+
if (b == null) return dflt;
103+
return new JSONObject(new JSONTokener(new String(b, StandardCharsets.UTF_8)));
104+
}
105+
106+
@Override
107+
public Storage put(String bucket, String object, String value, Charset encoding) throws IOException {
108+
return this.put(bucket, object, value.getBytes(encoding));
109+
}
110+
111+
@Override
112+
public Storage put(String bucket, String object, boolean value) throws IOException {
113+
return this.put(bucket, object, Boolean.toString(value).getBytes(StandardCharsets.UTF_8));
114+
}
115+
116+
@Override
117+
public Storage put(String bucket, String object, double value) throws IOException {
118+
return this.put(bucket, object, Double.toString(value).getBytes(StandardCharsets.UTF_8));
119+
}
120+
121+
@Override
122+
public Storage put(String bucket, String object, float value) throws IOException {
123+
return this.put(bucket, object, Float.toString(value).getBytes(StandardCharsets.UTF_8));
124+
}
125+
126+
@Override
127+
public Storage put(String bucket, String object, int value) throws IOException {
128+
return this.put(bucket, object, Integer.toString(value).getBytes(StandardCharsets.UTF_8));
129+
}
130+
131+
@Override
132+
public Storage put(String bucket, String object, long value) throws IOException {
133+
return this.put(bucket, object, Long.toString(value).getBytes(StandardCharsets.UTF_8));
134+
}
135+
136+
@Override
137+
public Storage put(String bucket, String object, File f) throws IOException {
138+
return this.put(bucket, object, new FileInputStream(f));
139+
}
140+
141+
@Override
142+
public Storage put(String bucket, String object, byte[] b) throws IOException {
143+
return this.put(bucket, object, new ByteArrayInputStream(b));
144+
}
145+
146+
@Override
147+
public Storage put(String bucket, String object, JSONObject value) throws IOException {
148+
return this.put(bucket, object, value.toString(0).getBytes(StandardCharsets.UTF_8));
149+
}
150+
151+
public static void main(String[] args) {
152+
try {
153+
Storage s = new FileStorage(new File("/tmp/storagetest"));
154+
155+
s
156+
.make("test1")
157+
.put("test2", "p1", "hello", StandardCharsets.UTF_8)
158+
.put("test2", "p2", " world", StandardCharsets.UTF_8)
159+
.append("test2", "p3", "test2", "p1", "p2");
160+
161+
List<String> l = s.list();
162+
System.out.println("buckets: " + l.toString());
163+
for (String bucket: l) {
164+
System.out.println("files in bucket " + bucket + ": " + s.list(bucket, ""));
165+
}
166+
167+
System.out.println("p3: " + s.getString("test2", "p3", StandardCharsets.UTF_8, ""));
168+
169+
System.out.println("filter1 in test2: " + s.list("test2", ""));
170+
System.out.println("filter2 in test2: " + s.list("test2", "p"));
171+
System.out.println("filter3 in test2: " + s.list("test2", "p1"));
172+
173+
s.delete("test2", "p1", "p2", "p3");
174+
s.delete("test1");
175+
s.delete("test2");
176+
} catch (IOException e) {
177+
e.printStackTrace();
178+
}
179+
180+
}
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/**
2+
* FileStorage
3+
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program in the file lgpl21.txt
17+
* If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
package org.loklak.tools.storage;
22+
23+
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.util.ArrayList;
31+
import java.util.Collection;
32+
import java.util.List;
33+
import java.util.Map;
34+
35+
public class FileStorage extends AbstractStorage implements Storage {
36+
37+
private File baseFile;
38+
39+
public FileStorage(File base) throws IOException {
40+
if (!base.exists()) base.mkdirs();
41+
if (!base.isDirectory()) throw new IOException(base.toString() + " must be a directory");
42+
this.baseFile = base;
43+
}
44+
public FileStorage(Path base) throws IOException {
45+
this.baseFile = base.toFile();
46+
if (!baseFile.isDirectory()) throw new IOException(base.toString() + " must be a directory");
47+
}
48+
49+
@Override
50+
public S3Storage setTimeout(long connectTimeout, long writeTimeout, long readTimeout) {
51+
return null; // do nothing
52+
}
53+
54+
private File constructFile(String bucket) throws IOException {
55+
if (bucket.indexOf("..") >= 0) throw new IOException("operation not allowed");
56+
return new File(this.baseFile, bucket);
57+
}
58+
59+
private File constructFile(String bucket, String object) throws IOException {
60+
if (bucket.indexOf("..") >= 0 || object.indexOf("..") >= 0) throw new IOException("operation not allowed");
61+
File f = new File(this.baseFile, bucket);
62+
String[] o = object.split("/");
63+
for (int i = 0; i < o.length; i++) f = new File(f, o[i]);
64+
return f;
65+
}
66+
67+
@Override
68+
public boolean exists(String bucket) throws IOException {
69+
return constructFile(bucket).exists();
70+
}
71+
72+
@Override
73+
public StorageMetadata exists(String bucket, String object) throws IOException {
74+
File f = constructFile(bucket, object);
75+
if (!f.exists()) throw new IOException("file does not exist: " + f.toString());
76+
String contentType = Files.probeContentType(f.toPath());
77+
long createdTime = f.lastModified();
78+
long length = f.length();
79+
return new StorageMetadata(bucket, object, contentType, createdTime, length);
80+
}
81+
82+
@Override
83+
public InputStream get(String bucket, String object) throws IOException {
84+
return new FileInputStream(constructFile(bucket, object));
85+
}
86+
87+
@Override
88+
public Storage put(String bucket, String object, InputStream is) throws IOException {
89+
byte[] buffer = new byte[2048];
90+
File f = constructFile(bucket, object);
91+
if (!f.getParentFile().exists()) f.getParentFile().mkdirs();
92+
FileOutputStream fos = new FileOutputStream(f);
93+
int len;
94+
while ((len = is.read(buffer)) > 0) {
95+
fos.write(buffer, 0, len);
96+
}
97+
fos.close();
98+
return this;
99+
}
100+
101+
@Override
102+
public Storage delete(String bucket, String... objects) throws IOException {
103+
if (objects.length == 0) {
104+
// delete the bucket
105+
File f = constructFile(bucket);
106+
f.delete();
107+
} else {
108+
// delete objects
109+
for (String object: objects) {
110+
File f = constructFile(bucket, object);
111+
if (f.exists()) f.delete();
112+
}
113+
}
114+
return this;
115+
}
116+
117+
@Override
118+
public Storage copy(String toBucket, String toObject, String fromBucket, String fromObject) throws IOException {
119+
byte[] buffer = new byte[2048];
120+
File fin = constructFile(fromBucket, fromObject);
121+
File fout = constructFile(toBucket, toObject);
122+
FileInputStream fis = new FileInputStream(fin);
123+
FileOutputStream fos = new FileOutputStream(fout);
124+
int len;
125+
while ((len = fis.read(buffer)) > 0) {
126+
fos.write(buffer, 0, len);
127+
}
128+
fis.close();
129+
fos.close();
130+
return this;
131+
}
132+
133+
@Override
134+
public Storage append(String toBucket, String toObject, String fromBucket, String... fromObjects) throws IOException {
135+
byte[] buffer = new byte[2048];
136+
File fout = constructFile(toBucket, toObject);
137+
FileOutputStream fos = new FileOutputStream(fout, true);
138+
for (String fromObject: fromObjects) {
139+
File fin = constructFile(fromBucket, fromObject);
140+
FileInputStream fis = new FileInputStream(fin);
141+
int len;
142+
while ((len = fis.read(buffer)) > 0) {
143+
fos.write(buffer, 0, len);
144+
}
145+
fis.close();
146+
}
147+
fos.close();
148+
return this;
149+
}
150+
151+
@Override
152+
public Storage append(String toBucket, String toObject, Map<String, Collection<String>> fromMap) throws IOException {
153+
byte[] buffer = new byte[2048];
154+
File fout = constructFile(toBucket, toObject);
155+
FileOutputStream fos = new FileOutputStream(fout, true);
156+
for (Map.Entry<String, Collection<String>> fromEntry: fromMap.entrySet()) {
157+
for (String fromObject: fromEntry.getValue()) {
158+
File fin = constructFile(fromEntry.getKey(), fromObject);
159+
FileInputStream fis = new FileInputStream(fin);
160+
int len;
161+
while ((len = fis.read(buffer)) > 0) {
162+
fos.write(buffer, 0, len);
163+
}
164+
fis.close();
165+
}
166+
}
167+
fos.close();
168+
return this;
169+
}
170+
171+
@Override
172+
public Storage make(String bucket) throws IOException {
173+
File f = constructFile(bucket);
174+
f.mkdirs();
175+
return this;
176+
}
177+
178+
@Override
179+
public List<String> list(String bucket, String prefix) throws IOException {
180+
File f = constructFile(bucket, prefix);
181+
182+
List<String> objects = new ArrayList<>();
183+
if (f.exists() && f.isDirectory()) {
184+
String[] x = f.list();
185+
for (String y: x) objects.add(y);
186+
} else {
187+
String n = f.getName();
188+
f = f.getParentFile();
189+
if (f.exists()) {
190+
String[] x = f.list();
191+
for (String y: x) if (y.startsWith(n)) objects.add(y);
192+
}
193+
}
194+
return objects;
195+
}
196+
197+
@Override
198+
public List<String> list() throws IOException {
199+
String[] files = this.baseFile.list();
200+
List<String> buckets = new ArrayList<>();
201+
for (String file: files) {
202+
if (new File(this.baseFile, file).isDirectory()) buckets.add(file);
203+
}
204+
return buckets;
205+
}
206+
207+
@Override
208+
public List<String> incomplete(String bucket, String prefix) throws IOException {
209+
return new ArrayList<>(0);
210+
}
211+
212+
}
+285
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/**
2+
* S3Storage
3+
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program in the file lgpl21.txt
17+
* If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
package org.loklak.tools.storage;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.ArrayList;
26+
import java.util.Collection;
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
import io.minio.BucketExistsArgs;
31+
import io.minio.ComposeObjectArgs;
32+
import io.minio.ComposeSource;
33+
import io.minio.CopyObjectArgs;
34+
import io.minio.CopySource;
35+
import io.minio.DisableVersioningArgs;
36+
import io.minio.GetObjectArgs;
37+
import io.minio.ListIncompleteUploadsArgs;
38+
import io.minio.ListObjectsArgs;
39+
import io.minio.MakeBucketArgs;
40+
import io.minio.MinioClient;
41+
import io.minio.ObjectStat;
42+
import io.minio.PutObjectArgs;
43+
import io.minio.RemoveBucketArgs;
44+
import io.minio.RemoveObjectArgs;
45+
import io.minio.RemoveObjectsArgs;
46+
import io.minio.Result;
47+
import io.minio.SetBucketNotificationArgs;
48+
import io.minio.StatObjectArgs;
49+
import io.minio.messages.Bucket;
50+
import io.minio.messages.DeleteObject;
51+
import io.minio.messages.EventType;
52+
import io.minio.messages.Item;
53+
import io.minio.messages.NotificationConfiguration;
54+
import io.minio.messages.QueueConfiguration;
55+
import io.minio.messages.Upload;
56+
57+
public class S3Storage extends AbstractStorage implements Storage {
58+
59+
private String endpoint, accessKey, secretKey;
60+
MinioClient s3;
61+
62+
public S3Storage(String endpoint, String accessKey, String secretKey) {
63+
this.endpoint = endpoint;
64+
this.accessKey = accessKey;
65+
this.secretKey = secretKey;
66+
this.s3 = MinioClient.builder()
67+
.endpoint(this.endpoint)
68+
.credentials(this.accessKey, this.secretKey)
69+
.build();
70+
}
71+
72+
@Override
73+
public Storage setTimeout(long connectTimeout, long writeTimeout, long readTimeout) {
74+
this.s3.setTimeout(connectTimeout, writeTimeout, readTimeout);
75+
return this;
76+
}
77+
78+
@Override
79+
public boolean exists(String bucket) throws IOException {
80+
try {
81+
return this.s3.bucketExists(BucketExistsArgs.builder()
82+
.bucket(bucket).build());
83+
} catch (Exception e) {
84+
throw new IOException(e.getMessage());
85+
}
86+
}
87+
88+
@Override
89+
public StorageMetadata exists(String bucket, String object) throws IOException {
90+
try {
91+
ObjectStat stat = this.s3.statObject(StatObjectArgs.builder()
92+
.bucket(bucket).object(object).build());
93+
StorageMetadata m = new StorageMetadata(
94+
stat.bucketName(), stat.name(), stat.contentType(),
95+
stat.createdTime().toEpochSecond() * 1000L, stat.length());
96+
return m;
97+
} catch (Exception e) {
98+
throw new IOException(e.getMessage());
99+
}
100+
}
101+
102+
@Override
103+
public InputStream get(String bucket, String object) throws IOException {
104+
try {
105+
return this.s3.getObject(GetObjectArgs.builder()
106+
.bucket(bucket).object(object).build());
107+
} catch (Exception e) {
108+
throw new IOException(e.getMessage());
109+
}
110+
}
111+
112+
@Override
113+
public Storage put(String bucket, String object, InputStream is) throws IOException {
114+
return this.put(bucket, object, is, is.available(), -1);
115+
}
116+
117+
private Storage put(String bucket, String object, InputStream is, long objectSize, long partSize) throws IOException {
118+
try {
119+
/*ObjectWriteResponse response = */ this.s3.putObject(PutObjectArgs.builder()
120+
.bucket(bucket).object(object).stream(is, objectSize, partSize).build());
121+
return this;
122+
} catch (Exception e) {
123+
throw new IOException(e.getMessage());
124+
}
125+
}
126+
127+
@Override
128+
public Storage delete(String bucket, String... objects) throws IOException {
129+
if (objects.length == 0) try {
130+
/*ObjectWriteResponse response = */ this.s3.removeBucket(RemoveBucketArgs.builder()
131+
.bucket(bucket).build());
132+
return this;
133+
} catch (Exception e) {
134+
throw new IOException(e.getMessage());
135+
} else if (objects.length == 1) try {
136+
/*ObjectWriteResponse response = */ this.s3.removeObject(RemoveObjectArgs.builder()
137+
.bucket(bucket).object(objects[0]).build());
138+
return this;
139+
} catch (Exception e) {
140+
throw new IOException(e.getMessage());
141+
} else try {
142+
List<DeleteObject> o = new ArrayList<>();
143+
for (int i = 0; i < objects.length; i++) {
144+
o.add(new DeleteObject(objects[i]));
145+
}
146+
this.s3.removeObjects(RemoveObjectsArgs.builder().bucket(bucket).objects(o).build());
147+
return this;
148+
} catch (Exception e) {
149+
throw new IOException(e.getMessage());
150+
}
151+
}
152+
153+
@Override
154+
public Storage copy(String toBucket, String toObject, String fromBucket, String fromObject) throws IOException {
155+
try {
156+
/*ObjectWriteResponse response = */ this.s3.copyObject(CopyObjectArgs.builder()
157+
.bucket(toBucket).object(toObject).source(CopySource.builder().bucket(fromBucket).object(fromObject).build()).build());
158+
return this;
159+
} catch (Exception e) {
160+
throw new IOException(e.getMessage());
161+
}
162+
}
163+
164+
@Override
165+
public Storage append(String toBucket, String toObject, String fromBucket, String... fromObjects) throws IOException {
166+
try {
167+
List<ComposeSource> list = new ArrayList<>();
168+
for (String o: fromObjects) {
169+
list.add(ComposeSource.builder().bucket(fromBucket).object(o).build());
170+
}
171+
/*ObjectWriteResponse response = */ this.s3.composeObject(ComposeObjectArgs.builder()
172+
.bucket(toBucket).object(toObject).sources(list).build());
173+
return this;
174+
} catch (Exception e) {
175+
throw new IOException(e.getMessage());
176+
}
177+
}
178+
179+
@Override
180+
public Storage append(String toBucket, String toObject, Map<String, Collection<String>> fromMap) throws IOException {
181+
try {
182+
List<ComposeSource> list = new ArrayList<>();
183+
for (Map.Entry<String, Collection<String>> me: fromMap.entrySet()) {
184+
for (String o: me.getValue()) {
185+
list.add(ComposeSource.builder().bucket(me.getKey()).object(o).build());
186+
}
187+
}
188+
/*ObjectWriteResponse response = */ this.s3.composeObject(ComposeObjectArgs.builder()
189+
.bucket(toBucket).object(toObject).sources(list).build());
190+
return this;
191+
} catch (Exception e) {
192+
throw new IOException(e.getMessage());
193+
}
194+
}
195+
196+
@Override
197+
public Storage make(String bucket) throws IOException {
198+
try {
199+
/*ObjectWriteResponse response = */ this.s3.makeBucket(MakeBucketArgs.builder()
200+
.bucket(bucket).build());
201+
disableVersioning(bucket);
202+
return this;
203+
} catch (Exception e) {
204+
throw new IOException(e.getMessage());
205+
}
206+
}
207+
208+
private Storage disableVersioning(String bucket) throws IOException {
209+
try {
210+
this.s3.disableVersioning(DisableVersioningArgs.builder().bucket(bucket).build());
211+
} catch (Exception e) {
212+
throw new IOException(e.getMessage());
213+
}
214+
return this;
215+
}
216+
217+
@Override
218+
public List<String> list(String bucket, String prefix) throws IOException {
219+
try {
220+
Iterable<Result<Item>> results = this.s3.listObjects(ListObjectsArgs.builder()
221+
.bucket(bucket).prefix(prefix).build());
222+
List<String> i = new ArrayList<>();
223+
for (Result<Item> r: results) {
224+
i.add(r.get().objectName());
225+
}
226+
return i;
227+
} catch (Exception e) {
228+
throw new IOException(e.getMessage());
229+
}
230+
}
231+
232+
@Override
233+
public List<String> list() throws IOException {
234+
try {
235+
List<Bucket> buckets = this.s3.listBuckets();
236+
List<String> bs = new ArrayList<>(buckets.size());
237+
buckets.forEach(b -> bs.add(b.name()));
238+
return bs;
239+
} catch (Exception e) {
240+
throw new IOException(e.getMessage());
241+
}
242+
}
243+
244+
@Override
245+
public List<String> incomplete(String bucket, String prefix) throws IOException {
246+
try {
247+
Iterable<Result<Upload>> results = this.s3.listIncompleteUploads(ListIncompleteUploadsArgs.builder()
248+
.bucket(bucket).recursive(true).prefix(prefix).build());
249+
List<String> i = new ArrayList<>();
250+
for (Result<Upload> r: results) {
251+
i.add(r.get().objectName());
252+
}
253+
return i;
254+
} catch (Exception e) {
255+
throw new IOException(e.getMessage());
256+
}
257+
}
258+
259+
@SuppressWarnings("unused")
260+
private Storage notification(String bucket, String prefix, String suffix) throws IOException {
261+
try {
262+
List<EventType> eventList = new ArrayList<>();
263+
eventList.add(EventType.OBJECT_CREATED_PUT);
264+
eventList.add(EventType.OBJECT_CREATED_COPY);
265+
eventList.add(EventType.OBJECT_ACCESSED_ANY);
266+
QueueConfiguration qc = new QueueConfiguration();
267+
qc.setQueue("arn:minio:sqs::1:webhook");
268+
qc.setEvents(eventList);
269+
qc.setPrefixRule(prefix);
270+
qc.setSuffixRule(suffix);
271+
List<QueueConfiguration> cl = new ArrayList<>();
272+
cl.add(qc);
273+
NotificationConfiguration config = new NotificationConfiguration();
274+
config.setQueueConfigurationList(cl);
275+
276+
/*ObjectWriteResponse response = */ this.s3.setBucketNotification(SetBucketNotificationArgs.builder()
277+
.bucket(bucket).config(config).build());
278+
disableVersioning(bucket);
279+
return this;
280+
} catch (Exception e) {
281+
throw new IOException(e.getMessage());
282+
}
283+
}
284+
285+
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Storage
3+
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program in the file lgpl21.txt
17+
* If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
package org.loklak.tools.storage;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.nio.charset.Charset;
27+
import java.util.Collection;
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
import org.json.JSONObject;
32+
33+
public interface Storage {
34+
35+
public Storage setTimeout(long connectTimeout, long writeTimeout, long readTimeout);
36+
37+
public boolean exists(String bucket) throws IOException;
38+
public StorageMetadata exists(String bucket, String object) throws IOException;
39+
40+
public InputStream get(String bucket, String object) throws IOException;
41+
public byte[] getBytes(String bucket, String object, byte[] dflt) throws IOException;
42+
public String getString(String bucket, String object, Charset encoding, String dflt) throws IOException;
43+
public Boolean getBoolean(String bucket, String object, Boolean dflt) throws IOException;
44+
public Double getDouble(String bucket, String object, Double dflt) throws IOException;
45+
public Float getFloat(String bucket, String object, Float dflt) throws IOException;
46+
public Integer getInt(String bucket, String object, Integer dflt) throws IOException;
47+
public Long getLong(String bucket, String object, Long dflt) throws IOException;
48+
public JSONObject getJSON(String bucket, String object, JSONObject dflt) throws IOException;
49+
50+
public Storage put(String bucket, String object, String value, Charset encoding) throws IOException;
51+
public Storage put(String bucket, String object, boolean value) throws IOException;
52+
public Storage put(String bucket, String object, double value) throws IOException;
53+
public Storage put(String bucket, String object, float value) throws IOException;
54+
public Storage put(String bucket, String object, int value) throws IOException;
55+
public Storage put(String bucket, String object, long value) throws IOException;
56+
public Storage put(String bucket, String object, JSONObject value) throws IOException;
57+
58+
public Storage put(String bucket, String object, File f) throws IOException;
59+
public Storage put(String bucket, String object, byte[] b) throws IOException;
60+
public Storage put(String bucket, String object, InputStream is) throws IOException;
61+
62+
public Storage delete(String bucket, String... objects) throws IOException;
63+
public Storage copy(String toBucket, String toObject, String fromBucket, String fromObject) throws IOException;
64+
65+
public Storage append(String toBucket, String toObject, String fromBucket, String... fromObjects) throws IOException;
66+
public Storage append(String toBucket, String toObject, Map<String, Collection<String>> fromMap) throws IOException;
67+
68+
public Storage make(String bucket) throws IOException;
69+
70+
public List<String> list(String bucket, String prefix) throws IOException;
71+
public List<String> list() throws IOException;
72+
73+
public List<String> incomplete(String bucket, String prefix) throws IOException;
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* StorageMetadata
3+
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program in the file lgpl21.txt
17+
* If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
package org.loklak.tools.storage;
22+
23+
public class StorageMetadata {
24+
25+
public final String bucketName, objectName, contentType;
26+
public final long createdTime, length;
27+
28+
public StorageMetadata(String bucketName, String objectName, String contentType, long createdTime, long length) {
29+
this.bucketName = bucketName;
30+
this.objectName = objectName;
31+
this.contentType = contentType;
32+
this.createdTime = createdTime;
33+
this.length = length;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.