Download a file using Azure Storage using SAS (shared signature)

I know that there is a library for downloading a file using Azure storage. I have to refer to the same.

But they do not provide information on how to use SAS with this. I have an account name and sas url to access and upload the file there. But I do not know how to use this to upload a file.

If I use the above library, it shows me an invalid storage connection string, because I do not pass the key in it (which is not required for sas). So I am confused how can I upload a file.

I mean this documentation also for downloading files using sas. but not getting the proper steps for this. They made a demo for their Windows application. I want to have this in Android using SAS.

Refresh

I tried with the code below with a link to a console application created by Azure to test 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 = "https://myStorageAccountName.blob.core.windows.net/image/" + 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"; //Console.WriteLine(); } 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) { e.printStackTrace(); response = e.getMessage(); } catch (IOException e) { e.printStackTrace(); response = e.getMessage(); } catch (URISyntaxException e) { e.printStackTrace(); response = e.getMessage(); } catch (Exception e){ e.printStackTrace(); response = e.getMessage(); } 

Now when I upload the text only it tells me the error below

The server failed to authenticate the request. Verify that the authorization header value is configured correctly, including the signature.

Now my requirement is to upload an image file. Therefore, when I uncomment the code for loading the image file, it does not produce any error, but does not even load the image file.

+8
android azure azure-storage azure-storage-blobs azure-mobile-services
source share
3 answers

@kumar kundal The mechanism you explain is absolutely right. Below is a more detailed answer on uploading a profile image to Azure Server.

First, create a SAS URL to load the image (or any file) into the memory 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>() { @Override public void onSuccess(JsonElement result) { // here you will get SAS url from server sasUrl = result; // You need to parse it as per your response } @Override public void onFailure(Throwable t) { } }); 

You now have sasURL. This will be something like the line below:

 sv=2015-04-05&ss=bf&srt=s&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=F%6GRVAZ5Cdj2Pw4tgU7IlSTkWgn7bUkkAg8P6HESXwmf%4B 

Now you need to increment the sas url using the download url. Below is the code in which I updated the SAS URL with my download 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 = inputStream.read(); while (next != -1) { blobOutputStream.write(next); next = inputStream.read(); } blobOutputStream.close(); // YOUR IMAGE/FILE GET UPLOADED HERE // IF YOU HAVE FOLLOW DOCUMENT, YOU WILL RECEIVE IMAGE/FILE URL HERE } catch (StorageException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } 

I hope this information helps you download the file using the blob repository. Please let me know if you have any doubts besides this. I can help with that.

+2
source share

You can refer to this article https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/ to find out how to correctly configure the connection string when accessing resources in the storage account via Shared Signature (SAS).

+1
source share

Upload a picture to the BLOB storage . I got it after searching for a few hours. See: -

Uploading images is a multi-step process:

First, you take a snapshot and insert the TodoItem string into the SQL database containing the new metadata fields used by Azure Storage. A new mobile service insertion script is requesting Azure Storage for Shared Access Signature (SAS) . This script returns the SAS and URI for blob for the client. The client uploads the photo using the SAS URI and blob.

So what is SAS ?

It is incorrect to store the credentials required to upload data to the Azure Storage service inside your client application. Instead, you save these credentials in your mobile service and use them to create a Shared Access Signature (SAS) that provides permission to upload a new image. SAS , an account after 5 minutes, is safely returned by Mobile Services to the client application. The application then uses these temporary credentials to upload the image.

for further inquiries and detailed analysis. Visit the official documentation https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-upload-data-blob-storage/

0
source share

All Articles