Search code examples
javacloudazure-blob-storageazure-file-share

Need to upload the file from local to Azure blob File Share folder using Java code


I am trying to upload the file from my local path to File Share folder in the Azure Storage blob by using below Java code. I am getting the below error. Kindly help me to resolve this issue or any code snippet to upload in the file share folder in azure blob.

String connectionString = "";
String shareName = "";
String filePath = "<local file path>";
String fileName = "";

// Create a ShareClient object
ShareClient shareClient = new ShareClientBuilder().connectionString(connectionString).shareName(shareName).buildClient();

try {
    // Open the file as a stream
    File file = new File(filePath);
    FileInputStream fis = new FileInputStream(file);

    // Upload the file to the share
    ShareFileClient fileClient = shareClient.getDirectoryClient("").getFileClient(fileName);
    fileClient.create(fis.available());
    fileClient.upload(fis, fis.available());

    // Close the file stream
    fis.close();

    System.out.println("File uploaded successfully.");
} catch (IOException e) {
    e.printStackTrace();
}

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/azure/core/client/traits/HttpTrait
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at utilities.azureBlob3.main(azureBlob3.java:22)
Caused by: java.lang.ClassNotFoundException: com.azure.core.client.traits.HttpTrait
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 10 more

Solution

  • Need to upload the file from local to Azure blob File Share folder using Java code

    You can use the below code to upload from the local path to Azure file storage using Azure Java SDK.

    Code:

    import com.azure.storage.file.share.ShareFileClient;
    import com.azure.storage.file.share.ShareFileClientBuilder; 
    
        public static void main(String[] args) {
            String connectionString = "your-connection-string";
            String shareName = "fileshare1";
            String directoryName = "directory1";
            String fileName = "sample.csv";
            String filePath = "C:\\Users\\xxxx\\xxxx\\day.csv";
        
            // Create a ShareClient object
                   // Create a ShareFileClient object
            ShareFileClient fileClient = new ShareFileClientBuilder()
                    .connectionString(connectionString)
                    .shareName(shareName)
                    .resourcePath(directoryName + "/" + fileName)
                    .buildFileClient();
        fileClient.uploadFromFile(filePath);
        
        System.out.println("File uploaded successfully.");
        }
    

    Dependency:

    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-file-share</artifactId>
      <version>12.14.0</version>
    </dependency> 
    

    Output:

     File uploaded successfully.
    

    enter image description here

    Portal:

    enter image description here

    Reference:

    Azure File Share client library for Java | Microsoft Learn