我正在编写一个Android应用程序的问题。我们将一些照片存储在我们的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 :(得分:6)
更新
我已经为s3上传和下载创建了RxJava工具。
希望这会对你有帮助..
希望你在清单中注册服务,如果你没有;请加入清单。确保您的POOL_ID和地区已结束。
我建议您检查您的poolID和区域,因为那里主要出现问题,
看看下面的代码从s3服务器下载文件。与你比较。
依赖关系:
//aws
compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
compile 'com.amazonaws:aws-android-sdk-s3:2.2.+'
将您的S3 Provider添加到Application Class,不需要每次都写。
<强> 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&amp; Manifest中的服务:
<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));
答案 1 :(得分:1)
我有同样的问题。我花了很多时间在搜索解决方案上,但所有解决方案都不是很好。我的意思是你也会遇到上传大图片的问题。
因此,请尝试通过&#34; MultiPart文件上传&#34;解决您的问题。比如https://dzone.com/articles/amazon-s3-parallel-multipart
它对我有用。它还解决了上传大图像的问题。