为何在AWS中未触发setTransferListener?
作者:互联网
我正在尝试将照片从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 one rror(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观察器= transferUtility.upload无法正常工作.并且图像没有被上传.
解决方法:
只需在清单中注明(提及服务)即可.
<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />
标签:amazon-web-services,amazon-s3,amazon-cognito,upload,android 来源: https://codeday.me/bug/20191119/2032323.html