2017-10-16 200 views
1

我正在使用名爲「imdb-datasets」的亞馬遜aws s3 imdb存儲區來顯示各種電影信息。在嘗試此操作之前,我使用自己的存儲桶測試了代碼,並設法從Android設備上傳和下載測試文件到我的存儲桶,反之亦然。但在嘗試使用imdb-datasets桶的同樣方法時,我開始接受拒絕403的錯誤。在做了一些研究之後,我發現可能的問題是我必須在代碼中指定它是請求者支付的存儲桶。我試圖實現請求者支付位(註釋掉的代碼),但它使我的應用程序崩潰。我不知道如何在我的應用程序中實現它,因爲我無法找到任何指南或解決方案。任何想法如何做到這一點?訪問被拒絕錯誤imdb s3存儲桶

編輯:我也給了應用程序的權限,以便能夠通過Android設備設置通過清單和手動在應用程序中訪問存儲。還分配了與iam用戶和imdb存儲桶的角色策略。

import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

import com.amazonaws.auth.CognitoCachingCredentialsProvider; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility; 
import com.amazonaws.regions.Region; 
import com.amazonaws.regions.Regions; 
import com.amazonaws.services.s3.AmazonS3; 
import com.amazonaws.services.s3.AmazonS3Client; 


import java.io.File; 



public class movieChooseActivity extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_movie_choose); 

    } 

public void getFile(View v) { 


    File sdcard = Environment.getExternalStorageDirectory(); 
    String key = "documents/v1/current/title.basics.tsv.gz"; 
    File file = new File(key, "title.basics.tsv.gz"); 


    // Initialize the Amazon Cognito credentials provider 
    CognitoCachingCredentialsProvider credentialsProvider = new 
    CognitoCachingCredentialsProvider(
      getApplicationContext(), 
      "eu-west-2:4c829519-0596-4fdb-ad04-6b8912e44164", // Identity 
                   //pool ID 
      Regions.EU_WEST_2 // Region 
    ); 


    AmazonS3 s3 = new AmazonS3Client(credentialsProvider); 
    s3.setRegion(Region.getRegion(Regions.US_EAST_1)); 

    //include the appropriate request-payer parameter 
    //s3.getObject(new GetObjectRequest("imdb-datasets", 
     key).withRequesterPays(true)); 



      TransferUtility transferUtility = new TransferUtility(s3, 
      getApplicationContext()); 

    TransferObserver transferObserver = transferUtility.download("imdb- 
    datasets","title.basics.tsv.gz",file); 

    transferObserver.setTransferListener(new TransferListener() { 
     @Override 
     public void onStateChanged(int i, TransferState state) { 
      Log.e("statechange",state + " "); 
      if(state == state.COMPLETED) { 
       Toast.makeText(movieChooseActivity.this, "File transfered", 
       Toast.LENGTH_LONG).show(); 
      } 
      if(state == state.FAILED) { 
       Toast.makeText(movieChooseActivity.this, "Failed", 
       Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onProgressChanged(int i, long l, long l1) { 

     } 

     @Override 
     public void onError(int i, Exception e) { 
      Log.e("whattowatchapp","Error:"+ e.getMessage()); 
     } 
    }); 
} 

} 
+0

確保您沒有存儲的運行時權限問題。不知道然後PLZ谷歌 –

回答

0

文件位於文件/ V1/*

在你的榜樣,關鍵是隻是title.basics.tsv.gz

在他們的代碼示例here

private static String bucketName = "imdb-datasets"; 
private static String key  = "documents/v1/current/name.basics.tsv.gz";  

public static void main(String[] args) throws IOException, InterruptedException { 
    ProfileCredentialsProvider credentialsProvider = 
      new ProfileCredentialsProvider("*** FULL PATH TO AWS CREDENTIALS FILE ***", 
              "*** CREDENTIALS PROFILE NAME ***"); 

    AmazonS3 s3Client = new AmazonS3Client(credentialsProvider); 

    try { 
     // Note: It's necessary to set RequesterPays to true 
     GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key) 
               .withRequesterPays(true); 

http://www.imdb.com/interfaces/

我不知道該應用程序是什麼呢還是一個,但對他們的許可

提醒您可以保留此數據的本地副本,它是受到我們的條款和條件。請參閱Non-Commercial Licensingcopyright/license並驗證合規性。