I have the code from below that works fine. My service and the storage are in the same environment in agent. Is there a way to write the credentials without having to provide a key, hence they are in the same environment in agent ?
StorageSharedKeyCredential credential =
new StorageSharedKeyCredential(accountName, **accountKey**);
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
Is there a way to write the credentials without having to provide a key?
To avoid key credential, I agree with @NotFound that you can use Tokencredential
with the DefaultAzureCredential method.
You need to assign RBAC roles to your storage account for accessing blob storage through identity, The roles are:-
Go to portal -> storage accounts -> Access Control (IAM) ->Add -> Add role assignments -> storage-blob-contributor or storage-blob-owner role to the storage account.
Portal:
I tried with sample code to upload a file from the local path to azure blob storage using identity it uploaded successfully.
Code:
import com.azure.storage.blob.*;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.*;
public class App
{
public static void main( String[] args )
{
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage account name>.blob.core.windows.net/")
.credential(credential)
.buildClient();
String containerName = "test";
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
String localPath = "<your local path >";
BlobClient blobClient = containerClient.getBlobClient("file.json");
System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());
blobClient.uploadFromFile(localPath);
System.out.println("Uploaded!!!");
}
}
Console:
Portal: