Para crear un cliente S3:
import software.amazon.awssdk.auth.credentials.{AwsCredentialsProvider, ProfileCredentialsProvider}
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.{S3Client, S3ClientBuilder}
// Crear un proveedor de credenciales basado en el perfil
val credentialsProvider: AwsCredentialsProvider = ProfileCredentialsProvider.builder()
.profileName("dev-usa")
.build()
// Crear un cliente de Amazon S3 utilizando el builder
val s3ClientBuilder: S3ClientBuilder = S3Client.builder()
.region(Region.US_EAST_1)
.credentialsProvider(credentialsProvider)
// Construir el cliente de Amazon S3
val s3Client: S3Client = s3ClientBuilder.build()
// Usar el cliente de Amazon S3 para realizar operaciones en S3
// ...
import java.io.PrintWriter
import software.amazon.awssdk.services.s3.{S3Client, S3Configuration}
import software.amazon.awssdk.services.s3.model.{PutObjectRequest, PutObjectResponse}
object CsvGenerator {
def main(args: Array[String]): Unit = {
val bucketName = "my-s3-bucket"
val key = "output.csv" // Ruta del fichero de salida en S3
val s3Client = S3Client.builder()
.region(Region.US_EAST_1) // Configura la región de tu bucket de S3
.serviceConfiguration(S3Configuration.builder()
.pathStyleAccessEnabled(true)
.build())
.build()
val writer = new PrintWriter(new S3OutputStream(s3Client, bucketName, key))
try {
// Escribir los datos en el fichero CSV
writer.println("Columna1,Columna2,Columna3")
writer.println("Valor1,Valor2,Valor3")
writer.println("Valor4,Valor5,Valor6")
// ...
println("Fichero CSV generado correctamente en S3.")
} catch {
case e: Exception => e.printStackTrace()
} finally {
writer.close()
}
}
}
class S3OutputStream(s3Client: S3Client, bucketName: String, key: String) extends java.io.OutputStream {
private val putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build()
private val outputStream = s3Client.putObject(putObjectRequest)
override def write(b: Int): Unit = {
outputStream.write(b)
}
override def flush(): Unit = {
outputStream.flush()
}
override def close(): Unit = {
outputStream.close()
}
}
0 comentarios:
Publicar un comentario
Gracias por participar en esta página.