amazon-web-services - 使用 AWS S3 签名 url 获取上传进度

标签 amazon-web-services amazon-s3 upload

我正在制作一个应用程序,让用户使用 AWS S3 将他们的视频上传到我们的终端。我使用服务器生成签名的 url 并将其返回给客户端(网络浏览器),然后客户端使用该 url 上传到我们的后端。它工作得很好,但我有一个小问题,我们无法跟踪从浏览器开始的文件上传进度。

那么有什么方法可以从我们的服务器获取上传进度?

最佳答案

相同的 Java 片段

public void uploadtoS3(File file) throws AmazonServiceException, AmazonClientException, InterruptedException {
        logger.debug("Uploading the File => S3");
        ProfileCredentialsProvider credentialProviderChain = new ProfileCredentialsProvider();
        TransferManager tx = new TransferManager(credentialProviderChain.getCredentials());
        final Upload upload = tx.upload(env.getProperty("amazon.s3.media.bucket"), file.getName(), file);

        // You can poll your transfer's status to check its progress
        if (upload.isDone() == false) {
            logger.debug("Transfer: " + upload.getDescription());
            logger.debug("State: " + upload.getState());
            logger.debug("Progress: " + upload.getProgress().getBytesTransferred());
        }

        // Transfers also allow you to set a <code>ProgressListener</code> to
        // receive
        // asynchronous notifications about your transfer's progress.

        upload.addProgressListener(new ProgressListener() {
            // This method is called periodically as your transfer progresses
            public void progressChanged(ProgressEvent progressEvent) {
                logger.debug(upload.getProgress().getPercentTransferred() + "%");

                if (progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                    logger.debug("Upload complete!!!");
                }
            }
        });

        // Or you can block the current thread and wait for your transfer to
        // to complete. If the transfer fails, this method will throw an
        // AmazonClientException or AmazonServiceException detailing the reason.
        upload.waitForCompletion();

        // After the upload is complete, call shutdownNow to release the
        // resources.
        tx.shutdownNow();
    }

也适用于 MultiPart 上传。

关于amazon-web-services - 使用 AWS S3 签名 url 获取上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914324/

相关文章:

php - 使用文件系统从 AWS S3 下载的 Laravel 文件已损坏

node.js - 使用 multer 将多个图像上传到 AWS S3

php - 上传文件并以单一形式提交文本是否可能?

amazon-web-services - 如果存储桶中 5 或 10 分钟没有收到数据,如何为 s3 存储桶设置云监视警报?

iphone - 如何使用Three20框架上传多个图像并处理JSON响应?

django - 在 Django 中将上传的 InMemoryUploadedFile 作为临时文件保存到磁盘

amazon-web-services - 我是否需要定义巨大的 VPC 子网才能并行运行许多 Lambda 函数?

ssl - 如何在CloudFormation模板中将预先上传的SSL证书附加到ELB?

amazon-s3 - 尝试使用 s3cmd 在 Amazon S3 文件上设置元数据

Java Lambda 中的 java.lang.NoClassDefFoundError