-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Customize the S3
putObject
presigned URL to include S3 metada…
…ta (#1139)
- Loading branch information
Showing
6 changed files
with
172 additions
and
11 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
36 changes: 36 additions & 0 deletions
36
...software/amazon/smithy/aws/swift/codegen/customization/PutObjectPresignedURLMiddleware.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,36 @@ | ||
package software.amazon.smithy.aws.swift.codegen.customization | ||
|
||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.swift.codegen.Middleware | ||
import software.amazon.smithy.swift.codegen.SwiftWriter | ||
import software.amazon.smithy.swift.codegen.integration.steps.OperationSerializeStep | ||
|
||
// This middleware is intended only for use with S3 `PutObject`, and only for use when | ||
// creating a presigned URL. | ||
// | ||
// Generates a middleware that writes S3 object metadata into the HTTP query string. | ||
class PutObjectPresignedURLMiddleware( | ||
inputSymbol: Symbol, | ||
outputSymbol: Symbol, | ||
outputErrorSymbol: Symbol, | ||
private val writer: SwiftWriter | ||
) : Middleware(writer, inputSymbol, OperationSerializeStep(inputSymbol, outputSymbol, outputErrorSymbol)) { | ||
override val typeName = "PutObjectPresignedURLMiddleware" | ||
|
||
override fun generateInit() { | ||
writer.write("public init() {}") | ||
} | ||
|
||
override fun generateMiddlewareClosure() { | ||
writer.apply { | ||
write("let metadata = input.operationInput.metadata ?? [:]") | ||
openBlock("for (metadataKey, metadataValue) in metadata {", "}") { | ||
openBlock("let queryItem = URLQueryItem(", ")") { | ||
write("name: \"x-amz-meta-\\(metadataKey.urlPercentEncoding())\",") | ||
write("value: metadataValue.urlPercentEncoding()") | ||
} | ||
write("input.builder.withQueryItem(queryItem)") | ||
} | ||
} | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
...e/amazon/smithy/aws/swift/codegen/middleware/PutObjectPresignedURLMiddlewareRenderable.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,29 @@ | ||
package software.amazon.smithy.aws.swift.codegen.middleware | ||
|
||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.swift.codegen.SwiftWriter | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewarePosition | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewareRenderable | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewareStep | ||
|
||
// This middleware renderer inserts a custom middleware named `PutObjectPresignedURLMiddleware` | ||
// into the operation stack. It is only intended for use with S3 `PutObject` and only when | ||
// generating a pre-signed URL. | ||
class PutObjectPresignedURLMiddlewareRenderable : MiddlewareRenderable { | ||
|
||
override val name = "PutObjectPresignedURLMiddleware" | ||
|
||
override val middlewareStep = MiddlewareStep.SERIALIZESTEP | ||
|
||
override val position = MiddlewarePosition.AFTER | ||
|
||
override fun render(writer: SwiftWriter, op: OperationShape, operationStackName: String) { | ||
writer.write( | ||
"\$L.\$L.intercept(position: \$L, middleware: \$L())", | ||
operationStackName, | ||
middlewareStep.stringValue(), | ||
position.stringValue(), | ||
name | ||
) | ||
} | ||
} |
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