Skip to content

Commit

Permalink
MinIO
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Aug 8, 2024
1 parent 8a01b29 commit 77df076
Show file tree
Hide file tree
Showing 14 changed files with 1,097 additions and 15 deletions.
4 changes: 0 additions & 4 deletions maven-resolver-named-locks-redisson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<name>Maven Artifact Resolver Named Locks using Redisson</name>
<description>A synchronization utility implementation using Redisson.</description>

<properties>
<testcontainersVersion>1.20.1</testcontainersVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
Expand Down
151 changes: 151 additions & 0 deletions maven-resolver-transport-minio/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver</artifactId>
<version>2.0.2-SNAPSHOT</version>
</parent>

<artifactId>maven-resolver-transport-minio</artifactId>
<packaging>jar</packaging>

<name>Maven Artifact Resolver Transport S3 MinIO</name>
<description>Maven Artifact Transport S3 MinIO (Java).</description>

<properties>
<javaVersion>8</javaVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-spi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.11</version>
</dependency>

<!-- Overrides/Updates for Minio Java -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.17.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-test-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainersVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainersVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>minio</artifactId>
<version>${testcontainersVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.transport.minio;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import io.minio.UploadObjectArgs;
import io.minio.errors.ErrorResponseException;
import org.eclipse.aether.ConfigurationProperties;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.AuthenticationContext;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
import org.eclipse.aether.spi.connector.transport.GetTask;
import org.eclipse.aether.spi.connector.transport.PeekTask;
import org.eclipse.aether.spi.connector.transport.PutTask;
import org.eclipse.aether.spi.connector.transport.Transporter;
import org.eclipse.aether.transfer.NoTransporterException;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.FileUtils;

/**
* A transporter for S3 backed by MinIO Java.
*
* @since 2.0.2
*/
final class MinioTransporter extends AbstractTransporter implements Transporter {
private final URI baseUri;

private final MinioClient client;

private final Function<String, ObjectName> objectNameMapper;

private final Map<String, String> headers;

MinioTransporter(
RepositorySystemSession session, RemoteRepository repository, Function<String, ObjectName> objectNameMapper)
throws NoTransporterException {
this.objectNameMapper = objectNameMapper;
try {
URI uri = new URI(repository.getUrl()).parseServerAuthority();
if (uri.isOpaque()) {
throw new URISyntaxException(repository.getUrl(), "URL must not be opaque");
}
if (uri.getRawFragment() != null || uri.getRawQuery() != null) {
throw new URISyntaxException(repository.getUrl(), "URL must not have fragment or query");
}
String path = uri.getPath();
if (path == null) {
path = "/";
}
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.endsWith("/")) {
path = path + "/";
}
this.baseUri = URI.create(uri.getScheme() + "://" + uri.getRawAuthority() + path);
} catch (URISyntaxException e) {
throw new NoTransporterException(repository, e.getMessage(), e);
}

HashMap<String, String> headers = new HashMap<>();
@SuppressWarnings("unchecked")
Map<Object, Object> configuredHeaders = (Map<Object, Object>) ConfigUtils.getMap(
session,
Collections.emptyMap(),
ConfigurationProperties.HTTP_HEADERS + "." + repository.getId(),
ConfigurationProperties.HTTP_HEADERS);
if (configuredHeaders != null) {
configuredHeaders.forEach((k, v) -> headers.put(String.valueOf(k), v != null ? String.valueOf(v) : null));
}
this.headers = headers;

String username = null;
String password = null;
try (AuthenticationContext repoAuthContext = AuthenticationContext.forRepository(session, repository)) {
if (repoAuthContext != null) {
username = repoAuthContext.get(AuthenticationContext.USERNAME);
password = repoAuthContext.get(AuthenticationContext.PASSWORD);
}
}
if (username == null || password == null) {
throw new NoTransporterException(repository, "No accessKey and/or secretKey provided");
}

try {
this.client = MinioClient.builder()
.endpoint(repository.getUrl())
.credentials(username, password)
.build();
} catch (Exception e) {
throw new NoTransporterException(repository, e);
}
}

@Override
public int classify(Throwable error) {
if (error instanceof ErrorResponseException) {
String errorCode = ((ErrorResponseException) error).errorResponse().code();
if ("NoSuchKey".equals(errorCode) || "NoSuchBucket".equals(errorCode)) {
return ERROR_NOT_FOUND;
}
}
return ERROR_OTHER;
}

@Override
protected void implPeek(PeekTask task) throws Exception {
ObjectName objectName =
objectNameMapper.apply(baseUri.relativize(task.getLocation()).getPath());
StatObjectArgs.Builder builder = StatObjectArgs.builder()
.bucket(objectName.getBucket())
.object(objectName.getName())
.extraHeaders(headers);
client.statObject(builder.build());
}

@Override
protected void implGet(GetTask task) throws Exception {
ObjectName objectName =
objectNameMapper.apply(baseUri.relativize(task.getLocation()).getPath());
try (InputStream stream = client.getObject(GetObjectArgs.builder()
.bucket(objectName.getBucket())
.object(objectName.getName())
.extraHeaders(headers)
.build())) {
final Path dataFile = task.getDataPath();
if (dataFile == null) {
utilGet(task, stream, true, -1, false);
} else {
try (FileUtils.CollocatedTempFile tempFile = FileUtils.newTempFile(dataFile)) {
task.setDataPath(tempFile.getPath(), false);
utilGet(task, stream, true, -1, false);
tempFile.move();
} finally {
task.setDataPath(dataFile);
}
}
}
}

@Override
protected void implPut(PutTask task) throws Exception {
ObjectName objectName =
objectNameMapper.apply(baseUri.relativize(task.getLocation()).getPath());
task.getListener().transportStarted(0, task.getDataLength());
final Path dataFile = task.getDataPath();
if (dataFile == null) {
try (FileUtils.TempFile tempFile = FileUtils.newTempFile()) {
Files.copy(task.newInputStream(), tempFile.getPath(), StandardCopyOption.REPLACE_EXISTING);
client.uploadObject(UploadObjectArgs.builder()
.bucket(objectName.getBucket())
.object(objectName.getName())
.filename(tempFile.getPath().toString())
.build());
}
} else {
client.uploadObject(UploadObjectArgs.builder()
.bucket(objectName.getBucket())
.object(objectName.getName())
.filename(dataFile.toString())
.build());
}
}

@Override
protected void implClose() {
try {
client.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 77df076

Please sign in to comment.