来自android示例的Azure存储块blob上传

我使用Android应用程序中的以下代码将blob上传到Azure Blob存储。 注意:下面的sasUrl参数是从我的Web服务获取的签名URL:

  // upload file to azure blob storage private static Boolean upload(String sasUrl, String filePath, String mimeType) { try { // Get the 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) to the 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 to 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; } 

代码适用于小blob但是当blob达到一定的大小时,取决于我正在测试的手机,我开始出现内存exception。 我想拆分blob并将其上传到块中。 但是,我在Web上找到的所有示例都是基于C#的,并且正在使用Storage Client库。 我正在寻找使用Azure Storage Rest API在块中上传blob的Java / Android示例。

此处发布了Azure存储安卓库。 基本blob存储示例位于samples文件夹中。 您可能想要使用的方法是blob类中的uploadFromFile。 默认情况下,如果大小小于64MB,则尝试将blob放在单个put中,否则以4MB块发送blob。 如果您想减少64MB限制,可以在CloudBlobClient的BlobRequestOptions对象上设置singleBlobPutThresholdInBytes属性(这将影响所有请求)或传递给uploadFromFile方法(仅影响该请求)。 存储库包括许多方便的function,例如自动重试和跨块放置请求的最大执行超时,这些请求都是可配置的。

如果您仍想使用更加手动的方法,那么PutBlock和Put Block List API引用就在这里,并提供通用的跨语言文档。 这些在Azure存储安卓库的CloudBlockBlob类中有很好的包装器,称为uploadBlock和commitBlockList,这可以节省您在手动请求构建中的大量时间,并且可以提供一些上述便利。