我已经从Maven中包含了依赖项,但我看到了这个错误:
java.lang.ClassNotFoundException:com.amazonaws.auth.AWSCredentials
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.8.5</version>
</dependency>
Follwoing是我正在使用的代码和我想要调用的方法。它从main方法调用时运行完美,但在从请求调用时失败。
公共类ThumbnailProcessor {
public static String processThumbnail(String link, String keyName , String secret ) {
AmazonS3 s3Client = null;
link = StringUtils.removeEnd(link, "/");
int index = link.lastIndexOf("/");
String fileName = StringUtils.substring(link, index + 1, link.length());
String existingBucketName = "staticmedia";
String fileNameWithoutExt = StringUtils.substringBefore(fileName, ".");
String ext = StringUtils.substringAfter(fileName, ".");
InitiateMultipartUploadResult initResponse = null;
String randomname = null;
try {
URL url = new URL(link);
BufferedImage image = ImageIO.read(url);
image = Thumbnails.of(image).forceSize(150, 100).asBufferedImage();
randomname = String.format("%s.%s",
RandomStringUtils.randomAlphanumeric(8), ext);
StringBuilder sb = new StringBuilder(randomname);
sb.insert(0, fileNameWithoutExt + "_");
randomname = sb.toString();
ImageIO.write(image, "jpg", new File("src/main/java/" + randomname));
String filePath = "src/main/java/" + randomname;
AWSCredentials awsCredentials = new BasicAWSCredentials(keyName,
secret);
s3Client = new AmazonS3Client(awsCredentials);
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
existingBucketName, randomname);
initResponse = s3Client.initiateMultipartUpload(initRequest);
File file = new File(filePath);
PutObjectResult item = s3Client.putObject(new PutObjectRequest(
existingBucketName, randomname, file));
return "https://s3-us-west-2.amazonaws.com/"
+ randomname;
} catch (Exception e) {
e.printStackTrace();
s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
existingBucketName, randomname, initResponse.getUploadId()));
}
return null;
}
}