Reputation: 1118
Hi I am trying to save image on azure storage , I already have configuration step and I have upload method
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(sourceFile.toPath());
TransferManager.uploadFileToBlockBlob(fileChannel, blob, 8 * 1024 * 1024, null).subscribe(response -> {
System.out.println("Completed upload request.");
System.out.println(response.response().statusCode());
});
How can I get the url image path on azure to save it on database and show it on my website?
Upvotes: 0
Views: 942
Reputation: 24128
As @GauravMantri said, you can get the URL of a blob via blob.toURL()
. Then, if the container of the blob is public (be set public access level) and the ContentType
property of the blob is set correctly like image/png
, you can directly access the image via the url, such as to use in an img
tag to show in a web page below.
<img src="myaccountname.blob.core.windows.net/test/testURL">
However, considering for secure access, a container is set private access level, please refer to the offical documents Secure access to an application's data in the cloud
and Using shared access signatures (SAS)
. Then, we need to generate a blob url with SAS signature for accessing.
Here is the sample code to generate a blob url with SAS signature.
SharedKeyCredentials credentials = new SharedKeyCredentials(accountName, accountKey);
ServiceSASSignatureValues values = new ServiceSASSignatureValues()
.withProtocol(SASProtocol.HTTPS_ONLY) // Users MUST use HTTPS (not HTTP).
.withExpiryTime(OffsetDateTime.now().plusDays(2)) // 2 days before expiration.
.withContainerName(containerName)
.withBlobName(blobName);
BlobSASPermission permission = new BlobSASPermission()
.withRead(true)
.withAdd(true)
.withWrite(true);
values.withPermissions(permission.toString());
SASQueryParameters serviceParams = values.generateSASQueryParameters(credentials);
String sasSign = serviceParams.encode();
String blobUrlWithSAS = String.format(Locale.ROOT, "https://%s.blob.core.windows.net/%s/%s%s",
accountName, containerName, blobName, sasSign);
You also can add the SAS signature at the end of the string of blob.toURL()
.
String blobUrlWithSAS = blob.toString()+sasSign;
About SAS Signature, you can refer to these sample codes in ServiceSASSignatureValues Class
and AccountSASSignatureValues Class
.
Upvotes: 1