【问题标题】:Get upload progress with AWS S3 signed url使用 AWS S3 签名的 url 获取上传进度
【发布时间】:2016-10-21 05:24:20
【问题描述】:

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

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

【问题讨论】:

  • 从客户端执行上传的过程是什么?
  • 这个过程只是正常的文件上传到服务器生成的签名url。据我所知,我可以从进行上传的浏览器中获得进度。但我需要为我的管理面板从服务器获取它。

标签: amazon-web-services amazon-s3 upload


【解决方案1】:

相同的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();
    }

也适用于多部分上传。

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2020-10-23
    • 2023-03-09
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多