简体   繁体   English

将图像上传到S3 Bucket - Java SDK

[英]Uploading Image to S3 Bucket - Java SDK

I am working on an iOS application which connects to my Tomcat/Jersey server. 我正在开发一个连接到我的Tomcat / Jersey服务器的iOS应用程序。

The way I am currently uploading images to S3 is using the following workflow: 我目前将图像上传到S3的方式是使用以下工作流程:
- upload image to a temp folder on my server using a POST request Multipart Form data - 使用POST请求Multipart Form数据将图像上传到我服务器上的临时文件夹
- take the file from the temp folder and using the Amazon Java SDK I upload it to S3 - 从临时文件夹中获取文件并使用Amazon Java SDK将其上传到S3
- delete the image file from the temp folder on the server once upload is completed to S3 - 上传完成后,从服务器上的临时文件夹中删除映像文件到S3

I do not want my iOS app to upload directly to S3 as I want to go through my server to perform some operations but in my opinion this seems redundant and will make the process slower than it may need to be. 我不希望我的iOS应用程序直接上传到S3,因为我想通过我的服务器执行一些操作,但在我看来,这似乎是多余的,并且会使进程变得比它可能需要的慢。 Is there a way to stream the file directly through the Amazon SDK instead of having to temp save it to my server? 有没有办法直接通过亚马逊SDK流式传输文件,而不必临时将其保存到我的服务器?

Thanks for the suggestions. 谢谢你的建议。 I used your answers to solve it by uploading the Input Stream directly through the Amazon SDK like so: 我使用您的答案通过直接通过Amazon SDK上传输入流来解决它,如下所示:

byte[] contents = IOUtils.toByteArray(inputStream);
InputStream stream = new ByteArrayInputStream(contents);

ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(contents.length);
meta.setContentType("image/png");

s3client.putObject(new PutObjectRequest(
        bucketName, fileName, stream, meta)
        .withCannedAcl(CannedAccessControlList.Private));

inputStream.close();

Where inputStream is the input stream received from the iOS application to my server and s3client is the AmazonS3Client initialization with the BasicAWSCredentials . 其中inputStream和iOS应用到我的服务器和接收到的输入流s3clientAmazonS3Client初始化与BasicAWSCredentials

You can get the InputStream by using HttpServletRequest .getInputStream() to save the input stream into a data member then pass it on to the S3 SDK: Execute one of the AmazonS3Client.putObject overloads depending on whether you are uploading data from a file, or a stream. 您可以通过使用HttpServletRequest .getInputStream()将输入流保存到数据成员然后将其传递给S3 SDK来获取InputStream: Execute one of the AmazonS3Client.putObject overloads depending on whether you are uploading data from a file, or a stream. from the Amazon S3 . 来自亚马逊S3

after the upload finishes successfully/with error, you can return a relevant response to the user. 上传成功完成/出错后,您可以向用户返回相关响应。

let me know if you need anymore help. 如果您需要帮助,请告诉我。

If you just want to protect the S3 upload behind some authentication mechanism, you could generate a pre-signed S3 upload url after the user has been authenticated, then you could upload directly to S3 from iOS using that URL. 如果您只想保护S3上传后面的某些身份验证机制,您可以在用户通过身份验证后生成预先签名的S3上传URL ,然后您可以使用该URL直接从iOS上传到S3。 That would meet your authentication requirements but keep all the upload traffic off your web server. 这将满足您的身份验证要求,但保持Web服务器上的所有上传流量。

This is a very common design pattern for allowing users to upload files to S3 after authentication. 这是一种非常常见的设计模式,允许用户在身份验证后将文件上传到S3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM