0

我正在尝试将 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

4

1 回答 1

0

解决方案

// 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();
}
于 2021-01-30T02:44:50.513 回答