, , . , POOL_ID Region currect.
poolID , ,
s3. .
:
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(),
"us-east-4658798797987",
Regions.US_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>
,
public class S3DownloadUtil {
private AmazonS3Client mS3Client;
private TransferUtility transferUtility;
public S3DownloadUtil() {
mS3Client = new AmazonS3Client(AppController.getCredentialProvider());
transferUtility = new TransferUtility(mS3Client, AppController.getContext());
}
public void download(final String FILE_KEY, File downloadFile) {
final TransferObserver observer = transferUtility.download(
AWS_S3_BUCKET_NAME,
FILE_KEY,
downloadFile
);
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);
Log.d(TAG, "onStateChanged: " + state);
break;
}
case CANCELED: {
transferUtility.deleteTransferRecord(id);
String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY);
Log.d(TAG, "onStateChanged: " + state);
break;
}
case FAILED: {
transferUtility.deleteTransferRecord(id);
String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY);
Log.d(TAG, "onStateChanged: " + state);
break;
}
case PAUSED: {
break;
}
case WAITING_FOR_NETWORK: {
transferUtility.deleteTransferRecord(id);
String resourceUrl = mS3Client.getResourceUrl(observer.getBucket(), FILE_KEY);
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();
}
});
}
}
, ,
String key = "yourKey";
String storageFilePath = "your_storage_location";
new S3DownloadUtil().download(key, new File(storageFilePath));
, .