-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
F!! Legger til lagring av rinasaker for gjenny i bucket
Co-authored-by: Daniel.skarpas@nav.no
- Loading branch information
1 parent
c545cbe
commit ebdaaa6
Showing
9 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package no.nav.eessi.pensjon.gcp | ||
|
||
import com.google.cloud.storage.Storage | ||
import com.google.cloud.storage.StorageOptions | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
|
||
@Profile("test", "prod") | ||
@Configuration | ||
class GcpConfig { | ||
|
||
@Bean | ||
fun gcpStorage() : Storage{ | ||
return StorageOptions.getDefaultInstance().service | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/no/nav/eessi/pensjon/gcp/GcpStorageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package no.nav.eessi.pensjon.gcp | ||
|
||
import com.google.cloud.storage.BlobId | ||
import com.google.cloud.storage.BlobInfo | ||
import com.google.cloud.storage.Storage | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.nio.ByteBuffer | ||
|
||
@Component | ||
class GcpStorageService( | ||
@param:Value("\${GCP_BUCKET_NAME}") var bucketname: String, | ||
private val gcpStorage: Storage) { | ||
private val logger = LoggerFactory.getLogger(GcpStorageService::class.java) | ||
|
||
init { | ||
ensureBucketExists() | ||
} | ||
|
||
private fun ensureBucketExists() { | ||
when (gcpStorage.get(bucketname) != null) { | ||
false -> throw IllegalStateException("Fant ikke bucket med navn $bucketname. Må provisjoneres") | ||
true -> logger.info("Bucket $bucketname funnet.") | ||
} | ||
} | ||
|
||
fun lagre(euxCaseId: String) { | ||
if (eksisterer(euxCaseId)) return | ||
val blobInfo = BlobInfo.newBuilder(BlobId.of(bucketname, euxCaseId)).setContentType("application/json").build() | ||
kotlin.runCatching { | ||
gcpStorage.writer(blobInfo).use { | ||
it.write(ByteBuffer.wrap(euxCaseId.toByteArray())) | ||
} | ||
}.onFailure { e -> | ||
logger.error("Feilet med å lagre dokument med id: ${blobInfo.blobId.name}", e) | ||
}.onSuccess { | ||
logger.info("Lagret fil med blobid: ${blobInfo.blobId.name} og bytes: $it") | ||
} | ||
} | ||
|
||
fun eksisterer(storageKey: String): Boolean{ | ||
logger.debug("sjekker om $storageKey finnes i bucket: $bucketname") | ||
val obj = gcpStorage.get(BlobId.of(bucketname, storageKey)) | ||
|
||
kotlin.runCatching { | ||
obj.exists() | ||
}.onFailure { | ||
}.onSuccess { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters