2012-01-31 60 views
4

我是一名使用S3的初學者,我想用它來存儲我的計算機的某些文件。我寫了在Java小程序,它會將我的所有文件和文件夾,但我有這樣的例外:無法在Java中以Amazon S3上傳文件

Infos: Received error response: Status Code: 403, AWS Request ID: F75DC5496B071F7D, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: XtRqjEuUmygswUxFUophRudYKbwi4OY4MK9QnYS4DMrH6JrHZXihUEC4mLZbPqz4 
Exception in thread "main" Status Code: 403, AWS Request ID: F75DC5496B071F7D, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: XtRqjEuUmygswUxFUophRudYKbwi4OY4MK9QnYS4DMrH6JrHZXihUEC4mLZbPqz4 
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:538) 
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:283) 
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:168) 
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2555) 
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1044) 
    at com.socialite.s3export.S3Export.parseDirectory(S3Export.java:89) 
    at com.socialite.s3export.S3Export.parseDirectory(S3Export.java:73) 
    at com.socialite.s3export.S3Export.parse(S3Export.java:48) 
    at com.socialite.s3export.S3Export.main(S3Export.java:122) 

我相信我提供了正確的憑據SDK。我使用了以前的程序中的相同憑據來進行測試,並且工作正常。我可以創建桶,但我無法存儲對象。在我的代碼中,我使用PutObjectRequest來存儲對象。

以下是我的init我的亞馬遜S3對象:

AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
      S3Export.class      .getResourceAsStream("AwsCredentials.properties"))); 

上載代碼:

private void parseDirectory(File fin, File fdefault, String out) 
      throws FileNotFoundException { 

     System.out.println("processing .." + fin.getName()); 

     File[] files = fin.listFiles(); 

     PrintWriter pw = new PrintWriter(fdefault); 

     for (File f : files) { 

      String key = UUID.randomUUID().toString(); 

      String name = ""; 

      PutObjectRequest por = null; 

      if (f.isDirectory()) { 

       name = "d_" + f.getName() + ".properties"; 

       File metadata = new File(out + name); 

       parseDirectory(f, metadata, out); 

       pw.println(key + "=" + name); 

       //upload to s3 
       por = new PutObjectRequest(this.bucketName, key, metadata); 

      } else { 

       name = f.getName(); 

       pw.println(key + "=" + name); 

       por = new PutObjectRequest(this.bucketName, key, f); 

      } 


      this.amazonS3.putObject(por); 
     } 
     pw.close(); 
    } 

我的代碼在這一行失敗:this.amazonS3.putObject(por);

有人可以解釋我的代碼有什麼問題。

回答

5

我終於找到了我的問題anwer。事實是在初始化時,我傳遞了一個錯誤的存儲桶名稱,並且我正在使用不同的名稱來提出請求。就這樣。

+2

請記住,您始終可以使用eclipse aws sdk生成示例項目,並瞭解與S3,SimpleDB等的交互如何工作。 – Kris 2012-02-01 10:02:48