Alica Corwin
Alica Corwin

Reputation: 86

Unable to upload blob(video) to azure blob storage

Hello everyone i am trying to upload the captured video from the camera of an android device to azure blob storage but i am unable to do it here is my code.

        CloudStorageAccount storageAccount =   CloudStorageAccount.parse(storageConnectionString);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        CloudBlobContainer container = blobClient.getContainerReference("clipshotitems");

      final String filepath=       Environment.getExternalStorageDirectory().getAbsolutePath()+"/video.mp4";
        // Create the container if it does not exist.
     //   container.createIfNotExists();
       BlobContainerPermissions containerPermissions=new BlobContainerPermissions();
        containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
        container.uploadPermissions(containerPermissions);
       CloudBlockBlob blob = container.getBlockBlobReference("miniclipId");


        FileInputStream file= new FileInputStream(filepath);
        blob.upload(file,source.length());
        p1.hide();
      `

Upvotes: 0

Views: 268

Answers (1)

Oscar Sanchez
Oscar Sanchez

Reputation: 26

This is my solution:

public class AzureManager {
    public static final String storageConnectionString = "DefaultEndpointsProtocol=https;" 
    + "AccountName=my_account;" 
    + "AccountKey=my_account_key;"
    + "EndpointSuffix=core.windows.net";

    private CloudBlobClient blobClient;
    private static CloudBlobContainer container;

    public AzureManager() {
        try {
            // Parse the connection string and create a Blob service client
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();

            // Retrieve a reference to a container
            container = blobClient.getContainerReference("my_container_name");
            container.createIfNotExists();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Then the method:

public static String uploadVideo(InputStream videoStream, String videoName) {
    try {
        // Generate a random name if videoName is null or empty
        if (videoName == null || videoName.isEmpty()) {
            videoName = UUID.randomUUID().toString();
        }

        // Create a blob in the container
        CloudBlockBlob blob = container.getBlockBlobReference(videoName);

        // Upload the video stream to the blob
        blob.upload(videoStream, -1);

        // Return the video name (or blob URI) for reference
        return videoName;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

And this is the usage in Activities:

AzureManager azureManager = new AzureManager();
String outputPath = new File(getCacheDir(), localFileUriString).getAbsolutePath();
String videoName = azureManager.uploadVideo(new FileInputStream(outputPath), "my_video.mp4");
System.out.println("Azure Blob URL video uploaded: " + videoName);

Upvotes: 0

Related Questions