来自android示例的Azure存储块blob上传 [英] Azure storage block blob upload from android example

查看:135
本文介绍了来自android示例的Azure存储块blob上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following code from an android application to upload a blob to Azure Blob Storage. Note: the sasUrl parameter below is a signed url acquired from my web service :

    // 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并将其上传到块中。但是,我在Web上找到的所有示例都是基于C#的,并且正在使用Storage Client库。我正在寻找使用Azure Storage Rest API在块中上传blob的Java / Android示例。

The code is working fine for small blobs but when a blob reaches a certain size depending on the phone I am testing with, I start to get out of memory exceptions. I would like to split the blobs and upload them in blocks. However, all the examples I find on the web are C# based and are using the Storage Client library. I am looking for a Java/Android example that uploads a blob in blocks using the Azure Storage Rest API.

推荐答案

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

There is an Azure Storage Android library published here. A basic blob storage example is in the samples folder. The method you’d probably like to use is uploadFromFile in the blob class. This will, by default attempt to put the blob in a single put if the size is less than 64MB and otherwise send the blob in 4MB blocks. If you’d like to reduce the 64MB limit, you can set the singleBlobPutThresholdInBytes property on the BlobRequestOptions object of either the CloudBlobClient (which will affect all requests) or passed to the uploadFromFile method (to affect only that request). The storage library includes many convenient features such as automated retries and maximum execution timeout across the block put requests which are all configurable.

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

If you’d still like to use a more manual approach, the PutBlock and Put Block List API references are here and provide generic, cross-language documentation. These have nice wrappers in the CloudBlockBlob class of the Azure Storage Android library called uploadBlock and commitBlockList which may save you a lot of time in manual request construction and can provide some of the aforementioned conveniences.

这篇关于来自android示例的Azure存储块blob上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆