4. npm更新命令:npm-check 检查更新 npm-upgrade更新,保证正在运行至少运行Node.js的版本大于8.11+,NPM版本大于5.x
10. 由于从2.7.0版开始TransferService将不会自动启动或停止TransferUtility。所以你必须在你的`Application`里面添加以下代码去启动TransferService服务。
private static String IMAGE_DATA_NAME = "public/user2"+DateUtil.getYear()+""+DateUtil.getMonth()+""+DateUtil.getDay()+""+ RandomUntil.getNewRandomCode(6)+".jpg";
//上传头像地址(用于下次加载图片)
private static String PAHT = Constants.AMAZON_S3_PIC + IMAGE_DATA_NAME;
* 上传到AWS
private void AwsTest() {
AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails userStateDetails) {
Log.i(TAG, "AWSMobileClient initialized. User State is " + userStateDetails.getUserState());
@Override
public void onError(Exception e) {
Log.e(TAG, "Initialization error.", e);
uploadWithTransferUtility();
public void uploadWithTransferUtility() {
AWSCredentials credentials = new BasicAWSCredentials(Constants.AMAZON_S3_KEY, Constants.AMAZON_S3_KEY_PWD);
TransferUtility transferUtility =
TransferUtility.builder()
.context(getApplicationContext())
.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
.s3Client(new AmazonS3Client(credentials))
.build();
TransferObserver uploadObserver =
transferUtility.upload(
IMAGE_DATA_NAME,
new File(mImageCropPath));
// Attach a listener to the observer to get state update and progress notifications
uploadObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if (TransferState.COMPLETED == state) {
// Handle a completed upload.
LogUtil.d(TAG,"AWS上传图片:"+IMAGE_DATA_NAME+"成功");
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
int percentDone = (int)percentDonef;
Log.d("YourActivity", "ID:" + id + " bytesCurrent: " + bytesCurrent
+ " bytesTotal: " + bytesTotal + " " + percentDone + "%");
@Override
public void onError(int id, Exception ex) {
// Handle errors
LogUtil.d(TAG,"AWS上传图片:"+IMAGE_DATA_NAME+"失败");
PAHT = "";
// If you prefer to poll for the data, instead of attaching a
// listener, check for the state and progress in the observer.
if (TransferState.COMPLETED == uploadObserver.getState()) {
// Handle a completed upload.
Log.d("YourActivity", "Bytes Transferred: " + uploadObserver.getBytesTransferred());
Log.d("YourActivity", "Bytes Total: " + uploadObserver.getBytesTotal());
- 在你需要下载的`Activity`的`onCreat`方法中添加
```
* AWS下载
private void AwsDownld() {
AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails userStateDetails) {
Log.i(TAG, "AWSMobileClient initialized. User State is " + userStateDetails.getUserState());
@Override
public void onError(Exception e) {
Log.e(TAG, "Initialization error.", e);
downloadWithTransferUtility();
private void downloadWithTransferUtility() {
TransferUtility transferUtility =
TransferUtility.builder()
.context(getApplicationContext())
.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
.s3Client(new AmazonS3Client(AWSMobileClient.getInstance()))
.build();
TransferObserver downloadObserver =
transferUtility.download(
"public/s3Key.txt",
new File("/path/to/file/localFile.txt"));
// Attach a listener to the observer to get state update and progress notifications
downloadObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if (TransferState.COMPLETED == state) {
// Handle a completed upload.
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;
int percentDone = (int)percentDonef;
Log.d("Your Activity", " ID:" + id + " bytesCurrent: " + bytesCurrent + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
@Override
public void onError(int id, Exception ex) {
// Handle errors
// If you prefer to poll for the data, instead of attaching a
// listener, check for the state and progress in the observer.
if (TransferState.COMPLETED == downloadObserver.getState()) {
// Handle a completed upload.
Log.d("Your Activity", "Bytes Transferred: " + downloadObserver.getBytesTransferred());
Log.d("Your Activity", "Bytes Total: " + downloadObserver.getBytesTotal());
- 最后上传成功后就可以在Amazon S3控制台的桶里查看了