将 base64 上传到 azure 文件共享
Posted
技术标签:
【中文标题】将 base64 上传到 azure 文件共享【英文标题】:Uploading base64 to azure file share 【发布时间】:2021-05-03 22:10:03 【问题描述】:我正在尝试将 base64 图像上传到 Azure 存储文件共享,但发生错误。
我该如何解决?
ShareFileClient dirClient = new ShareFileClientBuilder()
.endpoint("https://xxx.file.core.windows.net")
.shareName("xxx")
.resourcePath("photos")
.credential(new StorageSharedKeyCredential("xxx", "xxx"))
.buildFileClient();
byte[] bytes = Base64.decodeBase64("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes))
dirClient.upload(dataStream, bytes.length);
catch (IOException e)
e.printStackTrace();
pom.xml
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-file-share</artifactId>
<version>12.2.0</version>
</dependency>
错误
Status code 416, "<?xml version="1.0" encoding="utf-8"?>
<Error>
<Code>InvalidRange</Code>
<Message>The range specified is invalid for the current size of the resource.
RequestId:e9efce20-501a-006d-31a5-f67649000000 Time:2021-01-30T01:14:24.3586552Z
</Message>
</Error>
参考:https://github.com/Azure/azure-sdk-for-java/tree/azure-storage-blob_12.4.0/sdk/storage/azure-storage-file-share
【问题讨论】:
【参考方案1】:解决方案
// Create a source file client
ShareFileClient dirClient = new ShareFileClientBuilder()
.endpoint("https://xxx.file.core.windows.net")
.shareName("xxx")
.credential(new StorageSharedKeyCredential("xxx", "xxx"))
.resourcePath("photos" + "/" + "test.png")
.buildFileClient();
var base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
// Create a source file
try
var sizeInBytes = 4 * Math.ceil(base64Image.length() / 3)*0.5624896334383812;
dirClient.create((long) sizeInBytes);
catch (ShareStorageException e)
System.out.println("Failed to create source client. Reasons: " + e.getMessage());
// decodeBase64
byte[] bytes = Base64.decodeBase64(base64Image);
// Upload
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes))
dirClient.upload(dataStream, bytes.length);
catch (IOException e)
e.printStackTrace();
【讨论】:
以上是关于将 base64 上传到 azure 文件共享的主要内容,如果未能解决你的问题,请参考以下文章
我们可以在 .net core 3.1 中一次性上传多少大文件到 Azure 文件共享