我正在尝试上传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
解决方案
// 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();
}