为什么在AWS中没有触发setTransferListener?

时间:2016-03-05 09:44:53

标签: android amazon-web-services amazon-s3 upload amazon-cognito

我正在尝试将照片从Android上传到S3,而我正在关注S3网站上最新的tutorial如何,但看起来TransferObserver似乎没有被触发。我的所有变量都是全局声明的。当我运行此代码时:

               credentialsProvider = new CognitoCachingCredentialsProvider(
                        getApplicationContext(),
                        "us-east-SECRET", // Identity Pool ID
                        Regions.US_EAST_1 // Region
                );

                    // Instantiate an S3 Client
                    // Create an S3 client
                    s3 = new AmazonS3Client(credentialsProvider);

                    // Set the region of your s3 bucket
                    s3.setRegion(Region.getRegion(Regions.DEFAULT_REGION));

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

                    //Upload a file to amazon s3
                    Log.i("imageFile", imageFile.toString());

                    observer = transferUtility.upload(
                            "foond",     /* The bucket to upload to */
                            "key",    /* The key for the uploaded object */
                            imageFile        /* The file where the data to upload exists */
                    );

                    Log.i("observer", observer.getAbsoluteFilePath());
                    Log.i("observer", observer.toString());
                    Log.i("observer", observer.getState().toString());
                    Log.i("observer", String.valueOf(observer.getBytesTransferred()));

                    observer.setTransferListener(new TransferListener() {
                        @Override
                        public void onStateChanged(int id, TransferState state) {
                            Log.i("id", String.valueOf(id));
                            Log.i("TransferState", String.valueOf(state));
                        }

                        @Override
                        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                            Log.i("id", String.valueOf(id));
                            Log.i("bytesCurrent", String.valueOf(bytesCurrent));
                            Log.i("bytesTotal", String.valueOf(bytesTotal));
                        }

                        @Override
                        public void onError(int id, Exception ex) {
                            Log.e("Error", String.valueOf(ex));
                        }
                    });

                    Log.i("observer", observer.getState().toString());
                    Log.i("observer", String.valueOf(observer.getBytesTransferred()));

我在控制台中得到的是

I/OpenGLRenderer: Initialized EGL, version 1.4
W/art: Suspending all threads took: 21.244ms
D/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences
I/imageFile: /storage/emulated/0/DCIM/Camera/IMG_20160223_134442.jpg
V/RenderScript: 0x9ab29000 Launching thread(s), CPUs 4
I/observer: /storage/emulated/0/DCIM/Camera/IMG_20160223_134442.jpg
I/observer: com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver@6e1c6df
W/art: Suspending all threads took: 9.843ms
I/observer: WAITING
I/observer: 0
I/observer: WAITING
I/observer: 0

在我看来,没有任何日志在setTransferListener中打印出来。为什么会这样呢?此外,应该注意的是,图像不会上传到S3。

以下是我对清单

的权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

以下是我导入的依赖项:

compile 'com.amazonaws:aws-android-sdk-core:2.+'
compile 'com.amazonaws:aws-android-sdk-cognito:2.+'
compile 'com.amazonaws:aws-android-sdk-s3:2.+'
compile 'com.amazonaws:aws-android-sdk-ddb:2.+'

我知道S3和cognito的设置是正确的,因为我已经使用了github中提供的示例代码,并且我有一个要上传的文件。问题是代码不适合我在android studio中运行的android版本,我不想经历降级版本的麻烦。

以下是我在gradle中的配置

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "xxxxxxxxx"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

TL; DR:TransferObserver observer = transferUtility.upload无效。而且图片没有上传。

1 个答案:

答案 0 :(得分:3)

只需在清单中写明(提及服务)。

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