我正在使用AWS的Java SDK将一些文件(> 130 MB)上传到S3存储桶中。
我遇到的问题是在上传过程中(使用TransferManager
),getBytesTransferred()
会返回一个大于文件大小本身的数字。
这导致我的进度对话框以100%挂起。
我错过了某种配置吗?
我应该指出,这不会一直发生。
public class PutFileTask extends AsyncTask<Void, Long, UploadZipCode> {
@Override
protected UploadZipCode doInBackground(final Void... params) {
// Initialize S3 Client
BasicAWSCredentials credentials = new BasicAWSCredentials(mS3AccessKey, mS3SecretKey);
AmazonS3Client amazonS3Client = new AmazonS3Client(credentials);
mTransferManager = new TransferManager(credentials);
mUploadStart = new Date(System.currentTimeMillis());
Upload myUpload = mTransferManager.upload(mS3UploadBucket, mFile.getName(), mFile);
myUpload.addProgressListener(new ProgressListener() {
long totalBytesTransferred = 0;
@Override
public void progressChanged(ProgressEvent progressEvent) {
totalBytesTransferred += progressEvent.getBytesTransferred();
Log.d(TAG, "Bytes transferred = " + totalBytesTransferred);
onProgressUpdate(totalBytesTransferred);\
if (progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
cancel(true);
} else if (progressEvent.getEventCode() == ProgressEvent.FAILED_EVENT_CODE
|| progressEvent.getEventCode() == ProgressEvent.CANCELED_EVENT_CODE) {
Log.e(TAG, "Uploaded erred out with AWS Event Code: " + progressEvent.getEventCode());
cancel(true);
}
Log.d(TAG, "Setting completion code to: " + progressEvent.getEventCode());
}
});
try {
myUpload.waitForCompletion();
} catch (InterruptedException e) {
cancel(true);
e.printStackTrace();
return UploadZipCode.CONNECTION_ERROR;
} catch (Exception e) {
cancel(true);
e.printStackTrace();
return UploadZipCode.AWS_ERROR;
}
return UploadZipCode.UPLOAD_SUCCESS;
}
...
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(mActivity);
mProgressDialog.setTitle("Please wait...");
mProgressDialog.setMessage("Uploading your session zip file...");
mProgressDialog.setProgressStyle(mProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setProgress(0);
mProgressDialog.setMax((int) (mFile.length()));
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
...
@Override
protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values);
for (long value : values) {
mProgressDialog.setProgress((int) value);
}
}
...
@Override
protected void onPostExecute(UploadZipCode resultCode) {
super.onPostExecute(resultCode);
mProgressDialog.dismiss();
mListener.onUploadFinished(resultCode);
}
...
@Override
protected void onCancelled(UploadZipCode resultCode) {
super.onCancelled(resultCode);
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
mListener.onUploadFinished(resultCode);
}
}