【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
问题描述
查看Java Storage SDK,想找一个 uploadWithResponse 的示例代码,但是通过全网搜索,结果没有任何有帮助的代码。使用最近ChatGPT来寻求答案,得到非常有格式的内容:
| 问:java azure storage account to use uploadWithResponse |
|
答:To use the
<dependency>
import com.azure.storage.blob.BlobClient;
String connectionString = "your_connection_string";
String containerName = "your_container_name";
import com.azure.storage.blob.models.BlobUploadFromFileOptions; 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"; |
|
在继续问:如何使用 “To avoid overwriting, pass "*" to setIfNoneMatch(String ifNoneMatch). how to use the setifnonematch” |
答:(前面4步的答案一样,第五步的内容为)
BlobUploadOptions options = new BlobUploadOptions()
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
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)的更多相关文章
- 【Azure 存储服务】代码版 Azure Storage Blob 生成 SAS (Shared Access Signature: 共享访问签名)
问题描述 在使用Azure存储服务,为了有效的保护Storage的Access Keys.可以使用另一种授权方式访问资源(Shared Access Signature: 共享访问签名), 它的好处可 ...
- 【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob
问题描述 在使用Azure的存储服务时候,如果上传的文件大于了100MB, 1GB的情况下,如何上传呢? 问题解答 使用Azure存储服务时,如果要上传文件到Azure Blob,有很多种工具可以实现 ...
- 解读 Windows Azure 存储服务的账单 – 带宽、事务数量,以及容量
经常有人询问我们,如何估算 Windows Azure 存储服务的成本,以便了解如何更好地构建一个经济有效的应用程序.本文我们将从带宽.事务数量,以及容量这三种存储成本的角度探讨这一问题. 在使用 W ...
- 玩转Windows Azure存储服务——网盘
存储服务是除了计算服务之外最重要的云服务之一.说到云存储,大家可以想到很多产品,例如:AWS S3,Google Drive,百度云盘...而在Windows Azure中,存储服务却是在默默无闻的工 ...
- 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed
问题描述 使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见 The Azure Storage endpoint ...
- 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
什么是表存储 Azure 表存储是一项用于在云中存储结构化 NoSQL 数据的服务,通过无结构化的设计提供键/属性存储. 因为表存储无固定的数据结构要求,因此可以很容易地随着应用程序需求的发展使数据适 ...
- 玩转Windows Azure存储服务——高级存储
在上一篇我们把Windows Azure的存储服务用作网盘,本篇我们继续挖掘Windows Azure的存储服务——高级存储.高级存储自然要比普通存储高大上的,因为高级存储是SSD存储!其吞吐量和IO ...
- 【Azure 存储服务】Hadoop集群中使用ADLS(Azure Data Lake Storage)过程中遇见执行PUT操作报错
问题描述 在Hadoop集中中,使用ADLS 作为数据源,在执行PUT操作(上传文件到ADLS中),遇见 400错误[put: Operation failed: "An HTTP head ...
- 【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...
- oracle顺序控制语句goto、null和分页过程中输入输出存储、java程序的调用过程
顺序控制语句1 goto建议不要使用 declare i number:=; begin loop dbms_output.put_line(i); then goto end_loop; end i ...
随机推荐
- mybatis_19
id username birthday sex address 1 王五 2 10 张三 2014-07-10 1 北京市 16 张小明 1 河南郑州 22 陈小明 1 河南郑州 24 张三丰 1 ...
- Android工程接入UnityLibrary工程
目录结构 Unity工程指UnityLibrary目录下文件: 安卓工程指app目录下文件: 整体指App目录下不包括app和UnityLibrary: 1.Unity打包时勾选导出安卓工程: 拷贝g ...
- mybatis-plus使用FIND_IN_SET
xxxQueryWrapper.eq("is_deleted","0").apply(deptUser.getDeptId() != null,"de ...
- 帮你积累音视频知识,Agora 开发者漫游指南正式启航
"运气是设计的残留物."--John Milton 如果玩过<全面战争:中世纪 II>,或者读过 John Milton 书的人,可能对这句话有印象.我们发现,很多小伙 ...
- Python爬虫采集商品评价信息--京东
1.数据采集逻辑 在进行数据采集之前,明确哪些数据为所需,制定数据Schema为爬取工作做出要求,并根据数据Schema制定出有针对性的爬取方案和采集逻辑. 2.数据Schema 3.数据爬取 抓取京 ...
- Vue-Router 路由与配置
现在的很多应用都流行SPA应用(singe page application) . 传统的项目大多使用多页面结构,需要切换内容的时候我们往往会进行单个html文件的跳转,这个时候因受到网络.性能的影 ...
- ChatGPT 何许人也
有时候会想,如果chatGPT是人,它会是一个怎样的人呢?下面是我的推测过程. 首先,她应该是女人 会推测而不会计算 你问它: 3457 * 43216 = ,它回答 149261912 (这是错的. ...
- 常用Linux发行版操作系统大盘点
B站CodeSheep的教程 https://www.bilibili.com/read/cv6026694
- golang pprof监控系列(2) —— memory,block,mutex 使用
golang pprof监控系列(2) -- memory,block,mutex 使用 大家好,我是蓝胖子. profile的中文被翻译轮廓,对于计算机程序而言,抛开业务逻辑不谈,它的轮廓是是啥呢? ...
- ArrayBuffer、Float32Array、Uint8Array 详解
ArrayBuffer ArrayBuffer()是一个普通的JavaScript构造函数,可用于在内存中分配特定数量的字节空间. const buf = new ArrayBuffer(16); / ...