我正在尝试让我的grails应用程序与Amazon S3一起使用,我一直在关注以下文档... http://agorapulse.github.io/grails-aws-sdk/guide/single.html
在下面的步骤amazonWebService.s3.putObject(new PutObjectRequest('some-grails-bucket', 'somePath/someKey.jpg', new File('/Users/ben/Desktop/photo.jpg')).withCannedAcl(CannedAccessControlList.PublicRead))
该项目无法解析类PutObjectRequest
,我尝试手动导入com.amazonaws.services.s3.model.PutObjectRequest
,但仍找不到该类。我唯一能想到的是,尽管我只按照教程学习,但我可能拥有较旧版本的SDK。
我的BuildConfig.groovy
...
...
dependencies{
//dependencies for amazon aws plugin
build 'org.apache.httpcomponents:httpcore:4.3.2'
build 'org.apache.httpcomponents:httpclient:4.3.2'
runtime 'org.apache.httpcomponents:httpcore:4.3.2'
runtime 'org.apache.httpcomponents:httpclient:4.3.2'
}
plugins{
...
runtime ':aws-sdk:1.9.40'
}
还有其他人遇到这个问题并有解决方案吗?
最佳答案
我不使用插件,我只是直接使用SDK。不确定您需要使用什么插件。您不需要httpcomponents即可工作
将此添加到您的依赖项块中:
compile('com.amazonaws:aws-java-sdk-s3:1.10.2') {
exclude group: 'com.fasterxml.jackson.core'
}
这是我用的 bean 。我在Bean配置中设置了密钥,访问权和存储桶数据
class AmazonStorageService implements FileStorageService {
String accessKeyId
String secretAccessKey
String bucketName
AmazonS3Client s3client
@PostConstruct
private void init() {
s3client = new AmazonS3Client(new BasicAWSCredentials(accessKeyId, secretAccessKey));
}
String upload(String name, InputStream inputStream) {
s3client.putObject(new PutObjectRequest(bucketName, name, inputStream, null).withCannedAcl(CannedAccessControlList.PublicRead));
getUrl(name)
}
String upload(String name, byte[] data) {
upload(name, new ByteArrayInputStream(data))
}
String getUrl(String name) {
s3client.getUrl(bucketName, name)
}
Boolean exists(String name) {
try {
s3client.getObjectMetadata(bucketName, name)
true
} catch(AmazonServiceException e) {
false
}
}
}