在集成测试中使用“localstack/localstack:3.0.2”时(通过 TestContainers) 我通过 SDK JAVA (kotlin) 生成预签名 url
fun getPresignedPutUrl(bucket: String, key: String): String {
val putRequest = PutObjectRequest.builder()
.bucket(bucket)
.key(key)
.build()
val presignedPutRequest = PutObjectPresignRequest.builder()
.signatureDuration(putExpiration) // The URL expires in 10 minutes.
.putObjectRequest(putRequest)
.build()
val url = s3presigner.use { it.presignPutObject(presignedPutRequest) }
return url.url().toExternalForm()
}
生成的 url 用于从 Spring RestTemplate (groovy) 上传
void putFileToPresignedUrl(Resource resource, String presignedUrl) {
def decodedUri = URLDecoder.decode(presignedUrl, StandardCharsets.UTF_8.toString())
def headers = new HttpHeaders().tap {
setContentType(MediaType.MULTIPART_FORM_DATA)
}
final def body = new LinkedMultiValueMap<>().tap {
set("file", resource)
}
def httpEntity = new HttpEntity(body, headers)
rest.put(decodedUri, httpEntity)
}
日志中没有太多内容,但检查 localstack 会大喊: AWS s3.PutObject => 200
之后,使用aws cli
aws s3api --endpoint-url=http://localhost:52552 get-object --key theFile.json --bucket thetenant1 ~/Downloads/theFile.json
我得到了
{
"Key": "theFile.json",
"LastModified": "2023-12-28T22:47:14+00:00",
"ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"Size": 0,
"StorageClass": "STANDARD",
"Owner": {
"DisplayName": "webfile",
"ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
}
}
如果大小为0,获取对象会给出空(零大小)文件
我错过了什么吗?这些都是 github localstack 中的旧错误(全部关闭),抱怨同样。
嗯,localstack/localstack:3.0.2 的临时错误。最新标签解决了问题:-(