Accessing storage blob using SAS with Java
In order to access your storage blob using SAS with Java, please follow below steps -
-
Configure the SAS access service in your Blob for your storage account. Navigate to Shared access signature setting as shown below -
-
Copy the Blob Service SAS URL. (This is required to access the blob)
-
Access your Blob in your Java code as shown in below snippet -
try { HttpURLConnection httpClient = (HttpURLConnection) new URL(Copy Blob service SAS URL with container).openConnection(); httpClient.setRequestMethod(“PUT”); httpClient.setDoOutput(true); httpClient.setRequestProperty(“x-ms-blob-type”, “BlockBlob”); OutputStreamWriter out = new OutputStreamWriter(httpClient.getOutputStream()); out.write(“This is test”); out.close(); httpClient.getInputStream(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Where –
storageurl = “https://<storage_account_name>.blob.core.windows.net/<container_name>/“+file.name+“sas_content“