【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
问题描述
查看Java Storage SDK,想找一个 uploadWithResponse 的示例代码,但是通过全网搜索,结果没有任何有帮助的代码。使用最近ChatGPT来寻求答案,得到非常有格式的内容:
问:java azure storage account to use uploadWithResponse |
答:To use the
<dependency> <groupId>com.azure</groupId> <artifactId>azure-storage-blob</artifactId> <version>12.14.2</version> </dependency>
import com.azure.storage.blob.BlobClient; import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.BlobServiceClientBuilder;
String connectionString = "your_connection_string"; BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
String containerName = "your_container_name"; String blobName = "your_blob_name"; BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName); BlobClient blobClient = containerClient.getBlobClient(blobName);
import com.azure.storage.blob.models.BlobUploadFromFileOptions; import java.nio.file.Path; import java.nio.file.Paths; Path filePath = Paths.get("path/to/your/file"); BlobUploadFromFileOptions options = new BlobUploadFromFileOptions(filePath); blobClient.uploadWithResponse(options, null, null);
This will upload the file to the specified blob in the Azure Blob Storage container. Remember to replace the placeholders with your actual connection string, container name, blob name, and file path. |
继续追问:BlobParallelUploadOptions with string content |
答:(前面4步的答案一样,第五步的内容为)
String content = "your_string_content"; InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); long blockSize = 100 * 1024L; // 100 KB int maxConcurrency = 5; ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions(blockSize, maxConcurrency); BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream).setParallelTra |
在继续问:如何使用 “To avoid overwriting, pass "*" to setIfNoneMatch(String ifNoneMatch). how to use the setifnonematch” |
答:(前面4步的答案一样,第五步的内容为)
BlobUploadOptions options = new BlobUploadOptions() .setInputStream(inputStream) .setLength((long) content.length()) .setHttpHeaders(new BlobHttpHeaders().setContentType("text/plain")) .setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*"));
blobClient.uploadWithResponse(options, null, null); This will upload the string content to the specified blob in the Azure Blob Storage container only if the blob does not already exist. If the blob exists, the upload will fail with a |
问题解答
根据ChatGPT提供的三部分信息,通过人力拼接和测试。得到了成功执行的uploadWithResponse代码:
uploadWithResponse示例代码
String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=***************"; String containerName = "******************"; String fileName = "******************"; BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() .connectionString(storageConnectionString) .buildClient(); BlobContainerClient containerClient = blobServiceClient.createBlobContainer(containerName); BlobClient blobClient = containerClient.getBlobClient(fileName);
//准备content并且初始化blob parallel upload options对象
String content = "Test to append new content into exists blob! by blogs lu bian liang zhan deng "; InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream, content.getBytes().length); options.setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*")); Response<BlockBlobItem> rsp = blobClient.uploadWithResponse(options, null, null); if(rsp.getStatusCode()==201) { System.out.println("append content successful........"); }
说明:
1) content 中为需要写入Blob的内容
2) 把string转换为以UTF_8编码的input stream
3) 根据 input stream来初始化 blob paralle upload options对象
4) 设置 Request Conditions,当不需要重写的时候,可以使用 setIfNoneMatch("*")。如果操作的文件存在,则会出现 Status code 409, BlobAlreadyExistss 提示。
5) 调用upload with response方法,获取返回值,如果 返回值得status code为 201,表示Storage Account接受了这次 blob 内容的改动。
运行效果展示图
参考资料
BlobClient Class:https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.BlobClient?view=azure-java-stable
BlobRequestConditions Class:https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.models.blobrequestconditions?view=azure-java-stable#com-azure-storage-blob-models-blobrequestconditions-setifnonematch(java-lang-string)
适用于 Java 的 Azure Blob 存储客户端库 : https://docs.azure.cn/zh-cn/storage/blobs/storage-quickstart-blobs-java?tabs=powershell%2Cmanaged-identity%2Croles-azure-portal%2Csign-in-azure-cli#upload-blobs-to-a-container
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-05-08 【Azure Developer】解答《美丽的数学》一书中P120页的一道谜题:寻找第四个阶乘和数