0
votes

I am implementing an Android app where I need to upload images to a blob container using a container SAS.

Currently using the full endpoint credentials as a connection string, I can add a retry policy to my CloudBlobClient using the following code:

CloudStorageAccount storageAccount = CloudStorageAccount
        .parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

BlobRequestOptions options = new BlobRequestOptions();
RetryPolicy policy = new RetryExponentialRetry(BACKOFF_DELTA, MAX_RETRIES);
options.setRetryPolicyFactory(policy);
blobClient.setDefaultRequestOptions(options);

CloudBlobContainer container = blobClient.getContainerReference(containername);

Now, as I am switching from the account key to SAS, I don't know how to create a CloudBlobClient using the SAS signature I have for my container. I can create both CloudBlobClient and CloudBlobContainer using the URL with SAS, but the client is created with anonymous credentials and cannot be associated with my container:

CloudBlobClient blobClient = new CloudBlobClient(URI.create(containerurl));
CloudBlobContainer container = new CloudBlobContainer(URI.create(containerurl));

Is there a way to add RetryPolicy directly to the CloudBlobContainer or create an authenticated CloudBlobClient associated with my container class?

1

1 Answers

1
votes

This was tagged with both 'Java' and 'Android', so I'll reply about the Java SDK, Android should be similar.

There are a few options here:

  • All API's that make service calls offer an overload that takes in a BlobRequestOptions object. Settings in this object (including the retry policy) override defaults from the client.
  • When constructed with a URI, CloudBlobContainer and CloudBlob objects internally create a default BlobServiceClient object, which you can access with the getServiceClient() method. You can then set defaults on this.
  • If you want to continue to use a connection string, you can create one with a SAS token, as documented here.
  • You can create a StorageCredentials object using a SAS token, which you can then use to create a CloudStorageAccount object. Note that if you opt for this route, you will need to use one of the CloudStorageAccount constructors that either take an account name or an endpoint, otherwise the SDK will have no way of constructing the URL for your Storage Account.

Hope this helps!