I am trying to upload video file of 10-11 MB on azure server.It working fine on 4g or wifi network.But it is throwing below error on 3g or 2g network.
java.io.IOException: The client could not finish the operation within specified maximum execution timeout. Please see the cause for further information..
Below is my code :--
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) {
}
I am using com.microsoft.azure.android:azure-storage-android:2.0.0@aar
library.
Any help would be appreciated.
BlobRequestOptions
has a property called singleBlobPutThresholdInBytes
which determines whether or not a blob should be split in blocks while uploading. If you don't specify a value, the default value is 32 MB
i.e. any file that is being uploaded, if its size is less than 32 MB, then it would be uploaded without splitting into blocks.
Since you mentioned that the file size is 10-11 MB
which is smaller than this 32 MB limit, the SDK is trying to upload the file in one shot. Because of slow Internet connection, the request is timing out trying to upload this huge data.
What you could do is change it's value to a smaller size. The value should be more than 1 MB but less than 32 MB. Then your file will be uploaded in chunks and you should not get this timeout error (hopefully). To set the value, you can add following line of code:
uploadOptions.setSingleBlobPutThresholdInBytes(1024*1024);//1MB