gpt4 book ai didi

java - AWS S3上传图像在部署到EBS时不起作用,但在本地主机SpringBoot(JAVA)中起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:16:28 25 4
gpt4 key购买 nike

我可以从本地将图像上传到 S3,但在部署到 EBS 时却不能。该代码在我本地运行时可以工作,但当我在 AWS EBS 中部署相同的代码时,图像上传根本不起作用。我没有看到任何错误日志。试图寻找所有死胡同的解决方案。如果有人能指出我正确的方向,我将非常感激。我正在使用 t2.micro 实例

应用程序属性

amazonProperties.endpointUrl: https://s3.us-east-2.amazonaws.com
amazonProperties.bucketName: "BUCKETNAME"

ProductController.java

@Controller
public class ProductController {
@Autowired
private AmazonClient amazonClient;
@Value("${amazonProperties.endpointUrl}")
private String endpointUrl;
@Value("${amazonProperties.bucketName}")
private String bucketName;

@RequestMapping(value="/addmaincategory", method=RequestMethod.POST)
public String
addmaincategory(@ModelAttribute("product") Product product,BindingResult result,Model model){
MultipartFile image= product.getProductImage();
String imageName = amazonClient.addImage(image);
product.setProductImageName(imageName );
productRepository.save(category);
model.addAttribute("product",product);
return "productdetails";
}

}

AmazonClient.java

   @Service
public class AmazonClient {
@Autowired
private AmazonS3 s3client;

@Value("${amazonProperties.endpointUrl}")
private String endpointUrl;
@Value("${amazonProperties.bucketName}")
private String bucketName;


private File convertMultiPartToFile(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}

private void uploadFileTos3bucket(MultipartFile file) {
File file = convertMultiPartToFile(file);
String fileName = "myfile.png";
s3client = AmazonS3ClientBuilder.defaultClient();
PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file).withCannedAcl(CannedAccessControlList.PublicRead);
s3client.putObject(request);
}

}

具有可信实体的 IAM 角色

{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"elasticbeanstalk.amazonaws.com",
"ec2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}

附加策略权限

Attached Policy permission

最佳答案

Elasticbeanstalk 可信实体

 {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "elasticbeanstalk.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

EC2可信实体

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

两种情况下的附加许可

Attached Permission in Both Case

关于java - AWS S3上传图像在部署到EBS时不起作用,但在本地主机SpringBoot(JAVA)中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51664525/

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