【问题标题】:Azure storage block blob upload from android example从 android 示例上传 Azure 存储块 blob
【发布时间】:2014-08-16 22:52:57
【问题描述】:

我正在使用来自 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# 的,并且使用的是 Storage Client 库。我正在寻找一个 Java/Android 示例,该示例使用 Azure Storage Rest API 上传块中的 blob。

【问题讨论】:

    标签: java android azure-blob-storage azure-storage


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我不敢相信我没有遇到这个库,并且不知何故一直返回到移动服务客户端库。不过有一件事,CloudBlockBlob 类中的大多数方法都希望我提供我的存储帐户名称和密钥。我不确定将这些与我的应用一起分发是否是个好主意。
    • 绝对不要分发账号/密钥!使用公共访问策略或共享访问签名。有关 SAS 的更多信息是 herehere。公共访问将在here 进行更详细的讨论。要查看的适当 API 是容器 uploadPermissions、blob/container generateSharedAccessSignature 以及 BlobContainerPermissions 和 SharedAccessBlobPolicy 类。
    • 非常有帮助。但是我找不到任何与通知上传或下载完成相关的功能,我该怎么做?
    • @EmilyGerner,感谢您的链接。 Github 上的项目还在维护吗?我看到最后一次提交是 3 年前。我们还能使用这个库吗?感谢您的宝贵时间。
    • 我猜java/android的最新版本是这样的:docs.microsoft.com/en-us/java/api/overview/azure/…
    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2020-08-12
    • 2019-03-18
    • 2019-04-06
    • 2014-08-23
    • 2016-03-24
    • 2016-08-26
    • 1970-01-01
    相关资源
    最近更新 更多