iDroid Explorer iDroid Explorer - 1 year ago 129
Android Question

Upload file with Azure Storage using SAS (Shared Access Signature)

I know that there is library available for uploading the file using Azure Storage. I have refer this for same.

But, They have not give information for how to use SAS with that. I have account name, and sas url for access and upload file there. But i don't know how to use that for uploading file.

If i use above mention library it shows me invalide storage connection string because i am not passing the key in it (Which is not required with sas). So i am little confuse that How i can upload file.

I have refer this documentation also for uploading file using sas. but not getting proper steps to do this. They have made demo for their windows app. I want to have that in android with use of sas.

Can anyone help me in this?


I have try with below code with reference to the console app made by Azure to check and access SAS.

try {
//Try performing container operations with the SAS provided.

//Return a reference to the container using the SAS URI.
//CloudBlockBlob blob = new CloudBlockBlob(new StorageUri(new URI(sas)));
String[] str = userId.split(":");
String blobUri = "" + str[1] + "/story/" + storyId + "/image1.jpg" + sas.toString().replaceAll("\"","");
Log.d(TAG,"Result:: blobUrl 1 : "+blobUri);
CloudBlobContainer container = new CloudBlobContainer(new URI(blobUri));
Log.d(TAG,"Result:: blobUrl 2 : "+blobUri);
CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
String filePath = postData.get(0).getUrl().toString();
/*File source = new File(getRealPathFromURI(getApplicationContext(),Uri.parse(filePath))); // File path
blob.upload(new FileInputStream(source), source.length());*/
Log.d(TAG,"Result:: blobUrl 3 : "+blobUri);
//blob.upload(new FileInputStream(source), source.length());
//blob.uploadText("Hello this is testing..."); // Upload text file
Log.d(TAG, "Result:: blobUrl 4 : " + blobUri);
Log.d(TAG, "Write operation succeeded for SAS " + sas);
response = "success";
} catch (StorageException e) {
Log.d(TAG, "Write operation failed for SAS " + sas);
Log.d(TAG, "Additional error information: " + e.getMessage());
response = e.getMessage();
} catch (FileNotFoundException e) {
response = e.getMessage();
} catch (IOException e) {
response = e.getMessage();
} catch (URISyntaxException e) {
response = e.getMessage();
} catch (Exception e){
response = e.getMessage();

Now, when i upload text only it says me below error

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

Now, My requirement is to upload Image file. So when i uncomment code for uploading Image file it is not giving me any error but even not uploading image file. I don't know whats wrong here. Can anyone help me here.


Answer Source

@kumar kundal The mechanism that you have explain is completely right. Below is the more detail answer about uploading profile image to the Azure Server.

First create SAS url to upload Image(or any file) to blob storage:

String sasUrl = "";
// mClient is the MobileServiceClient
ListenableFuture<JsonElement> result = mClient.invokeApi(SOME_URL_CREATED_TO_MAKE_SAS, null, "GET", null);
Futures.addCallback(result, new FutureCallback<JsonElement>() {
        public void onSuccess(JsonElement result) {
            // here you will get SAS url from server
            sasUrl = result; // You need to parse it as per your response

        public void onFailure(Throwable t) {

Now, you have sasURL with you. That will be something like below string:


Now, you need to upend the sas url with your uploading url. See below code in which I have upend the SAS url with my uploading request.

try {
        File source = new File(filePath); // File path
        String extantion = source.getAbsolutePath().substring(source.getAbsolutePath().lastIndexOf("."));
        // create unique number to identify the image/file.
        // you can also specify some name to image/file
        String uniqueID = "image_"+ UUID.randomUUID().toString().replace("-", "")+extantion; 
        String blobUri = MY_URL_TO_UPLOAD_PROFILE_IMAGE + sas.replaceAll("\"","");
        StorageUri storage = new StorageUri(URI.create(blobUri));
        CloudBlobClient blobCLient = new CloudBlobClient(storage);
        CloudBlobContainer container = blobCLient.getContainerReference("");
        CloudBlockBlob blob = container.getBlockBlobReference(uniqueID);
        BlobOutputStream blobOutputStream = blob.openOutputStream();
        byte[] buffer = fileToByteConverter(source);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
        int next =;
        while (next != -1) {
            next =;
    } catch (StorageException e) {
    } catch (FileNotFoundException e) {
    } catch (Exception e){

I hope this information help you lot for uploading the file using blob storage. Please let me know if you have any doubt apart from this. I can help in that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download