android - AWS s3 TransferListener 未在 Android 中更新

标签 android amazon-web-services amazon-s3 awss3transferutility

我正在尝试从 s3 下载并更新进度条。我设置了 TransferUtility、TransferObserver 和 TransferListener。

问题是,当文件下载时,它很少更新进度。

对于 1.6mb (1665824) 文件,它将输出 0,然后 30 秒后输出 1050264,然后 30 秒后输出 1665824,然后重复自身并再次输出 1665824。

所以基本上对于用户来说,下载看起来像是卡住了或者充其量是不稳定的。

我还尝试将其放入循环中并每隔 100 毫秒检查一次值,但它只在前 30 秒返回 0,就像监听器一样。

这就是我的实现方式:

// Create s3 client and set region and endpoints
        AmazonS3Client s3Client = new AmazonS3Client(IdentityManager.getDefaultIdentityManager().getCredentialsProvider(), new ClientConfiguration());
        s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));

        // Create a tranfer utility and attach to s3 client
        TransferUtility transferUtility = TransferUtility.builder().s3Client(s3Client).context(getActivity()).build();

        // String key for the file to download
        String key = "ExampleVideo.mp4";

        // Location to download files from S3
        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + key);

        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Bucket that contains the file to download
        String bucket = "My_Bucket";

        // Create a tranfer observer to download
        TransferObserver observer = transferUtility.download(bucket,key,file);

        // Attach listener to the download to listen for changes in the the download process
        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {

            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

            }

            @Override
            public void onError(int id, Exception ex) {

            }
        }); 

感谢您的帮助

最佳答案

适用于 Android 的 AWS 开发工具包 2.6.6 版本包含修复进度更新问题的修复程序。升级到此版本应该可以缓解该问题。
除此之外,您还可以调整 AmazonS3ClientnotificationThreshold API 来更改通知进度的时间间隔。

关于android - AWS s3 TransferListener 未在 Android 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48210469/

相关文章:

带有 AsyncLayoutInflater 的 Android Fragment onCreateView

java - 如何在java android中给出 'for loop'的时间间隔

java - Android sleep 时的互联网连接问题

amazon-web-services - X 任务失败后取消 AWS CDK 部署

python - 如何使用 Python 中的 Mosquitto 将文件发布到 AWS-IoT

amazon-web-services - 使用 cloudformation 时,Kinesis Firehose Delivery Stream 无法承担角色

android - Kotlin Firebase Realtime数据库事务错误

node.js - 从 Amazon S3 存储中删除图像

python - 在 AWS Lambda 中使用 numpy

hadoop - 使用 s3distcp 将文件从 amazon s3 复制到 hdfs 失败