使用AWS S3签名的url获取上传进度

Yos*_*shi 5 upload amazon-s3 amazon-web-services

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

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

Mur*_*ala -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();
    }
Run Code Online (Sandbox Code Playgroud)

也适用于分段上传。