|
| 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 | +} |
0 commit comments