使用Java的Azure文件映像路径 [英] Azure file image path using java
问题描述
我正在尝试将图像保存在Azure存储器上,我已经有了配置步骤,并且有了上载方法
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());
});
我如何在azure上获取url图像路径以将其保存在数据库中并显示在我的网站上?
How can I get the url image path on azure to save it on database and show it on my website?
推荐答案
正如@GauravMantri所说,您可以通过blob.toURL()
获取Blob的URL.然后,如果Blob的容器是公共的(设置为公共访问级别),并且Blob的ContentType
属性已正确设置为image/png
,则可以直接通过url访问图像,例如在img
标签显示在下面的网页中.
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">
但是,考虑到安全访问,已将容器设置为私有访问级别,请参阅官方文档 Using shared access signatures (SAS)
.然后,我们需要生成带有SAS签名的Blob网址以进行访问.
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.
这是生成带有SAS签名的blob URL的示例代码.
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);
您也可以在blob.toURL()
字符串的末尾添加SAS签名.
You also can add the SAS signature at the end of the string of blob.toURL()
.
String blobUrlWithSAS = blob.toString()+sasSign;
关于SAS签名,您可以在 AccountSASSignatureValues Class
.
About SAS Signature, you can refer to these sample codes in ServiceSASSignatureValues Class
and AccountSASSignatureValues Class
.
这篇关于使用Java的Azure文件映像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!