在慢速网络中上传视频文件的天蓝色服务器抛出Timeoutexception

时间:2017-12-13 12:53:52

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

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

  

java.io.IOException:客户端无法完成其中的操作   指定的最大执行超时。请进一步了解原因   信息..

以下是我的代码: -

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库。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

BlobRequestOptions有一个名为singleBlobPutThresholdInBytes的属性,用于确定在上传时是否应在块中拆分blob。如果您没有指定值,则默认值为32 MB,即正在上传的任何文件,如果其大小小于32 MB,则将上载而不会拆分为块。

由于您提到文件大小为10-11 MB且小于此32 MB限制,因此SDK会尝试一次性上传文件。由于互联网连接速度缓慢,请求会尝试上传这些庞大的数据。

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

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