mirror of
https://github.com/HangarMC/Hangar.git
synced 2025-02-23 15:12:52 +08:00
start to toy around with s3 sdk
This commit is contained in:
parent
d529b35bc7
commit
d2927407c8
@ -10,7 +10,6 @@ fake-user:
|
||||
hangar:
|
||||
dev: false
|
||||
base-url: "https://hangar.benndorf.dev"
|
||||
plugin-upload-dir: "/hangar/uploads"
|
||||
|
||||
announcements:
|
||||
-
|
||||
@ -30,6 +29,14 @@ hangar:
|
||||
token-secret: "${TOKEN_SECRET}"
|
||||
image-proxy-url: "https://hangar-auth.benndorf.dev/image/%s"
|
||||
|
||||
storage:
|
||||
plugin-upload-dir: "/hangar/uploads"
|
||||
type: "object"
|
||||
access-key: "todo" # todo object storage creds
|
||||
secret-key: "todo"
|
||||
bucket: "todo"
|
||||
object-storage-endpoint: "todo"
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
|
13
pom.xml
13
pom.xml
@ -28,6 +28,7 @@
|
||||
<!-- dependency management -->
|
||||
<jdbi3-bom.version>3.32.0</jdbi3-bom.version>
|
||||
<configurate.version>4.1.2</configurate.version>
|
||||
<aws.version>2.16.60</aws.version>
|
||||
|
||||
<!-- dependencies -->
|
||||
<owasp-html-sanitizer>20220608.1</owasp-html-sanitizer>
|
||||
@ -62,6 +63,13 @@
|
||||
<version>${configurate.version}</version>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>bom</artifactId>
|
||||
<version>${aws.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@ -218,6 +226,11 @@
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- runtime -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -9,8 +9,15 @@ import io.papermc.hangar.HangarApplication;
|
||||
@ConfigurationProperties(prefix = "hangar.storage")
|
||||
public class StorageConfig {
|
||||
|
||||
private String pluginUploadDir = new ApplicationHome(HangarApplication.class).getDir().toPath().resolve("work").toString();
|
||||
// type = local or object
|
||||
private String type = "local";
|
||||
// local
|
||||
private String pluginUploadDir = new ApplicationHome(HangarApplication.class).getDir().toPath().resolve("work").toString();
|
||||
// object
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucket;
|
||||
private String objectStorageEndpoint;
|
||||
|
||||
public String getPluginUploadDir() {
|
||||
return pluginUploadDir;
|
||||
@ -27,4 +34,36 @@ public class StorageConfig {
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public String getObjectStorageEndpoint() {
|
||||
return objectStorageEndpoint;
|
||||
}
|
||||
|
||||
public void setObjectStorageEndpoint(String objectStorageEndpoint) {
|
||||
this.objectStorageEndpoint = objectStorageEndpoint;
|
||||
}
|
||||
|
||||
public String getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public void setBucket(String bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
}
|
||||
|
@ -5,52 +5,85 @@ import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import io.papermc.hangar.config.hangar.StorageConfig;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
|
||||
|
||||
@Service
|
||||
@ConditionalOnProperty(value = "hangar.storage.type", havingValue = "object")
|
||||
public class S3FileService implements FileService {
|
||||
|
||||
private final StorageConfig config;
|
||||
private final S3Client client;
|
||||
|
||||
public S3FileService(StorageConfig config) throws URISyntaxException {
|
||||
this.config = config;
|
||||
this.client = S3Client.builder()
|
||||
.endpointOverride(new URI(config.getObjectStorageEndpoint()))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(config.getAccessKey(), config.getSecretKey())))
|
||||
.region(Region.of("local"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileSystemResource getResource(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
client.headObject(HeadObjectRequest.builder().build());
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDirectory(String dir) {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
try {
|
||||
client.deleteObject(DeleteObjectRequest.builder().bucket(config.getBucket()).key(path).build());
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
// TODO does this make remotely sense?
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] bytes(String path) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(); // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(InputStream inputStream, String path) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(String oldPath, String newPath) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link(String existingPath, String newPath) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException();// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolve(String path, String fileName) {
|
||||
throw new UnsupportedOperationException();
|
||||
if (path.endsWith("/")) {
|
||||
return path + fileName;
|
||||
} else {
|
||||
return path + "/" + fileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -182,6 +182,10 @@ hangar:
|
||||
|
||||
storage:
|
||||
type: "object"
|
||||
access-key: "maaWFU0y2agLgJBD"
|
||||
secret-key: "toOBAwlz3ZC6c4dvbFh7Ywk8Y2J4b3kS"
|
||||
bucket: "test"
|
||||
object-storage-endpoint: "http://127.0.0.1:9000"
|
||||
|
||||
#################
|
||||
# Debug Logging #
|
||||
|
Loading…
Reference in New Issue
Block a user