如何使用Android设备上的Azure SAS URL上载媒体(.mp4)文件

时间:2017-03-22 15:28:20

标签: android azure-storage-blobs azure-media-services

我正在Android上构建丰富的视频库并尝试上传视频,因为我不希望客户端访问我的媒体密钥和其他详细信息我已经调用我的服务器并返回SAS url作为客户端将使用的响应上传媒体

只是想知道什么是在这里实现上传的最佳方式,我尝试使用okHttp库并且大部分时间都成功()几次我得到大文件的scoket超时异常但是不确定如何使用azure执行okHttp,

理论上大块媒体文件和上传总是好的,所以任何人都可以告诉我使用SAS网址从android上传azure blob文件的最佳方法是什么

1 个答案:

答案 0 :(得分:0)

您可以尝试将其剪切为块,然后上传不超过4Mb的单独部分,而不是上传大型文件。一旦上传了所有部分(块),您需要全部提交它们并给出它们应该出现的顺序。

enter image description here

enter image description here

enter image description here

有关详细信息,请参阅SO中的this similar topic

编辑:

以下是与OkHttp 3.6.0和SAS URL一起使用的示例Java代码。

public class UploadBlobUsingSASUrl {

    public static void main(String[] args) throws IOException {

        UploadBlobUsingSASUrl uploadHandler = new UploadBlobUsingSASUrl();

        String sasSignature = "?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2017-03-30T23:39:00Z&st=2017-03-20T15:39:00Z&spr=https&sig=A2m1O5qZf79L7925DHMJtfkBRI6GVFhYcrGLStBo%2BL0%3D";
        String blobStorageEndpoint = "https://<your-account>.blob.core.windows.net/<your-container>/<your-blob>" + sasSignature;

        InputStream inStream = new FileInputStream("<file-path>");
        BufferedInputStream bis = new BufferedInputStream(inStream);
        List<String> blockIds = new ArrayList<String>();

        int counter = 1;
        while (bis.available() > 0) {
            int blockSize = 4 * 1024 * 1024; // 4 MB;
            int bufferLength = bis.available() > blockSize ? blockSize : bis.available();

            byte[] buffer = new byte[bufferLength];
            bis.read(buffer, 0, buffer.length);
            String blockId = Base64.getEncoder().encodeToString(("Block-" + counter++).getBytes("UTF-8"));
            uploadHandler.UploadBlock(blobStorageEndpoint, buffer, blockId);
            blockIds.add(blockId);
        }

        uploadHandler.CommitBlockList(blobStorageEndpoint, blockIds);

        bis.close();
        inStream.close();
    }

    public void UploadBlock(String baseUri, byte[] blockContents, String blockId) throws IOException {

        OkHttpClient client = new OkHttpClient();

        MediaType mime = MediaType.parse("");
        RequestBody body = RequestBody.create(mime, blockContents);

        String uploadBlockUri = baseUri + "&comp=block&blockId=" + blockId;

        Request request = new Request.Builder()
                .url(uploadBlockUri)
                .put(body)
                .addHeader("x-ms-version", "2015-12-11")
                .addHeader("x-ms-blob-type", "BlockBlob")
                .build();

        client.newCall(request).execute();

    }

    public void CommitBlockList(String baseUri, List<String> blockIds) throws IOException {

        OkHttpClient client = new OkHttpClient();

        StringBuilder blockIdsPayload = new StringBuilder();
        blockIdsPayload.append("<?xml version='1.0' ?><BlockList>");
        for (String blockId : blockIds) {
            blockIdsPayload.append("<Latest>").append(blockId).append("</Latest>");
        }
        blockIdsPayload.append("</BlockList>");

        String putBlockListUrl = baseUri + "&comp=blocklist";
        MediaType contentType = MediaType.parse("");
        RequestBody body = RequestBody.create(contentType, blockIdsPayload.toString());

        Request request = new Request.Builder()
                .url(putBlockListUrl)
                .put(body)
                .addHeader("x-ms-version", "2015-12-11")
                .build();

        client.newCall(request).execute();
    }