Search code examples
javaspring-bootazure-storage

How to upload a file to Azure storage?


My question is simple. How to upload a file to Azure storage?

Thank you.


Solution

  • The below code is my test to upload a file to root directory, it uses Azure Storage SDK v8, if you want to use the latest version sdk, you could use this Azure File SDK:Azure File client library for Java. It will show you how to use the SDK and it also includes many samples, and this is the upload sample.

    final String storageConnectionString =
                "DefaultEndpointsProtocol=http;" +
                        "AccountName=your storage account name;" +
                        "AccountKey=your storage connection key";
        try {
            // Use the CloudStorageAccount object to connect to your storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
    
            // Create the file client.
            CloudFileClient fileClient = storageAccount.createCloudFileClient();
    
            // Get a reference to the file share
            CloudFileShare share = fileClient.getShareReference("windows");
    
            //Get a reference to the root directory for the share.
            CloudFileDirectory rootDir = share.getRootDirectoryReference();
    
            // Define the path to a local file.
            final String filePath = "C:\\Users\\georgec\\Documents\\test.json";
    
            //Get a reference to the file you want to upload
            CloudFile cloudFile = rootDir.getFileReference("test.json");
            cloudFile.uploadFromFile(filePath);
        }