在JavaSpringBoot项目中,映像未上载到AWSS3存储桶
我正试图使用Spring Boot应用程序将映像上载到AWS S3 Bucket。但映像未上载,并且在控制台中显示错误。但我的所有配置都是正确的。在这里,我的awsS3AudioBucketCoverPhoto是我的S3Bucket名称。 这是我的AmazonS3Config文件在JavaSpringBoot项目中,映像未上载到AWSS3存储桶,java,mysql,amazon-web-services,spring-boot,cloud,Java,Mysql,Amazon Web Services,Spring Boot,Cloud,我正试图使用Spring Boot应用程序将映像上载到AWS S3 Bucket。但映像未上载,并且在控制台中显示错误。但我的所有配置都是正确的。在这里,我的awsS3AudioBucketCoverPhoto是我的S3Bucket名称。 这是我的AmazonS3Config文件 @Configuration public class AmazonS3Config { @Value("${aws.access.key.id}") private String awsKeyId;
@Configuration
public class AmazonS3Config
{
@Value("${aws.access.key.id}")
private String awsKeyId;
@Value("${aws.access.key.secret}")
private String awsKeySecret;
@Value("${aws.region}")
private String awsRegion;
@Value("${aws.s3.audio.bucket.cover.photo}")
private String awsS3AudioBucketCoverPhoto;
@Value("${aws.s3.audio.bucket.profile.photo}")
private String awsS3AudioBucketProfilePhoto;
@Bean(name = "awsKeyId")
public String getAWSKeyId() {
return awsKeyId;
}
@Bean(name = "awsKeySecret")
public String getAWSKeySecret() {
return awsKeySecret;
}
@Bean(name = "awsRegion")
public Region getAWSPollyRegion() {
return Region.getRegion(Regions.fromName(awsRegion));
}
@Bean(name = "awsCredentialsProvider")
public AWSCredentialsProvider getAWSCredentials() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(this.awsKeyId, this.awsKeySecret);
return new AWSStaticCredentialsProvider(awsCredentials);
}
@Bean(name = "awsS3AudioBucketCoverPhoto")
public String getAWSS3AudioBucketCoverPhoto() {
return awsS3AudioBucketCoverPhoto;
}
@Bean(name = "awsS3AudioBucketProfilePhoto")
public String getAWSS3AudioBucketProfilePhoto() {
return awsS3AudioBucketProfilePhoto;
}
}
这是我的ServiceImpl类代码
@Override
public String uploadCoverImageToS3Bucket(MultipartFile multipartFileCover, boolean enablePublicReadAccess) {
String fileName = PathCOVER+multipartFileCover.getOriginalFilename();
try {
//creating the file in the server (temporarily)
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(multipartFileCover.getBytes());
fos.close();
PutObjectRequest putObjectRequest = new PutObjectRequest(this.awsS3AudioBucketCoverPhoto, fileName, file);
if (enablePublicReadAccess) {
putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead);
}
this.amazonS3.putObject(putObjectRequest);
//removing the file created in the server
file.delete();
} catch (IOException | AmazonServiceException ex) {
logger.error("error [" + ex.getMessage() + "] occurred while uploading [" + fileName + "] ");
}
return multipartFileCover.getOriginalFilename() + " File uploaded successfully";
}
这是intellij idea控制台中显示的错误
error [https:\elasticbeanstalk-ap-southeast-1-530228581445.s3-ap-southeast-1.amazonaws.com\CoverPhoto\henna.jpg (The filename, directory name, or volume label syntax is incorrect)] occurred while uploading [https://elasticbeanstalk-ap-southeast-1-530228581445.s3-ap-southeast-1.amazonaws.com/CoverPhoto/henna.jpg]
使用时,第一个参数是bucket的名称,第二个参数是object键
似乎作为对象键传递的文件名
具有该值
https://elasticbeanstalk-ap-southeast-1-530228581445.s3-ap-southeast-1.amazonaws.com/CoverPhoto/henna.jpg
这显然不是有效的对象键
我无法从您的代码中找出哪个值
awsS3AudioBucketCoverPhoto
,但一定要检查它-它必须是目标存储桶的名称。是的,我发现了错误。实际上文件名就是错误。我在添加新PutObjectRequest时,在文件名前面添加了AWS文件路径[路径](this.awsS3AudioBucketCoverPhoto,fileName,file)和我的awsS3AudioBucketCoverPhoto是我的bucket名称,我已经在application.properties文件中定义了它