-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
S3 content type user supplied #4895
Merged
shinyhappydan
merged 6 commits into
BlueBrain:master
from
shinyhappydan:s3-content-type-user-supplied
Apr 24, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aea589f
Move implicit class
shinyhappydan 860232b
Add bucketExists method
shinyhappydan 54fe6bc
Don't leak S3 details from headObject
shinyhappydan d8f9525
Allow user to provide content type for S3 file registration
shinyhappydan bf5af4e
Deal with S3 possibly not supplying content type
shinyhappydan 605d822
Use IO.raiseUnless
shinyhappydan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ import cats.effect.{IO, Ref, Resource} | |
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.StoragesConfig.S3StorageConfig | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.DigestAlgorithm | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.operations.s3.client.S3StorageClient.UploadMetadata | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.StorageRejection.StorageNotAccessible | ||
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.operations.s3.client.S3StorageClient.{HeadObject, UploadMetadata} | ||
import ch.epfl.bluebrain.nexus.delta.rdf.syntax.uriSyntax | ||
import ch.epfl.bluebrain.nexus.delta.sdk.error.ServiceError.FeatureDisabled | ||
import fs2.aws.s3.S3 | ||
|
@@ -32,7 +33,7 @@ trait S3StorageClient { | |
|
||
def readFile(bucket: BucketName, fileKey: FileKey): Stream[IO, Byte] | ||
|
||
def headObject(bucket: String, key: String): IO[HeadObjectResponse] | ||
def headObject(bucket: String, key: String): IO[HeadObject] | ||
|
||
def copyObject( | ||
sourceBucket: BucketName, | ||
|
@@ -50,13 +51,20 @@ trait S3StorageClient { | |
): IO[UploadMetadata] | ||
|
||
def objectExists(bucket: String, key: String): IO[Boolean] | ||
def bucketExists(bucket: String): IO[Boolean] | ||
|
||
def baseEndpoint: Uri | ||
} | ||
|
||
object S3StorageClient { | ||
|
||
case class UploadMetadata(checksum: String, fileSize: Long, location: Uri) | ||
case class HeadObject( | ||
fileSize: Long, | ||
contentType: Option[String], | ||
sha256Checksum: Option[String], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same it could be a Digest |
||
sha1Checksum: Option[String] | ||
) | ||
|
||
def resource(s3Config: Option[S3StorageConfig]): Resource[IO, S3StorageClient] = s3Config match { | ||
case Some(cfg) => | ||
|
@@ -93,8 +101,24 @@ object S3StorageClient { | |
override def readFile(bucket: BucketName, fileKey: FileKey): Stream[IO, Byte] = | ||
s3.readFile(bucket, fileKey) | ||
|
||
override def headObject(bucket: String, key: String): IO[HeadObjectResponse] = | ||
client.headObject(HeadObjectRequest.builder().bucket(bucket).key(key).checksumMode(ChecksumMode.ENABLED).build) | ||
override def headObject(bucket: String, key: String): IO[HeadObject] = | ||
client | ||
.headObject( | ||
HeadObjectRequest | ||
.builder() | ||
.bucket(bucket) | ||
.key(key) | ||
.checksumMode(ChecksumMode.ENABLED) | ||
.build | ||
) | ||
.map(resp => | ||
HeadObject( | ||
resp.contentLength(), | ||
Option(resp.contentType()), | ||
Option(resp.checksumSHA256()), | ||
Option(resp.checksumSHA1()) | ||
) | ||
) | ||
|
||
override def copyObject( | ||
sourceBucket: BucketName, | ||
|
@@ -175,13 +199,16 @@ object S3StorageClient { | |
} | ||
} | ||
|
||
implicit class PutObjectRequestOps(request: PutObjectRequest.Builder) { | ||
def deltaDigest(algorithm: DigestAlgorithm): PutObjectRequest.Builder = | ||
algorithm.value match { | ||
case "SHA-256" => request.checksumAlgorithm(ChecksumAlgorithm.SHA256) | ||
case "SHA-1" => request.checksumAlgorithm(ChecksumAlgorithm.SHA1) | ||
case _ => throw new IllegalArgumentException(s"Unsupported algorithm for S3: ${algorithm.value}") | ||
} | ||
override def bucketExists(bucket: String): IO[Boolean] = { | ||
listObjectsV2(bucket) | ||
.redeemWith( | ||
err => | ||
err match { | ||
case _: NoSuchBucketException => IO.pure(false) | ||
case e => IO.raiseError(StorageNotAccessible(e.getMessage)) | ||
}, | ||
_ => IO.pure(true) | ||
) | ||
} | ||
} | ||
|
||
|
@@ -195,7 +222,7 @@ object S3StorageClient { | |
|
||
override def readFile(bucket: BucketName, fileKey: FileKey): Stream[IO, Byte] = Stream.raiseError[IO](disabledErr) | ||
|
||
override def headObject(bucket: String, key: String): IO[HeadObjectResponse] = raiseDisabledErr | ||
override def headObject(bucket: String, key: String): IO[HeadObject] = raiseDisabledErr | ||
|
||
override def baseEndpoint: Uri = throw disabledErr | ||
|
||
|
@@ -215,5 +242,16 @@ object S3StorageClient { | |
key: String, | ||
algorithm: DigestAlgorithm | ||
): IO[UploadMetadata] = raiseDisabledErr | ||
|
||
override def bucketExists(bucket: String): IO[Boolean] = raiseDisabledErr | ||
} | ||
|
||
implicit class PutObjectRequestOps(request: PutObjectRequest.Builder) { | ||
def deltaDigest(algorithm: DigestAlgorithm): PutObjectRequest.Builder = | ||
algorithm.value match { | ||
case "SHA-256" => request.checksumAlgorithm(ChecksumAlgorithm.SHA256) | ||
case "SHA-1" => request.checksumAlgorithm(ChecksumAlgorithm.SHA1) | ||
case _ => throw new IllegalArgumentException(s"Unsupported algorithm for S3: ${algorithm.value}") | ||
} | ||
} | ||
} |
37 changes: 8 additions & 29 deletions
37
delta/testkit/src/main/scala/ch/epfl/bluebrain/nexus/testkit/scalatest/FileMatchers.scala
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 |
---|---|---|
@@ -1,40 +1,19 @@ | ||
package ch.epfl.bluebrain.nexus.testkit.scalatest | ||
|
||
import ch.epfl.bluebrain.nexus.testkit.scalatest.JsonMatchers.field | ||
import io.circe.Json | ||
import org.scalatest.matchers.{HavePropertyMatchResult, HavePropertyMatcher} | ||
import org.scalatest.matchers.HavePropertyMatcher | ||
|
||
object FileMatchers { | ||
|
||
def keywords(expected: (String, String)*): HavePropertyMatcher[Json, Map[String, String]] = keywords(expected.toMap) | ||
|
||
def keywords(expected: Map[String, String]): HavePropertyMatcher[Json, Map[String, String]] = HavePropertyMatcher { | ||
json => | ||
val actual = json.hcursor.downField("_keywords").as[Map[String, String]].toOption | ||
HavePropertyMatchResult( | ||
actual.contains(expected), | ||
"keywords", | ||
expected, | ||
actual.orNull | ||
) | ||
} | ||
def keywords(expected: Map[String, String]): HavePropertyMatcher[Json, Map[String, String]] = | ||
field("_keywords", expected) | ||
|
||
def description(expected: String): HavePropertyMatcher[Json, String] = HavePropertyMatcher { json => | ||
val actual = json.hcursor.downField("description").as[String].toOption | ||
HavePropertyMatchResult( | ||
actual.contains(expected), | ||
"description", | ||
expected, | ||
actual.orNull | ||
) | ||
} | ||
def description(expected: String): HavePropertyMatcher[Json, String] = field("description", expected) | ||
|
||
def name(expected: String): HavePropertyMatcher[Json, String] = HavePropertyMatcher { json => | ||
val actual = json.hcursor.downField("name").as[String].toOption | ||
HavePropertyMatchResult( | ||
actual.contains(expected), | ||
"name", | ||
expected, | ||
actual.orNull | ||
) | ||
} | ||
def name(expected: String): HavePropertyMatcher[Json, String] = field("name", expected) | ||
|
||
def mediaType(expected: String): HavePropertyMatcher[Json, String] = field("_mediaType", expected) | ||
} |
20 changes: 20 additions & 0 deletions
20
delta/testkit/src/main/scala/ch/epfl/bluebrain/nexus/testkit/scalatest/JsonMatchers.scala
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,20 @@ | ||
package ch.epfl.bluebrain.nexus.testkit.scalatest | ||
|
||
import io.circe.{Decoder, Json} | ||
import org.scalatest.matchers.{HavePropertyMatchResult, HavePropertyMatcher} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
object JsonMatchers { | ||
def field[A: Decoder: ClassTag](key: String, expectedValue: A)(implicit | ||
ev: Null <:< A | ||
): HavePropertyMatcher[Json, A] = HavePropertyMatcher { json => | ||
val actual = json.hcursor.downField(key).as[A].toOption | ||
HavePropertyMatchResult( | ||
actual.contains(expectedValue), | ||
key, | ||
expectedValue, | ||
actual.orNull | ||
) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could have directly a
ContentType
here