0

I want to upload file directly from internet to Azure blob storage in "mycontainer" , I dont want to download file first in local the upload. I want to do this using java code, can anyone please help me with sample code.

1
  • Azure Storage allows creation of blobs by copying the contents of a URL. Please take a look at SDK documentation for Copy Blobs here: dl.windowsazure.com/storage/javadoc. Commented Jul 28, 2017 at 8:19

2 Answers 2

1

Based on my understanding, I think you want to directly upload a file from internet url to Azure Blob Storage. You can use the method CloudBlob.startCopy(URI source) to implement your needs.

Here is my sample code.

String connectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", ACCOUNT_NAME, ACCOUNT_KEY);
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient client = account.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference("mycontainer");
CloudBlockBlob blob = container.getBlockBlobReference("bing.txt");

String uri = "http://www.bing.com";
blob.startCopy(new URI(uri));

Hope it helps.

Sign up to request clarification or add additional context in comments.

5 Comments

@GauravMantri Thanks for your help. I had been updated my reply.
@GauravMantri can please share a small sample code to use copyBlob methods
In above code I am getting an error that startCopy () is not a method of CloudBlockBlob.. I also tried using CloudBlob in place of CloudBlockBlob but not working
error -- The method startCopy(URL) is undefined for the type CloudBlockBlob
@anshul What version of Azure Storage SDK for Java you used?
0

You have to add this maven dependency in your project.

 <dependency>
 <groupId>com.azure</groupId>
 <artifactId>azure-storage-blob</artifactId>
 <version>12.0.0</version>
 </dependency>

And add the following imports.

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.specialized.BlockBlobClient;

You can use the below code to upload a new file to the container.

BlobClient blobClient = null;

try {
  String mConnectionstring = "DefaultEndpointsProtocol=https;AccountName=
         mystorageaccount;AccountKey
         =6xjXi/dA==;EndpointSuffix=core.windows.net";

// Create a BlobServiceClient object which will be used to create a container
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(mConnectionstring)
.buildClient();

// Create a unique name for the container
String containerName = "anycontainername";

// Create the container and return
// a container client object
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

// Get a reference to a blob
blobClient = containerClient.getBlobClient(file.getOriginalFilename());

// Note: We are creating BlockBlob instance.
BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient();

blockBlobClient.upload(file.getInputStream(), file.getSize());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.