2017-04-12 71 views
6

我遇到了一個我正在編寫的Android應用程序的問題。我們在我們的Amazon S3存儲桶中存儲了一些照片,當我嘗試從應用中訪問它們時,我的TransferListener中的回調永遠不會被調用。Amazon S3 TransferListener永遠不會被調用

// S3Networker.java singleton 
static final String AWS_COGNITO_POOL_ID = "OUR ID IS HERE"; 
static final String AWS_BUCKET_NAME = "OUR BUCKET NAME"; 
TransferUtility transferUtility = null; 

private S3Networker instance; 

private S3Networker() { 
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
      Application.getContext(), 
      AWS_COGNITO_POOL_ID, 
      Regions.US_WEST_2 
    ); 
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider); 
    transferUtility = new TransferUtility(s3Client,Application.getContext()); 
} 

public getInstance() { 
    if(instance == null) { 
     instance = new S3Networker(); 
    } 
    return instance; 
} 

// ... 
// Some class methods that don't touch S3 
// ... 

// S3 Download Method 
public void S3PhotoFetch() { 
    final String objectKey = "path/to/item.jpg"; 
    final File fileDownload = new File(Application.getContext().getCacheDir(), objectKey); 
    TransferObserver transferObserver = transferUtility.download(
      AWS_PHOTO_BUCKET_NAME, 
      objectKey, 
      fileDownload); 
    transferObserver.setTransferListener(new TransferListener(){ 

     @Override 
     public void onStateChanged(int id, TransferState state) { 
      Log.d(TAG, "onStateChanged: " + state); 
      if (TransferState.COMPLETED.equals(state)) { 
       Log.d(TAG, "Download finished"); 
      } 
     } 

     @Override 
     public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { } 

     @Override 
     public void onError(int id, Exception ex) { 
      Log.e(TAG, "onError: ", ex); 
     } 
    }); 
} 

該文件運行良好,沒有任何崩潰。但是,應用程序從不會調用TransferListener中的onStateChanged或onError函數,而且我以後也不會從S3接收數據。有人可以解釋爲什麼,以及如何解決它?

謝謝!

+0

你試圖使用S3 TransferUtility樣品上https://github.com/awslabs/aws-sdk -android-samples? 此示例執行您要完成的任務 –

+0

我已經使用了一些示例應用程序,其中包括上述應用程序。我遇到的問題是OnCrogressChanged()和onError()的回調函數在這個特定的應用程序中沒有被調用,儘管它們在其他應用程序中。 – ellman121

+0

您可以發佈您的AndroidManifest.xml並檢查您是否聲明<服務 android:name =「com.amazonaws.mobileconnectors.s3.transferutility。TransferService」 機器人:啓用=‘真’/>它標籤或不 – Ravi

回答

1

我有同樣的問題。我花了很多時間尋找解決方案,但所有解決方案都不好。我的意思是,你也會遇到上傳大圖片的問題。

所以,試圖通過「多文件上傳」之類https://dzone.com/articles/amazon-s3-parallel-multipart

它爲我解決您的問題。它也解決了上傳大圖片的問題。

4

希望你已經在清單中註冊服務,如果你不需要;請添加清單。確保你的POOL_ID和Region是正確的。

我建議你,因爲從那裏大多發生問題,請檢查您的poolID和地區,

看一看下面的代碼從S3服務器下載文件。與你的相比。

依賴關係:

//aws 
compile 'com.amazonaws:aws-android-sdk-core:2.2.+' 
compile 'com.amazonaws:aws-android-sdk-s3:2.2.+' 

添加您的S3提供給應用程序類,不需要在每一個時間來寫。

AppController.class

public class AppController extends Application { 

    private static Context mContext; 
    private static CognitoCachingCredentialsProvider credentialsProvider; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     mContext = this; 

     credentialsProvider = new CognitoCachingCredentialsProvider(
       getApplicationContext(), /* get the context for the application */ 
       "us-east-4658798797987", /* Identity Pool ID */ 
       Regions.US_WEST_2   /* Region for your identity pool--US_WEST_2 or EU_WEST_2*/ 
     ); 
    } 

    public static Context getContext() { 
     return mContext; 
    } 

    public static CognitoCachingCredentialsProvider getCredentialProvider() { 
     return credentialsProvider; 
    } 
} 

AppController.class &服務清單:

<application 
    android:name=".AppController" 
    ..... 
    .....> 

    <service 
     android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" 
     android:enabled="true" /> 

</application> 

這裏我UTIL創建進行下載呼叫,

public class S3DownloadUtil { 
    private AmazonS3Client mS3Client; 
    private TransferUtility transferUtility; 

    public S3DownloadUtil() { 

     mS3Client = new AmazonS3Client(AppController.getCredentialProvider()); 
     transferUtility = new TransferUtility(mS3Client, AppController.getContext()); 
    } 

    //download method 
    public void download(final String FILE_KEY, File downloadFile) { 
      final TransferObserver observer = transferUtility.download(
        AWS_S3_BUCKET_NAME,  /* The bucket to download from */ 
        FILE_KEY, /* The key for the object to download */ 
        downloadFile  /* The file to download the object to */ 
      ); 

      observer.setTransferListener(new TransferListener() { 
       @Override 
       public void onStateChanged(int id, TransferState state) { 
        switch (state) { 
         case COMPLETED: { 
          transferUtility.deleteTransferRecord(id); 
          String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY); // get resourceUrl 
          Log.d(TAG, "onStateChanged: " + state); 
          break; 
         } 

         case CANCELED: { 
          transferUtility.deleteTransferRecord(id); 
          String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY); // get resourceUrl 
          Log.d(TAG, "onStateChanged: " + state); 
          break; 
         } 

         case FAILED: { 
          transferUtility.deleteTransferRecord(id); 
          String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY); // get resourceUrl 
          Log.d(TAG, "onStateChanged: " + state); 
          break; 
         } 

         case PAUSED: { 
          break; 
         } 

         case WAITING_FOR_NETWORK: { 
          transferUtility.deleteTransferRecord(id); 
          String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY); // get resourceUrl 
          Log.d(TAG, "onStateChanged: " + state); 
          break; 
         } 
        } 
       } 

       @Override 
       public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 

       } 

       @Override 
       public void onError(int id, Exception ex) { 
        ex.printStackTrace(); 
       } 
      }); 
     } 
    } 

現在只需撥打UTIL到如下下載,

String key = "yourKey"; 
String storageFilePath = "your_storage_location"; 
new S3DownloadUtil().download(key, new File(storageFilePath)); 

希望這將幫助你..

+0

。第三次實現s3,但我仍然忘記清單中的這一行,非常感謝! –

相關問題