S3 storage: fix issues from code review

This commit is contained in:
Skye 2024-02-23 18:30:38 +09:00
parent 09473e7b72
commit 43adac0dbf
No known key found for this signature in database
GPG Key ID: 0104BC05F41B77B8
2 changed files with 32 additions and 41 deletions

View File

@ -8,15 +8,14 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import java.util.Optional; import java.util.Optional;
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
@DebugDump
@ConfigSerializable @ConfigSerializable
public class S3Config extends StorageConfig implements S3StorageSettings { public class S3Config extends StorageConfig implements S3StorageSettings {
private String endpoint = null; @DebugDump private String endpoint = null;
private String region = ""; @DebugDump private String region = "";
private String accessKey = ""; @DebugDump private String accessKey = "";
private String secretKey = ""; private String secretKey = "";
private String bucket = ""; @DebugDump private String bucket = "";
private Compression compression = Compression.GZIP; @DebugDump private Compression compression = Compression.GZIP;
@Override @Override
public Optional<String> getEndpoint() { public Optional<String> getEndpoint() {

View File

@ -5,6 +5,7 @@ import de.bluecolored.bluemap.core.storage.*;
import de.bluecolored.bluemap.core.util.OnCloseOutputStream; import de.bluecolored.bluemap.core.util.OnCloseOutputStream;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.S3Client;
@ -62,10 +63,9 @@ public class S3Storage extends Storage {
.build() .build()
); );
return Optional.of(new CompressedInputStream(is, compression)); return Optional.of(new CompressedInputStream(is, compression));
} catch (Exception e) { } catch (NoSuchKeyException e) {
if (e instanceof NoSuchKeyException) { return Optional.empty();
return Optional.empty(); } catch (SdkException e) {
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -104,10 +104,9 @@ public class S3Storage extends Storage {
return info.lastModified().getEpochSecond(); return info.lastModified().getEpochSecond();
} }
}); });
} catch (Exception e) { } catch (NoSuchKeyException e) {
if (e instanceof NoSuchKeyException) { return Optional.empty();
return Optional.empty(); } catch (SdkException e) {
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -123,10 +122,8 @@ public class S3Storage extends Storage {
.key(path) .key(path)
.build() .build()
); );
} catch (Exception e) { } catch (NoSuchKeyException ignored) {
if (e instanceof NoSuchKeyException) { } catch (SdkException e) {
return;
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -166,10 +163,9 @@ public class S3Storage extends Storage {
.build() .build()
); );
return Optional.of(is); return Optional.of(is);
} catch (Exception e) { } catch (NoSuchKeyException e) {
if (e instanceof NoSuchKeyException) { return Optional.empty();
return Optional.empty(); } catch (SdkException e) {
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -197,10 +193,9 @@ public class S3Storage extends Storage {
return info.contentLength(); return info.contentLength();
} }
}); });
} catch (Exception e) { } catch (NoSuchKeyException e) {
if (e instanceof NoSuchKeyException) { return Optional.empty();
return Optional.empty(); } catch (SdkException e) {
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -216,10 +211,8 @@ public class S3Storage extends Storage {
.key(path) .key(path)
.build() .build()
); );
} catch (Exception e) { } catch (NoSuchKeyException ignored) {
if (e instanceof NoSuchKeyException) { } catch (SdkException e) {
return;
}
throw new IOException(e); throw new IOException(e);
} }
} }
@ -238,22 +231,21 @@ public class S3Storage extends Storage {
var filesList = files.contents().stream().collect(Collectors.toList()); var filesList = files.contents().stream().collect(Collectors.toList());
for (int i = 0; i < filesList.size(); i++) { for (int i = 0; i < filesList.size(); i++) {
var file = filesList.get(i); var file = filesList.get(i);
client.deleteObject( try {
DeleteObjectRequest client.deleteObject(
.builder() DeleteObjectRequest
.bucket(bucket) .builder()
.key(file.key()) .bucket(bucket)
.build() .key(file.key())
); .build()
);
} catch (NoSuchKeyException ignored) {}
if (!onProgress.apply( if (!onProgress.apply(
new ProgressInfo((double) (i + 1) / filesList.size()) new ProgressInfo((double) (i + 1) / filesList.size())
)) return; )) return;
} }
} catch (Exception e) { } catch (SdkException e) {
if (e instanceof NoSuchKeyException) {
return;
}
throw new IOException(e); throw new IOException(e);
} }
} }