当前位置: 首页 > 知识库问答 >
问题:

需要在S3上传文件Java

滕英奕
2023-03-14

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

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

>

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

    直接上传到S3

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

  • 共有3个答案

    何长恨
    2023-03-14

    在Linux服务器上创建cfg文件

    vim ~/.aws/credentials
    
    [default]
    aws_access_key_id=AKIAIRVAQ7MASDFRTY
    aws_secret_access_key=PSIAiFv1Fj/2DySyUQWeCfTyosEoOLwqdmqrRlI
    

    然后保存那个文件。用NetBeans编写代码。还可以添加库:

    httpcore-4.4.9。jar
    aws-java-sdk-s3-1.11.367。jar
    aws-java-sdk-kms-1.11.367。jar
    aws-java-sdk-core-1.11.367。jar
    aws-java-sdk-1.4.0.1。jar
    joda-time-2.8.1。jar
    jmespath-java-1.11.367。jar
    jackson-dataformat-cbor-2.6.7。jar
    jackson-databind-2.6.7.1。jar
    jackson-core-2.6.7。jar
    jackson-annotations-2.6.0。jar
    ion-java-1.0.2。jar
    httpcore-4.3.2。jar
    httpclient-4.5.5。jar
    commons-logging-1.1.3。jar
    commons-codec-1.10。罐子

    package tests3;
    
     /**
    *
     * @author ravi.tyagi
    */
    import java.io.File;
    import com.amazonaws.AmazonServiceException;
    import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3Client;
    import com.amazonaws.services.s3.transfer.Download;
    import com.amazonaws.services.s3.transfer.TransferManager;
    import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
    import com.amazonaws.services.s3.transfer.Upload;
    
    public class TestS3 {
    
    
    
    
        public static void main(String[] args) throws Exception {
            String bucketName = "*******BucketName*******";
            String keyName = "recordings/0035db40bb32f4d95f0f23b4514d123g/videos/output/0035db40bb32f4d95f0f23b4514d123g"; // Also the name path from which you want save on S3 without Extension it will be same source file
            String filePath = "/usr/local/WowzaStreamingEngine-4.7.6/content/0035db40bb32f4d95f0f23b4514d123g.mp4";
            try {
                AmazonS3 s3Client = new AmazonS3Client(DefaultAWSCredentialsProviderChain.getInstance());
                TransferManager tm = TransferManagerBuilder.standard().withS3Client(s3Client).build();
                Upload upload = tm.upload(bucketName, keyName, new File(filePath));  
                System.out.println("---****************--->"+s3Client.getUrl(bucketName, "0035db40bb32f4d95f0f23b4514d123g.mp4").toString());
                upload.waitForCompletion();
                System.out.println("---*--->"+s3Client.getUrl(bucketName, "0035db40bb32f4d95f0f23b4514d123g.mp4").toString());
                System.out.println("--**->"+upload.getProgress());
                System.out.println("---***--->"+s3Client.getUrl(bucketName, "0035db40bb32f4d95f0f23b4514d123g.mp4").toString());
                System.out.println("--****->"+upload.getState());
                System.out.println("---*****--->"+s3Client.getUrl(bucketName, "0035db40bb32f4d95f0f23b4514d123g.mp4").toString());
                System.out.println("--******->0");
                System.out.println("Object upload complete");
                System.out.println("--**********-->1");
                tm.shutdownNow();
            } catch (AmazonServiceException e) {
                System.out.println("Exception :- "+e);
            }
            System.out.println("----->2");
    
        }
    
    }
    
    支劲
    2023-03-14

    尝试使用POST对象。使用它不需要编写任何代码。请参阅中的更多详细信息http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html

    如果你不怕javascript,另一个选择是Javascript SDK for S3。你可以在浏览器中运行它。http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html

    预签名url也可以工作,你可以在浏览器(通过javascript)或后端生成url,并让浏览器直接将请求发送到S3。

    孙元明
    2023-03-14

    我使用字节流数组将文件数据写入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属性文件的凭据。

    希望这有帮助。

     类似资料:
    • 问题内容: 我需要2个文本字段和1个文件上传。当我只需要文本字段时,一切都正常,但是当我需要文件上传时,即使我选择了一个文件,验证错误仍然会提示需要文件。有人知道我在做什么错吗?提前谢谢了。 //视图 //控制器 问题答案: 我找到了一种完全符合我想要的解决方案。 我变了 至

    • 我正在尝试使用下面描述的无服务器架构在Amazon s3存储桶中上传文件 API网关- 我正在使用postman发送请求,图像被编码为base64格式,并在键“base64”中传递,然后将其转换为inputstream,并传入

    • 问题内容: 我正在尝试使用Java-AWS API在AWS S3上上传文件。问题是我的应用程序无法上传大型文件,因为堆已达到极限。错误:java.lang.OutOfMemoryError:Java堆空间 我个人认为扩展堆内存不是永久性的解决方案,因为我必须上传最大100 GB的文件。我该怎么办 ? 这是代码片段: 问题答案: 我强烈建议on ,因为: ..如果未提供,则库将必须 缓冲 输入流的内

    • 在此处输入图像描述 其失败错误如下:线程名:jp@gc-终极线程组2-4示例开始:2021-02-04 17:28:44 IST加载时间:562连接时间:0延迟:562大小以字节为单位:595发送字节:84568头大小以字节为单位:446主体大小以字节为单位:149示例计数:1错误计数:1数据类型(“text”“bin”“”):文本响应代码:500响应消息: HTTPSampleResult字段:

    • 当我试图从客户端上传时,我一直得到403。这是因为桶上没有条件吗?如果我只是指定密钥-没有访问密钥,签名或策略-它将上传罚款。 桶策略: CORS(因当地开发而开放) 签名生成 客户:

    • 我正在使用Node上传mp3文件到亚马逊S3。当我上传它时,文件大小是9.0字节,当我转到公共url时,音频文件不会播放。这就是我所拥有的, 我也将权限设置为公共访问。