如何在不通过Java服务下载的情况下在浏览器上打开Blob? [英] How to open blobs on browser without downloading through java service?

查看:241
本文介绍了如何在不通过Java服务下载的情况下在浏览器上打开Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,该应用程序需要在浏览器中查看blob,而不是下载它们.当前,具有令牌的Blob链接会下载相应的Blob.

I am creating an application where I need to view blobs in browser rather than downloading them. Currently, links of blobs having token downloads the corresponding blob.

我在这里有一些参考,可以在浏览器中查看blob: https://github.com. com/Azure-Samples/storage-blob-java-getting-started/blob/master/src/BlobBasics.java (请参见第141行)

I got some reference here to view the blobs in browser : https://github.com/Azure-Samples/storage-blob-java-getting-started/blob/master/src/BlobBasics.java (See from line number 141)

这是我创建令牌的代码:

Here is my code to create token :

@Test
    public String testBlobSaS(CloudBlob blob, CloudBlobContainer container) throws InvalidKeyException,
            IllegalArgumentException, StorageException, URISyntaxException, InterruptedException {
        SharedAccessBlobPolicy sp = createSharedAccessBlobPolicy(
                EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST), 100);
        BlobContainerPermissions perms = new BlobContainerPermissions();
        perms.getSharedAccessPolicies().put("readperm", sp);
        perms.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
        container.uploadPermissions(perms);
        String sas = blob.generateSharedAccessSignature(sp, null);
        CloudBlockBlob sasBlob = new CloudBlockBlob(
                new URI(blob.getUri().toString() + "?" + blob.generateSharedAccessSignature(null, "readperm")));
        sasBlob.download(new ByteArrayOutputStream());
        CloudBlob blobFromUri = new CloudBlockBlob(
                PathUtility.addToQuery(blob.getStorageUri(), blob.generateSharedAccessSignature(null, "readperm")));
        assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
                blobFromUri.getServiceClient().getCredentials().getClass().toString());
        StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
                blob.generateSharedAccessSignature(null, "readperm"));
        CloudBlobClient bClient = new CloudBlobClient(sasBlob.getServiceClient().getStorageUri(), creds);
        CloudBlockBlob blobFromClient = bClient.getContainerReference(blob.getContainer().getName())
                .getBlockBlobReference(blob.getName());
        assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
                blobFromClient.getServiceClient().getCredentials().getClass().toString());
        assertEquals(bClient, blobFromClient.getServiceClient());
        return sas;
    } 

我已将此行添加到来自先前提供的url引用的代码中:

I have added this line into code from reference of url provided earlier:

perms.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

我有一些代码,可为我提供带有令牌的blob网址,例如:

I have code which gives me url for blob with token like :

https://accountName.blob.core.windows.net/directories /blobName?token

仍然使用此URL,它正在下载相应的Blob. 创建令牌时,我应该对代码进行哪些更改,以便无需下载就可以在浏览器本身中查看blob?

Still with this url, it's downloading the respective blob. What changes I should make in code while creating token, so that I can view blobs in browser itself without downloading?

推荐答案

您要检查的第一件事是Blob的content-type属性.很有可能,blob的内容类型将为application/octet-stream(这是默认的内容类型).因此,浏览器无法理解如何处理该Blob,因此无法下载.请尝试将Blob的内容类型更改为适当的值(例如image/png),这应该可以解决问题.

First thing you would want to check is the content-type property of the blob. In all likelihood, the content type of the blob would be application/octet-stream (which is the default content type). Because of this the browser is not understanding what to do with this blob and thus downloading it. Please try to change the content type of the blob to appropriate value (e.g. image/png) and that should fix the problem.

我还注意到您正在将容器的ACL设置为BlobContainerPublicAccessType.CONTAINER.如果执行此操作,则无需创建共享访问签名(SAS).您的Blob可以通过其URL(https://accountname.blob.core.windows.net/containername/blobname)轻松访问.当容器的ACL为Private时,SAS开始起作用.

Also I noticed that you're setting the container's ACL to BlobContainerPublicAccessType.CONTAINER. If you're doing this, then there's no need for you to create Shared Access Signature (SAS). Your blobs will be accessible simply by their URL (https://accountname.blob.core.windows.net/containername/blobname). SAS comes into play when the container's ACL is Private.

这篇关于如何在不通过Java服务下载的情况下在浏览器上打开Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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