gpt4 book ai didi

jakarta-ee - 需要用Java在S3上上传文件

转载 作者:行者123 更新时间:2023-12-03 18:38:30 25 4
gpt4 key购买 nike

我最近开始在 AWS 上工作。我目前正在开发 S3 存储的上传功能。

根据我的理解,可能有两种方法可以将文件上传到 S3:-

  • 客户端的文件上传到我的服务器,我使用我的凭据将此文件上传到 S3 服务器。 [我也可以对客户端隐藏它,因为我不会显示上传详细信息。]
  • 直接上传到 S3

  • 我能够使用 simple upload api 实现第一种方法,但我想跳过“ 将上传的文件写入服务器磁盘 ”部分并将内容直接流式传输到 S3 存储,同时将上传详细信息保存在我的数据库中。我也想实现AWS细节的抽象。我该怎么做 ??请帮忙。
    提前致谢

    最佳答案

    我正在使用字节流数组将文件数据写入 S3 对象。

    接收上传文件的 servlet 代码:-

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;

    import com.src.code.s3.S3FileUploader;

    public class FileUploadHandler extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    try{
    List<FileItem> multipartfiledata = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

    //upload to S3
    S3FileUploader s3 = new S3FileUploader();
    String result = s3.fileUploader(multipartfiledata);

    out.print(result);
    } catch(Exception e){
    System.out.println(e.getMessage());
    }
    }
    }

    将此数据上传为 AWS 对象的代码:-
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.util.List;
    import java.util.UUID;

    import org.apache.commons.fileupload.FileItem;

    import com.amazonaws.AmazonClientException;
    import com.amazonaws.AmazonServiceException;
    import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3Client;
    import com.amazonaws.services.s3.model.ObjectMetadata;
    import com.amazonaws.services.s3.model.PutObjectRequest;
    import com.amazonaws.services.s3.model.S3Object;

    public class S3FileUploader {


    private static String bucketName = "***NAME OF YOUR BUCKET***";
    private static String keyName = "Object-"+UUID.randomUUID();

    public String fileUploader(List<FileItem> fileData) throws IOException {
    AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    String result = "Upload unsuccessfull because ";
    try {

    S3Object s3Object = new S3Object();

    ObjectMetadata omd = new ObjectMetadata();
    omd.setContentType(fileData.get(0).getContentType());
    omd.setContentLength(fileData.get(0).getSize());
    omd.setHeader("filename", fileData.get(0).getName());

    ByteArrayInputStream bis = new ByteArrayInputStream(fileData.get(0).get());

    s3Object.setObjectContent(bis);
    s3.putObject(new PutObjectRequest(bucketName, keyName, bis, omd));
    s3Object.close();

    result = "Uploaded Successfully.";
    } catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, which means your request made it to Amazon S3, but was "
    + "rejected with an error response for some reason.");

    System.out.println("Error Message: " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code: " + ase.getErrorCode());
    System.out.println("Error Type: " + ase.getErrorType());
    System.out.println("Request ID: " + ase.getRequestId());

    result = result + ase.getMessage();
    } catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, which means the client encountered an internal error while "
    + "trying to communicate with S3, such as not being able to access the network.");

    result = result + ace.getMessage();
    }catch (Exception e) {
    result = result + e.getMessage();
    }

    return result;
    }
    }

    注意:- 我使用 aws 属性文件作为凭据。

    希望这可以帮助。

    关于jakarta-ee - 需要用Java在S3上上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22502400/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com