0

How to upload a file to Azure Blob storage using Azure Java SDK and also print the timestamp after uploading the file?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pratik Dey
  • 11
  • 3
  • 1
    Did you have a look at the documentation? https://learn.microsoft.com/en-us/java/api/overview/azure/storage-blob-readme?view=azure-java-stable – Julian Hüppauff Nov 09 '22 at 09:46

1 Answers1

0

I tried in my environment and got successfully uploaded file with timestamp in azure blob storage:

Code:

package  com.blobs.quickstart;
import  org.apache.commons.lang3.time.StopWatch;
import  com.azure.storage.blob.*;
import  com.azure.storage.blob.BlobServiceClient;

public  class  App
{
public  static  void  main( String[] args )
{
String  connectStr = "< Connection string >";
BlobServiceClient  blobServiceClient = new  BlobServiceClientBuilder().connectionString(connectStr).buildClient();
String  containerName = "test";
BlobContainerClient  containerClient = blobServiceClient.getBlobContainerClient(containerName);
String  localPath = "path of your file";
BlobClient  blobClient = containerClient.getBlobClient("barcode.docx");
StopWatch  watch = new  StopWatch();
watch.start();
System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());
blobClient.uploadFromFile(localPath);
watch.stop();
System.out.println("Time Elapsed: " + watch.getTime());
}
}

Console:

enter image description here

Portal:

enter image description here

Reference: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-java?tabs=powershell%2Cmanaged-identity%2Croles-azure-portal%2Csign-in-azure-cli

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • do we need to install something because for BlobServiceClient Iam getting error – chopss Nov 21 '22 at 06:08
  • Hi @chopss what error you are getting? also you need to include dependency azure storage blob in pom.xml – Venkatesan Nov 21 '22 at 06:10
  • You need to use POM.xml file because pom.xml is Maven configuration file and controls the build process for the project. web.xml is web application configuration file. – Venkatesan Nov 21 '22 at 09:23
  • Exception in thread "main" java.lang.NoSuchMethodError: com.azure.core.util.CoreUtils.getDefaultTimeoutFromEnvironment(Lcom/azure/core/util/Configuration;Ljava/lang/String;Ljava/time/Duration;Lcom/azure/core/util/logging/ClientLogger;)Ljava/time/Duration; at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.(NettyAsyncHttpClientBuilder.java:70) getting this error....my main function is also loading when I put the below line it shows error there .....BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient(); – chopss Nov 21 '22 at 10:46
  • 1
    Could you raise in new question i will answer there. – Venkatesan Nov 21 '22 at 10:47
  • 1
    https://stackoverflow.com/questions/74518525/problem-while-uploading-from-local-to-blob-storage....added here plz..check ...i have wriiten main fuction in correct format,,,,but something wrong with connection string line – chopss Nov 21 '22 at 12:00