Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

azure - How to open blobs on browser without downloading through java service?

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.

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;
    } 

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

perms.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...