我正在使用
azure-storage-blob
Java SDK 版本 12.8.0
和 SpringBoot 2.x,我想从本地存储到 Azure blob 容器进行分段上传,当仅在代码中指定文件名时效果很好。
这是示例代码
String folder = "20241024T180025";
String filename = "corey.jpg";
String folderandfilename = folder + "/" + filename;
BlobContainerClient containerClient = new BlobContainerClientBuilder()
.connectionString(connectionString)
.containerName(containerName)
.buildClient();
BlockBlobClient blobClient = containerClient.getBlobClient(filename).getBlockBlobClient();
List<String> blockIds = new ArrayList<>();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
byte[] buffer = new byte[(int) chunkSize];
int bytesRead;
int blockNumber = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
String blockId = Base64.getEncoder().encodeToString(String.format("block-%07d", blockNumber).getBytes(StandardCharsets.UTF_8));
byte[] uploadBuffer;
if (bytesRead < buffer.length) {
uploadBuffer = Arrays.copyOf(buffer, bytesRead);
} else {
uploadBuffer = buffer;
}
blockIds.add(blockId);
blobClient.stageBlock(blockId, new ByteArrayInputStream(uploadBuffer), bytesRead);
blockNumber++;
}
blobClient.commitBlockList(blockIds);
}
我还需要创建一个文件夹来存储上传的文件,因此我为其分配了字符串文件夹和文件名。
BlockBlobClient blobClient = containerClient.getBlobClient(folderandfilename).getBlockBlobClient();
但是失败并显示错误消息:
状态代码 400,“
指定的 blob 或块内容无效。InvalidBlobOrBlock
BlockBlobClient 似乎接受“corey.jpg”之类的文件名,但不支持“20241024T180025/corey.jpg”之类的文件名,我可以做些什么来解决该问题并实现我的目标吗?
如果有任何想法,我将不胜感激,谢谢。
如何使用 BlockBlobClient 将文件上传到文件夹?
在 Azure Blob 存储中,文件夹是虚拟目录。因此,
"20241024T180025/corey.jpg"
应被视为单个 blob 名称,而不是实际的目录结构。
您可以使用以下代码通过 Azure java SDK 将文件上传到使用
BlockBlobClient
的文件夹。
代码:
String connectionString = "xxxxx";
String containerName = "venkat";
String folder = "20241024T180025";
String filename = "corey.jpg";
String folderandfilename = folder + "/" + filename;
long chunkSize = 4 * 1024 * 1024; // 4MB chunk size, adjust if needed
File file = new File("C:\\Users\\v-vsettu\\venkat\\openai\\image stamp.jpg");
BlobContainerClient containerClient = new BlobContainerClientBuilder()
.connectionString(connectionString)
.containerName(containerName)
.buildClient();
BlockBlobClient blobClient = containerClient.getBlobClient(folderandfilename).getBlockBlobClient();
List<String> blockIds = new ArrayList<>();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
byte[] buffer = new byte[(int) chunkSize];
int bytesRead;
int blockNumber = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
String blockId = Base64.getEncoder().encodeToString(String.format("block-%07d", blockNumber).getBytes(StandardCharsets.UTF_8));
byte[] uploadBuffer;
if (bytesRead < buffer.length) {
uploadBuffer = Arrays.copyOf(buffer, bytesRead);
} else {
uploadBuffer = buffer;
}
blockIds.add(blockId);
blobClient.stageBlock(blockId, new ByteArrayInputStream(uploadBuffer), bytesRead);
blockNumber++;
}
blobClient.commitBlockList(blockIds);
System.out.println("File uploaded successfully as " + folderandfilename);
} catch (IOException e) {
e.printStackTrace();
}
输出:
File uploaded successfully as 20241024T180025/corey.jpg
传送门: