javaazureazure-storage

How to upload a file from java to Azure?


I am trying upload a file to Azure from a Java function but i dont get it, i am not sure if my local directories or another variables such as "shareName" or "dirName" or "fileName" i was declared incorrectly

i was a follow the next instructions in the apartated Upload a File: Azure documentation.

At runs my code i was receive the next console output:

DEBUG reactor.netty.resources.PooledConnectionProvider - [id:c341493a, L:/192.168.1.68:56172 - R:wiebs4fefe66q0knduvyeie.file.core.windows.net/52.239.169.232:443] Channel cleaned, now: 0 active connections, 1 inactive connections and 0 pending acquire requests. uploadFile exception: Status code 404, "?<?xml version="1.0" encoding="utf-8"?><Error><Code>ParentNotFound</Code><Message>The specified parent path does not exist. RequestId:f70e845e-401a-0068-66fd-9d44f5000000 Time:2025-03-26T03:17:18.3651535Z</Message></Error>"

I was try use this code extracted from the Azure oficial web site in the Upload a File aparted

the uploadFile metod is the next:

public static Boolean uploadFile(String connectStr, String shareName,
                                    String dirName, String fileName)
{
    try
    {
        ShareDirectoryClient dirClient = new ShareFileClientBuilder()
             .connectionString(connectStr).shareName(shareName)
             .resourcePath(dirName)
             .buildDirectoryClient();

        ShareFileClient fileClient = dirClient.getFileClient(fileName);
        fileClient.create(1024);
        fileClient.uploadFromFile(fileName);
        return true;
    }
    catch (Exception e)
    {
        System.out.println("uploadFile exception: " + e.getMessage());
        return false;
    }
}

and i call the uploadFile method that next way:

String connectionString = "DefaultEndpointsProtocol=https;" +
                "AccountName=wisdfdsfsie;" +
                "AccountKey=Y9vsdfsdfsdfsdffsdfGdQZK8Lz4DAIwi9A==";

        String shareName = "mis-archivos";
        String dirName = "hybris/master/mabe";
        String fileName = "temp/GR_IMPEXP_25-03-2025.csv";

        boolean result = uploadFile(connectionString, shareName, dirName, fileName);

        if (result) {
            System.out.println("Upload Correct.");
        } else {
            System.out.println("Upload Failed....");
        }

i am still not very clear the form of use the variables as shareName, dirName and fileName. I should set a special configuration into my Microsoft Azure Storage Explorer?

in the string fileName i should put the file to transfer to my Azure Storage?

I am should be create a File Shared Resource in the Microsoft Azure Storage Explorer? how to it?

in the dirName i should set the directory trees set into the file shared resources?

I am a little bit confused in how functions the uploadFiles method.


Solution

  • If you look at the error message, you will notice that you are getting the error about the directory not exist. Before you can upload a file in a directory, the directory must be present in that share.

    Please see the code below:

    public static Boolean uploadFile(String connectStr, String shareName,
                                        String dirName, String fileName)
    {
        try
        {
            ShareDirectoryClient dirClient = new ShareFileClientBuilder()
                 .connectionString(connectStr).shareName(shareName)
                 .resourcePath(dirName)
                 .buildDirectoryClient();
    
            //create directory if it does not exist
            dirClient.createIfNotExists();
    
            ShareFileClient fileClient = dirClient.getFileClient(fileName);
            fileClient.create(1024);
            fileClient.uploadFromFile(fileName);
            return true;
        }
        catch (Exception e)
        {
            System.out.println("uploadFile exception: " + e.getMessage());
            return false;
        }
    }