java - 在 azure 服务器中缓慢网络上传视频文件时抛出超时异常

标签 java android azure azure-storage azure-mobile-services

我正在尝试在 azure 服务器上上传 10-11 MB 的视频文件。它在 4g 或 wifi 网络上工作正常。但在 3g 或 2g 网络上抛出以下错误。

java.io.IOException: The client could not finish the operation within specified maximum execution timeout. Please see the cause for further information..

下面是我的代码:--

try {
            // Setup the cloud storage account.
            CloudStorageAccount account = CloudStorageAccount
                    .parse("somestring");

            // Create a blob service client
            CloudBlobClient blobClient = account.createCloudBlobClient();
            BlobRequestOptions uploadOptions = new BlobRequestOptions();
            uploadOptions.setMaximumExecutionTimeInMs(300000);
            uploadOptions.setTimeoutIntervalInMs(100000);
            RetryPolicy retryPolicy=new RetryExponentialRetry(
                    60000 /* minBackoff in milliseconds */,
                    30000 /* delatBackoff in milliseconds */,
                    180000 /* maxBackoff in milliseconds */,
                    3 /* maxAttempts */);
            uploadOptions.setRetryPolicyFactory(retryPolicy);
            // Get a reference to a container
            // The container name must be lower case
            // Append a random UUID to the end of the container name so that
            // this sample can be run more than once in quick succession.
            CloudBlobContainer container = blobClient.getContainerReference("videos");

            // Create the container if it does not exist
            container.createIfNotExists();

            // Make the container public
            // Create a permissions object
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            OperationContext opContext = new OperationContext();
            StorageEventMultiCaster storageEventMultiCaster = new StorageEventMultiCaster<>();

            // Include public access in the permissions object
            containerPermissions
                    .setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
            storageEventMultiCaster.addListener(new StorageEvent<SendingRequestEvent>() {

                @Override
                public void eventOccurred(SendingRequestEvent eventArg) {

                    // Do custom monitoring here...
                }
            });
            opContext.setSendingRequestEventHandler(storageEventMultiCaster);
            // Set the permissions on the container
            container.uploadPermissions(containerPermissions);
            CloudBlockBlob blob = container.getBlockBlobReference(file.getName());

            byte[] buffer = IOUtils.toByteArray(new FileInputStream(file));
            ByteArrayInputStream  inputStream = new ByteArrayInputStream(buffer);
            blob.upload(inputStream, inputStream.available(),null,uploadOptions,opContext);
            inputStream.close();
            String url = blob.getUri().toString();
                    } catch (Throwable t) {

        }

我正在使用 com.microsoft.azure.android:azure-storage-android:2.0.0@aar 库。

如有任何帮助,我们将不胜感激。

最佳答案

BlobRequestOptions有一个名为 singleBlobPutThresholdInBytes 的属性,用于确定上传时是否应将 Blob 分割成 block 。如果您未指定值,则默认值为 32 MB,即任何正在上传的文件,如果其大小小于 32 MB,则将上传而不分割为 block 。

由于您提到文件大小为 10-11 MB,小于 32 MB 限制,因此 SDK 正在尝试一次性上传文件。由于互联网连接速度较慢,尝试上传如此庞大的数据的请求超时。

您可以做的是将其值更改为较小的大小。该值应大于 1 MB 但小于 32 MB。然后您的文件将分块上传,您不应该收到此超时错误(希望如此)。要设置该值,您可以添加以下代码行:

uploadOptions.setSingleBlobPutThresholdInBytes(1024*1024);//1MB

关于java - 在 azure 服务器中缓慢网络上传视频文件时抛出超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793540/

相关文章:

java - 通过 SockJS 连接 Stomp 并订阅消息后仅接收 header

java - 没有父 json 数组的重复 json 对象的 Json 响应

java - 如何检查堆栈的子堆栈?

java - Android setText 不适用于 TextView

azure - 运行 Get-AzManagementGroup PowerShell 命令时出错

azure - 如何保护 Azure 中的目标目录免遭部署

java - 从同步方法调用同步方法的同步成本是多少?

android - Acartengine BarChart使用BasicStroke添加实线轮廓

android - 使用计算机名称的套接字连接有问题吗?

wcf - 如何使用 ACS(Azure) 保护 wcf Web 服务