java - Azure storage block blob upload from android example -
i using following code android application upload blob azure blob storage. note: sasurl
parameter below signed url acquired web service :
// upload file azure blob storage private static boolean upload(string sasurl, string filepath, string mimetype) { try { // file data file file = new file(filepath); if (!file.exists()) { return false; } string absolutefilepath = file.getabsolutepath(); fileinputstream fis = new fileinputstream(absolutefilepath); int bytesread = 0; bytearrayoutputstream bos = new bytearrayoutputstream(); byte[] b = new byte[1024]; while ((bytesread = fis.read(b)) != -1) { bos.write(b, 0, bytesread); } fis.close(); byte[] bytes = bos.tobytearray(); // post our image data (byte array) server url url = new url(sasurl.replace("\"", "")); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setdooutput(true); urlconnection.setconnecttimeout(15000); urlconnection.setreadtimeout(15000); urlconnection.setrequestmethod("put"); urlconnection.addrequestproperty("content-type", mimetype); urlconnection.setrequestproperty("content-length", "" + bytes.length); urlconnection.setrequestproperty("x-ms-blob-type", "blockblob"); // write file data server dataoutputstream wr = new dataoutputstream(urlconnection.getoutputstream()); wr.write(bytes); wr.flush(); wr.close(); int response = urlconnection.getresponsecode(); if (response == 201 && urlconnection.getresponsemessage().equals("created")) { return true; } } catch (exception e) { e.printstacktrace(); } return false; }
the code working fine small blobs when blob reaches size depending on phone testing with, start out of memory exceptions. split blobs , upload them in blocks. however, examples find on web c# based , using storage client library. looking java/android example uploads blob in blocks using azure storage rest api.
there azure storage android library published here. basic blob storage example in samples folder. method you’d use uploadfromfile in blob class. will, default attempt put blob in single put if size less 64mb , otherwise send blob in 4mb blocks. if you’d reduce 64mb limit, can set singleblobputthresholdinbytes property on blobrequestoptions object of either cloudblobclient (which affect requests) or passed uploadfromfile method (to affect request). storage library includes many convenient features such automated retries , maximum execution timeout across block put requests configurable.
if you’d still use more manual approach, putblock , put block list api references here , provide generic, cross-language documentation. these have nice wrappers in cloudblockblob class of azure storage android library called uploadblock , commitblocklist may save lot of time in manual request construction , can provide of aforementioned conveniences.
Comments
Post a Comment