我正在使用 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);
}
}