Acceso S3
This commit is contained in:
commit
35908fe7c0
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
/bin/
|
||||
*.class
|
||||
/target/
|
||||
.project
|
||||
.classpath
|
||||
.settings/
|
||||
BIN
fichero/subida_archivos_a_s3.jpg
Normal file
BIN
fichero/subida_archivos_a_s3.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 156 KiB |
26
pom.xml
Normal file
26
pom.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.lapaloma.s3</groupId>
|
||||
<artifactId>pfc_acceso_s3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<!-- Dependencia para S3 -->
|
||||
<!-- Source: https://mvnrepository.com/artifact/software.amazon.awssdk/s3 -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<version>2.44.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Dependencia para la autenticación -->
|
||||
<!-- Source: https://mvnrepository.com/artifact/software.amazon.awssdk/auth -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>auth</artifactId>
|
||||
<version>2.44.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
97
src/main/java/org/lapaloma/pfc/AppPrincipal.java
Normal file
97
src/main/java/org/lapaloma/pfc/AppPrincipal.java
Normal file
@ -0,0 +1,97 @@
|
||||
package org.lapaloma.pfc;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.lapaloma.pfc.gestores.GestorClientesServiciosAWS;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
|
||||
|
||||
public class AppPrincipal {
|
||||
private final static String NOMBRE_BUCKET_EN_S3 = "s3-obj-proyecto1";
|
||||
|
||||
private static final int TIEMPO_VIDA_MINUTOS_URL = 60;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
AppPrincipal app = new AppPrincipal();
|
||||
|
||||
// Información del bucket y del archivo a subir
|
||||
int idProyecto = 1;
|
||||
int numeroHito = 2;
|
||||
File documentoEjemplo = new File("fichero/subida_archivos_a_s3.jpg");
|
||||
String claveDocumento = idProyecto + "/" + numeroHito + "/" + documentoEjemplo.getName();
|
||||
|
||||
app.almacenarDocumentoEnS3(claveDocumento, documentoEjemplo);
|
||||
|
||||
String url = app.obtenerURLObjetoS3(claveDocumento);
|
||||
System.out.println("URL del objeto en S3: " + url);
|
||||
}
|
||||
|
||||
|
||||
private void almacenarDocumentoEnS3(String clave, File documento) {
|
||||
|
||||
// Crear el cliente de S3 (usando SDK v2)
|
||||
S3Client clienteS3 = GestorClientesServiciosAWS.getClienteBucketS3();
|
||||
|
||||
|
||||
// Solicitud para subir el archivo
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(NOMBRE_BUCKET_EN_S3).key(clave)
|
||||
.build();
|
||||
|
||||
try {
|
||||
// Realizar la carga del archivo
|
||||
PutObjectResponse response = clienteS3.putObject(putObjectRequest, documento.toPath());
|
||||
|
||||
// Imprimir respuesta de éxito
|
||||
System.out.println("Archivo subido correctamente. ETag: " + response.eTag());
|
||||
} catch (S3Exception e) {
|
||||
System.err.println("No se pudo insertar el ítem: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
clienteS3.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String obtenerURLObjetoS3(String claveDocumento) {
|
||||
String urlObjetoS3 = null;
|
||||
// Crear el cliente de S3 (usando SDK v2)
|
||||
//S3Client clienteS3 = GestorClientesServiciosAWS.getClienteBucketS3();
|
||||
|
||||
// Usar el perfil `learnerlab-profile` (el nombre del perfil en `~/.aws/credentials`)
|
||||
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create("default");
|
||||
|
||||
// Crear el cliente presigner de S3 con el perfil específico
|
||||
S3Presigner presigner = S3Presigner.builder().region(GestorClientesServiciosAWS.REGION_AWS)
|
||||
.credentialsProvider(credentialsProvider) // Usar el perfil especificado
|
||||
.build();
|
||||
|
||||
// Crear la solicitud de obtención de objeto
|
||||
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(NOMBRE_BUCKET_EN_S3).key(claveDocumento)
|
||||
.build();
|
||||
|
||||
// Generar la URL firmada
|
||||
PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(
|
||||
builder -> builder.getObjectRequest(getObjectRequest).signatureDuration(Duration.ofMinutes(TIEMPO_VIDA_MINUTOS_URL)) // Duración de la URL firmada
|
||||
);
|
||||
|
||||
// Obtener la URL firmada
|
||||
URL presignedUrl = presignedRequest.url();
|
||||
urlObjetoS3 = presignedUrl.toString();
|
||||
|
||||
// Cerrar el presigner
|
||||
presigner.close();
|
||||
|
||||
return urlObjetoS3;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package org.lapaloma.pfc.gestores;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
public class GestorClientesServiciosAWS {
|
||||
public final static Region REGION_AWS = Region.US_EAST_1;
|
||||
|
||||
// Evita que pueda construirse un objeto de la clase.
|
||||
private GestorClientesServiciosAWS() {
|
||||
}
|
||||
|
||||
public static S3Client getClienteBucketS3() {
|
||||
S3Client clienteS3 = S3Client.builder()
|
||||
.credentialsProvider(ProfileCredentialsProvider.create())
|
||||
.region(REGION_AWS).build();
|
||||
return clienteS3;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user