我正在尝试从S3存储桶中下载文件。我之前已经测试过代码,并且在AsycTask中运行时TransferUtility.download可以正常工作。但是,到目前为止,即使没有任何错误,文件也无法下载。
我尝试使用AmazonS3Client.ListObjectsV2列出存储桶中的键,因此我知道文件存在于存储桶中。秘密密钥和访问密钥也正确。
private void fileDownload(String key, File localFile) {
TransferObserver observer = transferUtility.download(
"bucketname",
key,
localFile);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
Log.d("Hello", "Upload state changed to " + state);
if(state == TransferState.COMPLETED){
Log.d("Hello", "Completed closing now");
finishAndRemoveTask();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
int percentDone = (int) percentDonef;
Log.d("Hello", "ID:" + id + " bytesCurrent: " + bytesCurrent
+ " bytesTotal: " + bytesTotal + " " + percentDone + "%");
}
@Override
public void onError(int id, Exception ex) {
Log.e("Hello", ex.toString());
}
});
我希望将文件下载到File localFile中指定的位置,但是什么也没有发生。它只是完成运行,但无法下载。