如何将图像直接上传到S3,而不让Ec2中运行的java webapp存储多部分图像?
在我的笔记本电脑localhost中 下面的代码通过浏览器上传图片,然后上传到S3 我知道弹性Beanstalk应用程序运行在没有持久本地存储的AmazonEC2实例上。我们应该直接将文件写入S3 当我的java webapp在Ec2实例中运行时,如何将文件直接写入S3? 这是我用来将图像上传到S3的代码,它在我的本地主机上运行良好如何将图像直接上传到S3,而不让Ec2中运行的java webapp存储多部分图像?,java,amazon-web-services,file-upload,amazon-ec2,Java,Amazon Web Services,File Upload,Amazon Ec2,在我的笔记本电脑localhost中 下面的代码通过浏览器上传图片,然后上传到S3 我知道弹性Beanstalk应用程序运行在没有持久本地存储的AmazonEC2实例上。我们应该直接将文件写入S3 当我的java webapp在Ec2实例中运行时,如何将文件直接写入S3? 这是我用来将图像上传到S3的代码,它在我的本地主机上运行良好 try { final ObjectMetadata objectMetadata = new ObjectMetadata();
try {
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
byte[] contentBytes = null;
try {
final InputStream is = file.getInputStream();
contentBytes = IOUtils.toByteArray(is);
} catch (final IOException e) {
log.error("Failed while reading bytes from %s" + e.getMessage());
}
final Long contentLength = Long.valueOf(contentBytes.length);
objectMetadata.setContentLength(contentLength);
objectMetadata.setHeader("filename", fileNameWithExtn);
/*
* Reobtain the tmp uploaded file as input stream
*/
final InputStream inputStream = file.getInputStream();
final File convFile = new File(fileNameWithExtn);
file.transferTo(convFile);
FileUtils.copyInputStreamToFile(inputStream, convFile);
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, convFile))
.getVersionId();
} catch (final 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());
} catch (final 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.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}
我检查了这篇文章,但其中一个答案说它不适用于文件或字符串,而其他答案没有明确的代码或答案
请帮忙 您可以尝试直接使用前端,查看S3文档中关于直接向S3提交html表单的内容 检查链接以供参考 将表单数据直接上传到AWS S3-Stuff…和Things…() 使用HTML5和Backbone.js()将图像文件直接从web浏览器保存到S3,您也可以通过java来实现。下面的代码将文件直接存储到s3,而不将其本地存储在EC2上
仅仅因为本地存储在重启之间不是持久的,并不意味着它不存在。您可以将文件临时存储在本地文件系统上,只要您不依赖这些文件在以后的任何特定时间可用。另请参见:(如何附加短命卷)谢谢。但是,如果我想检查图像文件并对其进行处理,然后将其存储到后端,该怎么办呢。我想检查,做些别的事情,然后保存到S3。你可以进行内存访问并应用更改
public boolean uploadFileDirectlyToS3(MultipartFile multipartFile, String keyName) {
ObjectMetadata data = new ObjectMetadata();
try {
data.setContentType(multipartFile.getContentType());
data.setContentLength(multipartFile.getSize());
s3Client.putObject(CORE_BUNDLE.getString("bucket.name"), keyName, multipartFile.getInputStream(), data);
//KeyName is the specific and unique path where you wanna place your file in s3
return true;
} catch (Exception e) {
logger.error("Error occured while uploading file to s3 having name: "+multipartFile.getOriginalFilename(), e);
return false;
}
}