0

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
James Z
  • 12,209
  • 10
  • 24
  • 44
oliver
  • 75
  • 6
  • are you using Azure SDK in your project? The error message looks like a dependent jar file is missing. – Raju Rudru Aug 01 '23 at 09:05
  • Hi @RajuRudru I am using the below dependency. Now i am getting "Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" Error. Kindly suggest me to resolve this. com.azure azure-storage-file-share 12.19.0 – oliver Aug 01 '23 at 09:35

1 Answers1

0

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

Venkatesan
  • 3,748
  • 1
  • 3
  • 15