gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:09:28 28 4
gpt4 key购买 nike

我正在制作一个应用程序,让用户使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com