我有一个非常简单的 scala 项目,作为性能测试的一部分,我尝试将文件复制到 s3 或从 s3 复制文件。我的代码在 kubernetes 中运行,我的组织正在使用 STS。我有一个工作服务帐户,它通过 helm 应用于我的容器。从命令行,我可以使用“aws s3 cp”成功复制文件,但无法使其与 awscala 一起使用。
这是我的代码:
package util
import java.io.{File}
import java.nio.file.{Files, Path, StandardCopyOption}
import awscala._, s3._, sts._
import scala.util.Properties
trait AWSStorageClient {
val bucketName: String = "bucketName"
val fileNameSuffix: String = java.net.InetAddress.getLocalHost().getHostName()
val downloadPath: String = "src/gatling/resources"
implicit val s3 = S3.at(Region.US_WEST_2)
}
trait fsUtils {
def getListOfDirs(dir: String): List[File] = {
val d = new File(dir)
if (d.exists && d.isDirectory) {
d.listFiles.filter(_.isDirectory).toList
} else {
List[File]()
}
}
}
object LogUploader extends AWSStorageClient with fsUtils {
@throws[Exception]
def main(args: Array[String]): Unit = {
val fileList = getListOfDirs("build/reports/gatling")
val reportDir = fileList.head
println("fileList: " + fileList)
val bucket = s3.bucket(bucketName).get
bucket.put(s"simulation-$fileNameSuffix.log",
new java.io.File((s"$reportDir/simulation.log"))
)
}
}
object DataFileDownloader extends AWSStorageClient with fsUtils {
@throws[Exception]
def main(args: Array[String]): Unit = {
val bucket = s3.bucket(bucketName).get
bucket.get("7500.csv")
val d1 = new File("/7500.csv").toPath
val d2 = new File("/src/gatling/resources/7500.csv").toPath
Files.move(d1, d2, StandardCopyOption.ATOMIC_MOVE)
}
}
这是错误:
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied
DataFileDownloader 和 LogUploader 应该分别在加特林模拟之前和之后从 gradle 运行。
有人对如何让我的代码与 STS 一起工作有任何想法吗?我是否需要以某种方式承担角色或在代码中的某个位置提供我的服务帐户?
非常感谢任何帮助。
附加说明: 基于此:https://github.com/aws/aws-sdk-java-v2/issues/1470
我想我可能需要做这样的事情:
import software.amazon.awssdk.auth.credentials.WebIdentityTokenFileCredentialsProvider;
...
S3Client s3 = S3Client.builder()
.credentialsProvider(WebIdentityTokenFileCredentialsProvider.create())
.region(region)
.build();
但我不确定。我是 Scala 和 Awscala 的新手。
最佳答案
您拥有的 java 代码片段是正确的,您必须承担服务帐户角色。我可以确认 java 库 1.11.719 及更高版本可以正常工作。
为了确认这一点,我还观察到了相同的行为:AWS CLI 成功,但 JAVA 代码失败并出现权限错误。
import software.amazon.awssdk.auth.credentials.WebIdentityTokenFileCredentialsProvider;
...
S3Client s3 = S3Client.builder()
.credentialsProvider(WebIdentityTokenFileCredentialsProvider.create())
.region(region)
.build();
关于java - 将 awscala 与 STS 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59760889/