2015-10-26 151 views
-1

我想第一次配置亞馬遜的S3服務,但我無法上傳文件,因爲我得到以下異常(我已經給了用戶完全訪問S3的權限,但我不斷收到此):Amazon S3 - 方法不允許

不允許的方法(服務:亞馬遜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(); 
     } 
    } 
} 

任何人都知道如何解決?

謝謝你們!

回答

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」)。