0

I am generating a url for a public azure blob by

    String connectStr = "connection string";
    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

    BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient("container name");

    BlobClient blobClient=containerClient.getBlobClient("blob name");

    String newstring = blobClient.getBlobUrl();
    System.out.println(newstring);

but the problem is that this generate a url only for public blob how can I get the url of private blob.

1 Answer 1

3

Check this Java Storage V12 change, there is a description about sas.

It was not discoverable how to do something as fundamental as create a SAS token because there was no generateSAS method, and figuring how to attach a SAS to a URL was yet another problem.

So for now if you want to use v12 sdk, have to say no way to implement it. If you accept other version sdk, you could refer to the below code, it uses a V8 sdk.

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.*;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.*;

public class App 
{
    public static void main( String[] args ) throws URISyntaxException, InvalidKeyException, StorageException {

        String storageConnectionString ="connection string";

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference("test");

        CloudBlockBlob blob = container.getBlockBlobReference("test.txt");

        SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

        // Create a UTC Gregorian calendar value.
        GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        // Use the start time delta one hour as the end time for the shared
        // access signature.
        calendar.add(Calendar.HOUR, 10);
        sasPolicy.setSharedAccessExpiryTime(calendar.getTime());

        sasPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE,
                SharedAccessBlobPermissions.LIST));
        String sas = blob.generateSharedAccessSignature(sasPolicy,null);

        String sasurl=blob.getUri()+"?"+sas;

        System.out.println(sasurl);


    }
}

enter image description here

My dependency:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure-storage</artifactId>
      <version>8.4.0</version>
    </dependency>

  </dependencies>
Sign up to request clarification or add additional context in comments.

1 Comment

which libraries are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.