从 Android 示例上传 Azure 存储块 blob

2024-04-09

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

    // 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 达到一定大小(具体取决于我测试的手机)时,我开始出现内存不足异常。我想分割 blob 并分块上传。但是,我在网上找到的所有示例都是基于 C# 的,并且使用存储客户端库。我正在寻找一个 Java/Android 示例,该示例使用 Azure Storage Rest API 上传块中的 blob。


发布了一个 Azure Storage Android 库here https://github.com/Azure/azure-storage-android/。一个基本的blob 存储示例 https://github.com/Azure/azure-storage-android/blob/master/microsoft-azure-storage-samples/src/com/microsoft/azure/storage/blob/gettingstarted/BlobBasics.java位于示例文件夹中。您可能想要使用的方法是 blob 类中的 uploadFromFile。默认情况下,如果大小小于 64MB,则尝试将 blob 放入单个 put 中,否则以 4MB 块的形式发送 blob。如果要减少 64MB 限制,可以在 CloudBlobClient 的 BlobRequestOptions 对象上设置 singleBlobPutThresholdInBytes 属性(这将影响所有请求)或传递给 uploadFromFile 方法(仅影响该请求)。存储库包括许多方便的功能,例如自动重试和跨块放置请求的最大执行超时,这些功能都是可配置的。

如果您仍然想使用更手动的方法,PutBlock 和 Put Block List API 参考是here http://msdn.microsoft.com/en-us/library/azure/dd135726.aspx并提供通用的跨语言文档。这些在 Azure 存储 Android 库的 CloudBlockBlob 类中有很好的包装器,称为 uploadBlock 和 commitBlockList,这可以在手动请求构建中节省大量时间,并可以提供一些上述便利。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Android 示例上传 Azure 存储块 blob 的相关文章

随机推荐