Skip to content

Commit

Permalink
feature: Integration with OCI Object Storage temp
Browse files Browse the repository at this point in the history
task:  8697bjvqr
  • Loading branch information
KinTrae committed Jan 7, 2025
1 parent 7102085 commit e9f11eb
Show file tree
Hide file tree
Showing 38 changed files with 686 additions and 464 deletions.
228 changes: 140 additions & 88 deletions backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,93 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>meowhub</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>backend</name>
<description>backend</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>meowhub</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>backend</name>
<description>backend</description>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-addons-resteasy-client-configurator</artifactId>
</dependency>

<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-objectstorage</artifactId>
</dependency>

<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
</dependency>
<!-- Other dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.18.0</version> <!-- Sprawdź najnowszą wersję na Maven Central -->
</dependency>


</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-bom</artifactId>
<version>3.55.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
<version>3.55.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package meowhub.backend.ext.oci;

import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class OCIClientConfiguration {
// Path to OCI configs file
String configurationFilePath = "backend/src/main/resources/config";
String profile = "DEFAULT";

public ObjectStorage getObjectStorage() throws IOException {
//load configs file
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parse(configurationFilePath, profile);
final ConfigFileAuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile);

//build and return client
return ObjectStorageClient.builder().build(provider);
}

}
42 changes: 42 additions & 0 deletions backend/src/main/java/meowhub/backend/ext/oci/OCIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package meowhub.backend.ext.oci;

import jakarta.annotation.security.RolesAllowed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("api/ext")
@Deprecated(since = "only to test connection with OCI Object Storage. Otherwise - don't use!!!", forRemoval = true)
@RolesAllowed("ROLE_ADMIN")
public class OCIController {
private static final String urlPrefix = "https://objectstorage.eu-frankfurt-1.oraclecloud.com";

@Autowired
private OCIUploadService ociUploadService;

@PostMapping(path = "upload")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file){
try {
ociUploadService.upload(file, "TEST/"+file.getOriginalFilename());
return ResponseEntity.ok().body("Uploaded File : "+file.getOriginalFilename());
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}

@GetMapping(path = "file/{fileName}")
public ResponseEntity<Object> getURl(@PathVariable(value = "fileName") String fileName){
try {
return ResponseEntity.ok().body(ociUploadService.getFileObjectUrl(fileName));
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package meowhub.backend.ext.oci;

import com.oracle.bmc.objectstorage.model.CreatePreauthenticatedRequestDetails;
import com.oracle.bmc.objectstorage.model.PreauthenticatedRequest.BucketListingAction;
import com.oracle.bmc.objectstorage.requests.CreatePreauthenticatedRequestRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.CreatePreauthenticatedRequestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.UUID;

@Service
public class OCIUploadService {
private static final String urlPrefix = "https://objectstorage.eu-frankfurt-1.oraclecloud.com";

@Value("${oci.objectstorage.bucket-name}")
private String bucketName;

@Value("${oci.objectstorage.namespaceName}")
private String namespaceName;

@Autowired
private OCIClientConfiguration configuration;

public void upload(MultipartFile file, String objectName) throws Exception {
InputStream inputStream = file.getInputStream();

//build upload request
PutObjectRequest putObjectRequest =
PutObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.objectName(objectName)
.contentLength(file.getSize())
.putObjectBody(inputStream)
.build();

//upload the file
try {
configuration.getObjectStorage().putObject(putObjectRequest);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
configuration.getObjectStorage().close();
}
}

public String getFileObjectUrl(String objectName) throws Exception{
OffsetDateTime expirationTime = OffsetDateTime.now().plusYears(10);

// Build request details
CreatePreauthenticatedRequestDetails createPreauthenticatedRequestDetails = CreatePreauthenticatedRequestDetails.builder()
.name("OCI_Request")
.bucketListingAction(BucketListingAction.Deny)
.objectName(objectName)
//readonly access
.accessType(CreatePreauthenticatedRequestDetails.AccessType.ObjectRead)
//here we set expiration time for
.timeExpires(Date.from(expirationTime.toInstant())).build();

//Build request
CreatePreauthenticatedRequestRequest createPreauthenticatedRequestRequest = CreatePreauthenticatedRequestRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.createPreauthenticatedRequestDetails(createPreauthenticatedRequestDetails)
.opcClientRequestId(UUID.randomUUID().toString()).build();

// send request to oci
CreatePreauthenticatedRequestResponse response = configuration.getObjectStorage().createPreauthenticatedRequest(createPreauthenticatedRequestRequest);
configuration.getObjectStorage().close();

String accessUri = response.getPreauthenticatedRequest().getAccessUri();

return urlPrefix + accessUri;
}
}
Loading

0 comments on commit e9f11eb

Please sign in to comment.