我尝试第一次配置来自亚马逊的S3服务,但我无法上传文件,因为我收到以下异常(我已经授予用户对S3的完全访问权限,但是我一直得到这个):
方法不允许(服务:Amazon S3;状态代码:405;错误代码:405方法不允许; ...)
下面的类是我的配置类(为了安全起见,密钥更改为" xxx"
public class AmazonConfiguration {
private final String ACCESS_KEY = "xxxxxxxxx";
private final String SECRET_KEY = "xxxxxxxxx";
@Bean
private AmazonS3 amazonS3client() {
//Credenciais de acesso
AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration());
s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
s3Client.setEndpoint("http://springmvccasadocodigo.s3-website-us-west-2.amazonaws.com/"); //springmvccasadocodigo.s3-website-sa-east-1.amazonaws.com
return s3Client;
}
}
下一课是我尝试上传文件的地方:
@Component
public class FileServer {
@Autowired
private HttpServletRequest request;
@Autowired
private AmazonS3 s3;
public String writeAmazonS3(String baseFolder, MultipartFile multpartFile) {
try {
s3.putObject("springmvccasadocodigo",
multpartFile.getOriginalFilename(),
multpartFile.getInputStream(), new ObjectMetadata());
return "http://springmvccasadocodigo.s3-website-us-west-2.amazonaws.com/"
+ multpartFile.getOriginalFilename() + "?noAuth=true";
} catch (AmazonClientException | IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}
任何人都知道如何解决?
谢谢你们!
答案 0 :(得分:1)
s3Client.setEndpoint("http://springmvccasadocodigo.s3-website-us-west-2.amazonaws.com/"); //springmvccasadocodigo.s3-website-sa-east-1.amazonaws.com
这不是一个合适的终点。将其与“路径样式访问”相结合使得调试变得更加困难。 (“路径样式语法...要求您在尝试访问存储区时使用特定于区域的端点。”)
Here's the list of endpoints;如果您的存储桶位于us-west-2中,则正确的端点为s3-us-west-2.amazonaws.com
。但是,这对API的“标签外”使用非常有用,例如连接到OpenStack资源。
我相信你可以简单地删除这两行(“setEndpoint”和“setS3ClientOptions”)。