svt_pil
svt_pil

Reputation: 76

how to upload file in java for given SAS URI

Sample C# code:

 static void UploadFile(string sasUrl, string filepath)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("x-ms-version", Version);
            client.DefaultRequestHeaders.Add("x-ms-client-request-id", SessionGuid);

            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?><BlockList>");

            foreach (byte[] chunk in GetFileChunks(filepath))
            {
                var blockid = GetHash(chunk);
                HttpRequestMessage chunkMessage = new HttpRequestMessage()
                {
                    Method = HttpMethod.Put,
                    RequestUri = new Uri(sasUrl + "&timeout=90&comp=block&blockid=" + WebUtility.UrlEncode(blockid)),
                    Content = new ByteArrayContent(chunk)
                };
                chunkMessage.Headers.Add("x-ms-blob-type", "BlockBlob");
                chunkMessage.Content.Headers.Add("MD5-Content", blockid);

                TimeAction("Uploading chunk " + blockid + " took {0} ms", () =>
                {
                    var response = client.SendAsync(chunkMessage).Result;
                });
                sb.Append("<Latest>");
                sb.Append(blockid);
                sb.Append("</Latest>");
            }
            sb.Append("</BlockList>");

            Trace.WriteLine(sb.ToString());

            HttpRequestMessage commitMessage = new HttpRequestMessage()
            {
                Method = HttpMethod.Put,
                RequestUri = new Uri(sasUrl + "&timeout=90&comp=blocklist"),
                Content = new StringContent(sb.ToString())
            };
            TimeAction("Commiting the blocks took {0} ms", () =>
            {
                var commit = client.SendAsync(commitMessage).Result;
            });
        }
    }

I am stuck at the point where I've to upload a file. Also want to know what the reason is to commit in given code?

my progress so far is :

public static void uploadFile(String sasUrl , String filepath , String sessionGuid)
{
    File file = new File(filepath);
    FileInputStream fileInputStream=null;
    Response reply = new Response();
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(sasUrl);
    request.setHeader("x-ms-version", "2013-08-15");
    request.setHeader("x-ms-client-request-id", sessionGuid);
    StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?><BlockList>");


}
}

Note: I cannot run the code multiple times as I cannot spam the server. Any suggestions will be appreciated Referring to : https://msdn.microsoft.com/en-us/library/windows/hardware/dn800660(v=vs.85).aspx

Upvotes: 1

Views: 1137

Answers (1)

Peter Pan
Peter Pan

Reputation: 24148

According to the reference code in C#, it seems to be using the REST API Put Block List to upload a file as a block blob.

So you can refer to the REST API reference without refering to the C# sample to use httpclient to construct the request for uploading.

However, the simple way is using Azure Storage SDK for Java. To upload a file, you just need to use the class CloudBlockBlob to upload a file with the function upload(InputStream sourceStream, long length), please refer to the tutorial https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-blob-storage/#upload-a-blob-into-a-container.

The sas url seems like https://myaccount.blob.core.windows.net/mycontainer/myblob?comp=blocklist&...

Here is the code as example.

URL sasUrl = new URL("<sas-url>");
try
{.
    CloudBlockBlob blob = new CloudBlockBlob(sasUrl)
    File source = new File(filePath);
    blob.upload(new FileInputStream(source), source.length());
}
catch (Exception e)
{
    // Output the stack trace.
    e.printStackTrace();
}

As reference, please see the javadocs for Azure Java Storage SDK.

Upvotes: 2

Related Questions