Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
s3 only allows uploading from one folder

The problem I am having is that I can only upload images from the projects directory (/home/usr/workspace/project/~from here~).

For obvious reasons this won't work when I go to publish this feature. I am not sure where I should configure this differently. Help me stack overflow you're my only hope.

@RequestMapping("/saveImage")
public String getPreparedUploadUrl(@RequestParam File fileName, 
  HttpSession session) throws IOException, InterruptedException {
    java.util.Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(msec);
    ObjectMetadata md = new ObjectMetadata();
    md.setContentType("image/jpg");
    md.setContentLength(fileName.length());
    md.setHeader(fileName.getName(), fileName.getAbsolutePath());
    File file = new File(fileName.getAbsolutePath());
    FileInputStream fis = new FileInputStream(file);
    byte[] content_bytes = IOUtils.toByteArray(fis);
    String md5 = new 
 String(Base64.encodeBase64(DigestUtils.md5(content_bytes)));
    md.setContentMD5(md5);


    GeneratePresignedUrlRequest generatePresignedUrlRequest = 
            new GeneratePresignedUrlRequest("wandering-wonderland-
images", fileName.getName());

    generatePresignedUrlRequest.setMethod(HttpMethod.PUT); 
    generatePresignedUrlRequest.setExpiration(expiration);


    URL s = 
 s3client.generatePresignedUrl(generatePresignedUrlRequest);

    try {

        UploadObject(s, fileName);

    } catch (IOException e) {
        e.printStackTrace();
    }
    session.setAttribute("saved", fileName + " has been saved!");
    return "redirect:/saved3";
}

// working, don't f@$# with it!
public static void UploadObject(URL url, File file) throws 
IOException, InterruptedException {

    HttpURLConnection connection=(HttpURLConnection) 
  url.openConnection();
    InputStream inputStream = new 
   FileInputStream(file.getAbsolutePath());
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT");
    OutputStream out =
            connection.getOutputStream();

    byte[] buf = new byte[1024];
    int count;
    int total = 0;
    long fileSize = file.length();

    while ((count =inputStream.read(buf)) != -1)
    {
        if (Thread.interrupted())
        {
            throw new InterruptedException();
        }
        out.write(buf, 0, count);
        total += count;
        int pctComplete = new Double(new Double(total) / new 
Double(fileSize) * 100).intValue();

        System.out.print("\r");
        System.out.print(String.format("PCT Complete: %d", 
 pctComplete));
    }
    System.out.println();
    out.close();
    inputStream.close();

    int responseCode = connection.getResponseCode();
    System.out.println("Service returned response code " + 
      responseCode);
}
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda